Skip to content

Conversation

ademariag
Copy link

@ademariag ademariag commented Sep 1, 2025

Summary

  • Fix docker_build_args with Target References not working #22602
  • Fixed Docker image target references not being resolved when passed via extra_build_args in BUILD files
  • Modified DockerBuildContext.create() to consistently use merged build args (including extra_build_args) for both interpolation context and Dockerfile stage parsing
  • Added comprehensive test coverage for scenarios where extra_build_args override default ARG values in Dockerfiles

Problem

Previously, when using target references like :base-image in extra_build_args, Pants failed to resolve these to actual Docker image tags during build context creation. This prevented users from creating hierarchical Docker image dependencies and dynamic base image selection.

Solution

The fix ensures that extra_build_args from BUILD files are properly merged and made available during all phases of build context creation, particularly when parsing Dockerfile stages that contain FROM instructions with variable substitution.

Test Coverage

Added test_from_image_build_arg_with_extra_build_args() which validates:

  • Default build args can be overridden via extra_build_args
  • Target references in extra_build_args resolve to proper image tags
  • Multiple downstream images can reference different base images via build arg overrides

@tdyas tdyas requested a review from benjyw September 2, 2025 18:24
@tdyas tdyas added the category:bugfix Bug fixes for released features label Sep 2, 2025
@benjyw
Copy link
Contributor

benjyw commented Sep 5, 2025

I haven't forgotten about this, will try and get to it this weekend.

@benjyw
Copy link
Contributor

benjyw commented Sep 5, 2025

Meanwhile the CI test failures look meaningful.

@benjyw benjyw requested a review from kaos September 9, 2025 19:04
Copy link
Contributor

@benjyw benjyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

Having paged some of this backend into my brain, this seems right. But see my question, which will help me understand this better.

build_env: DockerBuildEnvironment,
upstream_image_ids: Iterable[str],
dockerfile_info: DockerfileInfo,
should_suggest_renames: bool = True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been removed? The underlying option now doesn't do anything, looks like.

# Override default value type for the `build_args` context to get helpful error messages.
interpolation_context["build_args"] = DockerBuildArgsInterpolationValue(
interpolation_context.get("build_args", {})
merged_build_args_with_extensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is quite subtle, so help me understand something...

This seems to be the critical line, where it's important for some reason that merged_build_args_with_extensions is not converted to a DockerBuildArgsInterpolationValue. Why does this fix the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category:bugfix Bug fixes for released features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker_build_args with Target References not working

3 participants