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

Build scripts can distinguish between solve nodes for different targets. #3531

Merged
merged 5 commits into from
Oct 8, 2024

Conversation

mitchell-as
Copy link
Contributor

@mitchell-as mitchell-as commented Oct 7, 2024

TaskDX-2960 We can interpret a buildscript where the solve node is not top level

Clients need to pass the target when querying requirements, platforms, etc. Use "" for the default target (i.e. the name assigned to 'main').

Clients need to pass the target when querying requirements, platforms, etc.
Use "" for the default target (i.e. the name assigned to 'main').
…mat.

Previously, build scripts with multiple targets only had their default target's requirements transformed.

if f := a.Value.FuncCall; target == "" && f != nil && isSolveFuncName(f.Name) {
// This is coming from a complex build expression with no straightforward way to determine
// a default target. Fall back on a top-level solve node.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mitchell-as mitchell-as marked this pull request as ready for review October 8, 2024 15:46
Comment on lines 215 to 229
// Otherwise, the "src" key contains a reference to the solve node. Look over the build expression
// again for that referenced node.
for _, arg := range node.FuncCall.Arguments {
if arg.Assignment == nil {
continue
}
a := arg.Assignment
if a.Key == srcKey && a.Value.Ident != nil {
node, err := b.getSolveNode(*a.Value.Ident)
if err != nil {
return nil, errs.Wrap(err, "Could not get solve node from target")
}
return node, nil
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Why can't this solve node be identified by the getTargetNode above? Is it because the target value wasn't found? Can you please add a more detailed comment.

Copy link
Contributor Author

@mitchell-as mitchell-as Oct 8, 2024

Choose a reason for hiding this comment

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

Consider this build script snippet:

runtime = state_tool_artifacts_v1(src = sources)
sources = solve(...)
main = runtime

getTargetNode() would return sources (main -> runtime -> src -> sources). Since sources is not a solve node (it's a reference), we have to look again for the sources node. The second time, getTargetNode("sources") will give us the solve node we need.

I've added a brief comment in the code.

Comment on lines +55 to +61
seen := make(map[string]bool)
for _, assignment := range raw.Assignments {
if _, exists := seen[assignment.Key]; exists {
return nil, locale.NewInputError(locale.Tl("err_buildscript_duplicate_keys", "Build script has duplicate '{{.V0}}' assignments", assignment.Key))
}
seen[assignment.Key] = true
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this still allow multiple solve nodes for different targets? The ACs in the story don't stipulate that it has to be multiple solve nodes for a single target that raise an error. Maybe the story needs to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you can have more than one solve node in a buildscript. There's only an issue if you have a buildscript whose target has two different solve nodes. For example:

runtime = state_tool_artifacts_v1(src = sources)
sources = solve(...)
sources = solve(... something different...)
another_solve = solve(...)

This error would identify sources has having duplicate assignments. another_solve is totally fine, as it's a different target or whatever.

return name == solveFuncName || name == solveLegacyFuncName
}

func (b *BuildScript) getTargetNode(target string) (*Value, error) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: Any taste for a variadic argument here? Would allow us to forgo the passing of "" and be ready for multiple targets when the time comes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I was looking for an excuse to make this an optional argument, and multiple target support justifies it!

MDrakos
MDrakos previously approved these changes Oct 8, 2024
Copy link
Member

@MDrakos MDrakos 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 clarifications!

return nil, errNodeNotFound
}

func (b *BuildScript) getSolveNode(target ...string) (*Value, error) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: This should probably be targets

Copy link
Member

@MDrakos MDrakos left a comment

Choose a reason for hiding this comment

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

Thank you for dealing with my nits 😆

@mitchell-as mitchell-as merged commit cc5977c into version/0-47-0-RC1 Oct 8, 2024
8 checks passed
@mitchell-as mitchell-as deleted the mitchell/dx-2960 branch October 8, 2024 20:13
Naatan added a commit that referenced this pull request Oct 11, 2024
Naatan added a commit that referenced this pull request Oct 11, 2024
Revert "Merge branch version/0-47-0-RC1 to adopt changes from PR #3531"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants