Skip to content

Commit b8b3c72

Browse files
authored
Docker: fix base image dependency inference for parametrized targets. (#20633)
Fixes #20632
1 parent 908d245 commit b8b3c72

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/python/pants/backend/docker/subsystems/dockerfile_parser_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,39 @@ def test_generate_lockfile_without_python_backend() -> None:
206206
"--resolve=dockerfile-parser",
207207
]
208208
).assert_success()
209+
210+
211+
def test_baseimage_dep_inference(rule_runner: RuleRunner) -> None:
212+
# We use a single run to grab all information, rather than parametrizing the test to save on
213+
# rule invocations.
214+
base_image_tags = dict(
215+
BASE_IMAGE_1=":sibling",
216+
BASE_IMAGE_2=":sibling@a=42,b=c",
217+
BASE_IMAGE_3="else/where:weird#name@with=param",
218+
BASE_IMAGE_4="//src/common:name@parametrized=foo-bar.1",
219+
BASE_IMAGE_5="should/allow/default-target-name",
220+
)
221+
222+
rule_runner.write_files(
223+
{
224+
"test/BUILD": "docker_image()",
225+
"test/Dockerfile": "\n".join(
226+
dedent(
227+
f"""\
228+
ARG {arg}="{tag}"
229+
FROM ${arg}
230+
"""
231+
)
232+
for arg, tag in base_image_tags.items()
233+
)
234+
+ dedent(
235+
"""\
236+
ARG DECOY="this is not a target address"
237+
FROM $DECOY
238+
"""
239+
),
240+
}
241+
)
242+
addr = Address("test")
243+
info = rule_runner.request(DockerfileInfo, [DockerfileInfoRequest(addr)])
244+
assert info.from_image_build_args.to_dict() == base_image_tags

src/python/pants/backend/docker/subsystems/dockerfile_wrapper_script.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,22 @@ class ParsedDockerfileInfo:
2828

2929
_address_regexp = re.compile(
3030
r"""
31-
(?://)?[^:# ]*:[^:#!@?/\= ]+(?:\#[^:#!@?= ]+)?$
31+
# Optionally root:ed.
32+
(?://)?
33+
# Optional path.
34+
[^:# ]*
35+
# Optional target name.
36+
(?::[^:#!@?/\= ]+)?
37+
# Optional generated name.
38+
(?:\#[^:#!@?= ]+)?
39+
# Optional parametrizations.
40+
(?:@
41+
# key=value
42+
[^=: ]+=[^,: ]*
43+
# Optional additional `,key=value`s
44+
(?:,[^=: ]+=[^,: ]*)*
45+
)?
46+
$
3247
""",
3348
re.VERBOSE,
3449
)

0 commit comments

Comments
 (0)