Test ignores Docker stage scoping of ARG VERSION, so it can pass while VERSION is empty at build time
The finding
The test finds the first ARG VERSION line anywhere in the Dockerfile. It then only checks that the uv build RUN comes after it. In Docker, an ARG declared before the first FROM (global scope) is not visible inside a stage unless that stage re-declares it. Likewise, an ARG declared in one build stage does not carry over to a later stage after another FROM. So a Dockerfile with ARG VERSION at the top, or in an earlier stage, and uv build in a later stage would pass this test. In that case $VERSION would expand to an empty string, and hatch-vcs would get an empty SETUPTOOLS_SCM_PRETEND_VERSION. The image would then be built with the wrong (fallback) version, which is exactly the regression this test is meant to catch.
1| """The published image reports its tag's version, not whatever git inside the build makes of the tree.
2|
3| v0.1.0's image reported ``0.1.1.dev0+g69730fbd9.d<date>``: ``.dockerignore`` left the tracked ``.github`` out of the
4| build context, so hatch-vcs saw a dirty tree. The release now passes the tag in as ``VERSION``, checks the built image
5| reports it before pushing, and the context leaves out nothing git tracks.
6| """
7|
8| from __future__ import annotations
9|
10| import fnmatch
11| import re
12| import shutil
13| import subprocess
14| from pathlib import Path
15|
16| import pytestThe work pack
Recommendation. Look for the last FROM line before the build RUN. Require the ARG VERSION declaration to fall between that FROM and the build RUN, i.e. inside the same stage. Reject an ARG that only appears before the first FROM or in an earlier stage.
Acceptance criteria
- Address: Test ignores Docker stage scoping of ARG VERSION, so it can pass while VERSION is empty at build time
- Keep the change scoped to the cited file/symbols unless impact requires otherwise
- Look for the last
FROMline before the build RUN. Require theARG VERSIONdeclaration to fall between that FROM and the build RUN, i.e. inside the same stage. Reject an ARG that only appears before the first FROM or in an earlier stage. - Re-check
tests/test_image_version.pyafter the fix
Status
- claimed
- resolved
The fix
f6f9eb43c4 test: the image version check requires ARG VERSION in the building stage.
@@ -38,14 +38,34 @@ def test_release_passes_the_tag_version_as_a_build_arg(): assert "value=${{ needs.release-tag.outputs.tag }}" in meta["with"]["tags"] -def test_dockerfile_builds_the_wheel_with_the_version_arg():- text = re.sub(r"\\\n", " ", (ROOT / "Dockerfile").read_text())- lines = text.splitlines()- arg = next((i for i, line in enumerate(lines) if re.match(r"\s*ARG\s+VERSION(=|\s|$)", line)), None)- assert arg is not None, "the Dockerfile declares no ARG VERSION"+def version_arg_problem(dockerfile: str) -> str | None:+ """Why ``dockerfile`` would build the wheel without ``VERSION``, or None. An ARG is scoped to its stage: one+ before the first FROM, or in an earlier stage, is empty in the stage that builds unless declared there again."""+ lines = re.sub(r"\\\n", " ", dockerfile).splitlines() build = next((i for i, line in enumerate(lines) if re.match(r"\s*RUN\s", line) and "uv build" in line), None)- assert build is not None and build > arg, "the wheel must be built after ARG VERSION"- assert 'SETUPTOOLS_SCM_PRETEND_VERSION="$VERSION"' in lines[build], "the build RUN must hand VERSION to hatch-vcs"+ if build is None:+ return "no RUN builds the wheel"+ stage = max((i for i in range(build) if re.match(r"\s*FROM\s", lines[i], re.IGNORECASE)), default=None)+ if stage is None:+ return "the wheel is built outside any stage"+ if not any(re.match(r"\s*ARG\s+VERSION(=|\s|$)", line) for line in lines[stage + 1 : build]):+ return "ARG VERSION is not declared in the stage that builds the wheel, before the build"+ if 'SETUPTOOLS_SCM_PRETEND_VERSION="$VERSION"' not in lines[build]:+ return "the build RUN must hand VERSION to hatch-vcs"+ return None+++def test_dockerfile_builds_the_wheel_with_the_version_arg():+ assert version_arg_problem((ROOT / "Dockerfile").read_text()) is None+++def test_version_arg_check_respects_stage_scope():+ build = 'RUN SETUPTOOLS_SCM_PRETEND_VERSION="$VERSION" uv build --wheel\n'+ assert version_arg_problem(f"FROM python AS app\nARG VERSION\n{build}") is None+ assert version_arg_problem(f"ARG VERSION\nFROM python\n{build}") is not None # global scope only+ assert version_arg_problem(f"FROM a\nARG VERSION\nFROM b\n{build}") is not None # an earlier stage's+ assert version_arg_problem(f"FROM python\n{build}ARG VERSION\n") is not None # declared after the build+ assert version_arg_problem("FROM python\nARG VERSION\nRUN uv build --wheel\n") is not None def test_release_checks_the_image_version_before_pushing():How it was judged
Waiver evidence waived: Test-only finding: the fix strengthens a test, so the same test runs on both trees and passes on dev's source. Verified locally by mutation: the strengthened test fails when the guarded behaviour is b
Judged 24 Nov 2026, 04:54 UTC against dev at d0c0f66.
This fix strengthens a test. The stronger test passes on dev's code as well, so there is no failure on dev for it to show. It resolved on a written waiver of at least twenty characters, recorded with the pack:
Test-only finding: the fix strengthens a test, so the same test runs on both trees and passes on dev's source. Verified locally by mutation: the strengthened test fails when the guarded behaviour is broken, where the old one passed.