setuptools_scm: setuptools-scm does not function when installing as part of a git hook while working inside of a submodule

this issue is oddly specific but here’s the reproduction:

#!/usr/bin/env bash
set -euxo pipefail

rm -rf y z home black venv

git clone https://github.com/psf/black

export PRE_COMMIT_HOME="$PWD/home"
git init y
git init z
git -C z commit --allow-empty -m 'commit!'
git -C y submodule add "$PWD/z"
cat > "$PWD/y/.git/modules/z/hooks/pre-commit" <<EOF
#!/usr/bin/env bash
virtualenv "$PWD/venv"
"$PWD/venv/bin/pip" install "$PWD/black"
EOF
chmod +x "$PWD/y/.git/modules/z/hooks/pre-commit"
cd y/z
git commit -m "test"

this currently ends as:

$ bash t.sh 
+ rm -rf y z home black venv
+ git clone https://github.com/psf/black
Cloning into 'black'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 3284 (delta 7), reused 2 (delta 1), pack-reused 3270
Receiving objects: 100% (3284/3284), 3.72 MiB | 2.15 MiB/s, done.
Resolving deltas: 100% (2223/2223), done.
+ export PRE_COMMIT_HOME=/tmp/z/home
+ PRE_COMMIT_HOME=/tmp/z/home
+ git init y
Initialized empty Git repository in /tmp/z/y/.git/
+ git init z
Initialized empty Git repository in /tmp/z/z/.git/
+ git -C z commit --allow-empty -m 'commit!'
[master (root-commit) 4f0b4dc] commit!
+ git -C y submodule add /tmp/z/z
Cloning into '/tmp/z/y/z'...
done.
+ cat
+ chmod +x /tmp/z/y/.git/modules/z/hooks/pre-commit
+ cd y/z
+ git commit -m test
Using real prefix '/usr'
Path not in prefix '/home/asottile/opt/venv/include/python3.6m' '/usr'
New python executable in /tmp/z/venv/bin/python3
Also creating executable in /tmp/z/venv/bin/python
Installing setuptools, pip, wheel...
done.
Processing /tmp/z/black
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... error
    ERROR: Command errored out with exit status 1:
     command: /tmp/z/venv/bin/python3 /tmp/z/venv/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmp33fojq8s
         cwd: /tmp/pip-req-build-6b2fw0yg
    Complete output (36 lines):
    Traceback (most recent call last):
      File "/tmp/z/venv/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 257, in <module>
        main()
      File "/tmp/z/venv/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 240, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
      File "/tmp/z/venv/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 110, in prepare_metadata_for_build_wheel
        return hook(metadata_directory, config_settings)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 156, in prepare_metadata_for_build_wheel
        self.run_setup()
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 142, in run_setup
        exec(compile(code, __file__, 'exec'), locals())
      File "setup.py", line 97, in <module>
        "blackd=blackd:patched_main [d]",
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/__init__.py", line 145, in setup
        return distutils.core.setup(**attrs)
      File "/usr/lib/python3.6/distutils/core.py", line 108, in setup
        _setup_distribution = dist = klass(attrs)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/dist.py", line 448, in __init__
        k: v for k, v in attrs.items()
      File "/usr/lib/python3.6/distutils/dist.py", line 281, in __init__
        self.finalize_options()
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/dist.py", line 740, in finalize_options
        ep.load()(self)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools/dist.py", line 747, in _finalize_setup_keywords
        ep.load()(self, ep.name, value)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools_scm/integration.py", line 17, in version_keyword
        dist.metadata.version = get_version(**value)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools_scm/__init__.py", line 150, in get_version
        parsed_version = _do_parse(config)
      File "/tmp/pip-build-env-ydhjt6g5/overlay/lib/python3.6/site-packages/setuptools_scm/__init__.py", line 113, in _do_parse
        "use git+https://github.com/user/proj.git#egg=proj" % config.absolute_root
    LookupError: setuptools-scm was unable to detect version for '/tmp/pip-req-build-6b2fw0yg'.
    
    Make sure you're either building from a fully intact git repository or PyPI tarballs. Most other sources (such as GitHub's tarballs, a git checkout without the .git folder) don't contain the necessary metadata and will not work.
    
    For example, if you're using pip, instead of https://github.com/user/proj/archive/master.zip use git+https://github.com/user/proj.git#egg=proj
    ----------------------------------------
ERROR: Command errored out with exit status 1: /tmp/z/venv/bin/python3 /tmp/z/venv/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmp33fojq8s Check the logs for full command output.

guess at a fix

I don’t know much about setuptools-scm, but I suspect a patch with functionality similar to this would fix the problem

Original issue: https://github.com/psf/black/issues/1180

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

pr upcoming - @asottile thanks for providing extensive analysis and the solution

@asottile i can confirm that the helper fixes the issue

im not sure if i can build up a sound regression test from the example, i’ll try to build a minimal regression test the other way around

thanks. it seems to be the git env vars indeed ill rework the scripts a bit more to use setuptools_scm from its own folder and copy over the clean env helpers