Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker: fix base image dependency inference for parametrized targets. #20633

Merged
merged 1 commit into from
Mar 8, 2024

Conversation

kaos
Copy link
Member

@kaos kaos commented Mar 1, 2024

Fixes #20632

@kaos kaos added needs-cherrypick category:bugfix Bug fixes for released features labels Mar 1, 2024
@kaos kaos added this to the 2.19.x milestone Mar 1, 2024
@kaos kaos requested a review from huonw March 1, 2024 10:49
@kaos
Copy link
Member Author

kaos commented Mar 1, 2024

I marked it for picking to 2.19.x, as it's a low risk fix, but we can close that or hold it for later if we don't want it going into 2.19.1..

Copy link
Contributor

@huonw huonw 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.

hold it for later if we don't want it going into 2.19.1.

This sounds good 👍

# Optional path.
[^:# ]*
# Optional target name.
(?::[^:#!@?/\= ]+)?
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this was previously required, but is now optional. Is that an intentional change?

Copy link
Member Author

@kaos kaos Mar 4, 2024

Choose a reason for hiding this comment

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

yes, I looked at

rule address() -> AddressInput<'input>
= path:path() target:target()? generated:generated()? parameters:parameters()? {
when re-modeling this to cover an actual valid address as close as possible. (perhaps being slightly more conservative with special characters in the various parts..)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, okay. Given everything is now optional, I wonder if this means dependency inference will now be looking at ~every bit of text in a docker file, e.g. random text like abc matches the "optional path" bit (and, even an empty string now satisfies this regex, I think).

The change to allow inferring without a target name seems somewhat of separate to supporting parametrised targets, and is potentially more "dangerous"? For instance, being more likely to change user-observed behaviour by inferring spurious dependencies or performance issues.

Some options I can think of:

  1. make the change here and cherrypick it all, as originally planned
  2. make the change here but avoid cherrypicking
  3. do the two changes separately, and cherry pick the support for parameters but not the optional-name one
  4. something else?

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

Astute observations, as always :)

I think, trying to recollect the original train of thought as best I can, could've been to be a little conservative with what values to pick up as potential addresses to infer as dependencies to, as back then the "try this value and see if it resolves to something" was a little ugly. Now when it's explicitly OK to not fail for invalid values (not an actual existing address) but merely doesn't resolve to an address, I don't see any reason to not try every value that could remotely resemble a valid address.

The thing is, the values we do try to use are from a very specific narrow part of the whole Dockerfile, namely default build arg values that is also used in the FROM instruction. So, any build arg that isn't used in the FROM instruction, won't be considered. Any build arg that doesn't have a default value.. same. etc. So, given that I don't think this is going to hit performance noticeably, and we shouldn't get any false hits either. (I could make a case up where this is not true, but I don't think we'll see that in practice.)

So I'm leaning towards option 1. How does that sound?

Copy link
Contributor

Choose a reason for hiding this comment

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

The thing is, the values we do try to use are from a very specific narrow part of the whole Dockerfile, namely default build arg values that is also used in the FROM instruction. So, any build arg that isn't used in the FROM instruction, won't be considered.

Ah okay, cool. To confirm, does it also apply to FROM //some/path/to:target?

Copy link
Member Author

Choose a reason for hiding this comment

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

The thing is, the values we do try to use are from a very specific narrow part of the whole Dockerfile, namely default build arg values that is also used in the FROM instruction. So, any build arg that isn't used in the FROM instruction, won't be considered.

Ah okay, cool. To confirm, does it also apply to FROM //some/path/to:target?

No, it does not, as Pants doesn't patch/edit the Dockerfile, so there would not be a way to replace that value with the actual image name.

Copy link
Contributor

@huonw huonw left a comment

Choose a reason for hiding this comment

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

Looks good.

# Optional path.
[^:# ]*
# Optional target name.
(?::[^:#!@?/\= ]+)?
Copy link
Contributor

Choose a reason for hiding this comment

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

The thing is, the values we do try to use are from a very specific narrow part of the whole Dockerfile, namely default build arg values that is also used in the FROM instruction. So, any build arg that isn't used in the FROM instruction, won't be considered.

Ah okay, cool. To confirm, does it also apply to FROM //some/path/to:target?

@kaos kaos merged commit b8b3c72 into main Mar 8, 2024
24 checks passed
@kaos kaos deleted the kaos/docker-from-dep-inference-20632 branch March 8, 2024 08:48
@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

✔️ 2.19.x

Successfully opened #20658.

✔️ 2.20.x

Successfully opened #20657.


Thanks again for your contributions!

🤖 Beep Boop here's my run link

kaos added a commit that referenced this pull request Mar 8, 2024
kaos added a commit that referenced this pull request Mar 8, 2024
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: base image build arg detection does not support parametrized targets
3 participants