Closed dhdaines closed 2 months ago
Hi!
You are correct that the .git/
directory is not present during the build itself (and thus not available to buildpacks). However, this design choice is not specific to the Python buildpack, but instead the whole Heroku build system. It has been implemented this way for some time - I would imagine due to some combination of:
.git/
directory from ending up in the sluggit.heroku.com
server to the build systemHowever, the originally commit information is available via the SOURCE_VERSION
env var:
https://devcenter.heroku.com/articles/buildpack-api#bin-compile
Since this issue is not specific to the Python buildpack, it would be best to file this as a "make Git repo available during the build" feature request here: https://github.com/heroku/roadmap
As a workaround for this specific scenario, it seems your options are either:
SETUPTOOLS_SCM_PRETEND_VERSION
env var on your app to a fixed fake value (which as you say, won't match the actual version)setuptools-scm
to use SOURCE_VERSION
if it's set.setuptools-scm
can't be made to use SOURCE_VERSION
directly, then to create a buildpack that exports the SOURCE_VERSION
env var to the env var SETUPTOOLS_SCM_PRETEND_VERSION
using the export
file: https://devcenter.heroku.com/articles/buildpack-api#composing-multiple-buildpacksThanks for the really detailed response - I definitely understand why copying the .git
folder to the deployment isn't a great idea. I'm a little bit surprised nobody ran into this problem before though...
It looks like there is a workaround possible, so I'll post it here when I figure it out and close the issue at that point (since as you say this is by design).
No problem!
- Or, if
setuptools-scm
can't be made to useSOURCE_VERSION
directly, then to create a buildpack that exports theSOURCE_VERSION
env var to the env varSETUPTOOLS_SCM_PRETEND_VERSION
using theexport
file: https://devcenter.heroku.com/articles/buildpack-api#composing-multiple-buildpacks
I haven't tested this (so there might be a typo in there), but a buildpack with a bin/compile
script with something like the following contents should work:
#!/usr/bin/env bash
set -euo pipefail
BUILDPACK_DIR=$(cd $(dirname $0); cd ..; pwd)
# Make the `SETUPTOOLS_SCM_PRETEND_VERSION` env var available to later buildpacks.
# `SOURCE_VERSION` is set by the Heroku build system:
# https://devcenter.heroku.com/articles/buildpack-api#bin-compile
echo "export SETUPTOOLS_SCM_PRETEND_VERSION='${SOURCE_VERSION}'" > "${BUILDPACK_DIR}/export"
Closing since this isn't an issue with the Python buildpack, and there are workarounds available.
It's become common practice to use something like setuptools-scm to manage package versioning automatically using Git or other version control history (sometimes via another tool like hatch-vcs).
What this often means in practice (and I admit it is not too clear from the setuptools-scm documentation) is that the project version is defined in a Python module, e.g.
mypackage/__version__.py
which is NOT checked in to Git but is generated dynamically when the project is installed (possibly in "editable" mode withpip install -e .
).You may see where I'm going with this - this is fundamentally incompatible with the way Heroku deploys things, because, from what I can tell:
requirements.txt
file.bin/post_compile
to actually, um, compile your app.So, you can hack around this by using the
SETUPTOOLS_SCM_PRETEND_VERSION
environment variable, which I guess is fine as long as you don't actually care about having the same version number as you otherwise would have.In the case where your Heroku app is also a library distributed on PyPI, this is kind of annoying.