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

go: add tailor support for go_package, go_module, and go_external_module targets #12406

Merged
merged 6 commits into from
Jul 26, 2021

Conversation

tdyas
Copy link
Contributor

@tdyas tdyas commented Jul 22, 2021

This PR adds support for ./pants tailor to the Go plugin:

  • Infer go_package targets from the existence of *.go files.
  • Infer go_module targets from the presence of go.mod files.
  • Infer go_external_module targets based on resolving existing go_module targets for their module dependencies. These are written into a separate BUILD.godeps file so it is easy to delete just the generated go_external_module targets in order to re-run tailor to re-generate.

Note: The inference for go_external_module targets is necessary because Pants does not currently support dynamic target injection from engine rules and we want to implement dependency inference between go_package and go_external_module targets. Since plugins cannot dynamically create go_external_module targets, the short term solution is to require users to run ./pants tailor to create the appropriate go_external_module targets. A future PR will implement dependency inference between these targets. This PR does not try to solve what we would want for this use case in the long term.

src/python/pants/base/specs.py Outdated Show resolved Hide resolved
tdyas pushed a commit that referenced this pull request Jul 22, 2021
Introduce the `MaybeEmptySiblingAddresses` address spec to allow searching for addresses in a directory without a BUILD file or for which there are no targets in any of the BUILD files. It is similar in spirit to the already-existing `MaybeEmptyDescendantAddresses`.

This PR is necessary for the Go tailor support in #12406. The inference for `go_external_module` attempts to find `go_module` targets along the tailor `search_paths`, but cannot use the `SiblingAddresses` address spec because directories at the top of the tree that may not have a BUILD file will cause `SiblingAddresses` to raise an exception.

`SiblingAddresses` will now fail if it does not find any matching targets.

[ci skip-rust]

[ci skip-build-wheels]
tdyas pushed a commit that referenced this pull request Jul 26, 2021
## Problem

`./pants tailor` currently assumes that all targets on which it operates will have a `sources` field. As part of the Go plugin's tailor support in #12406, I would like `tailor` to infer `go_external_module` targets which do not have a `sources` field from the dependencies of `go_module` targets.

Attempting to add a `GoExternalModule` target as a `PutativeTarget` caused invocation of `tailor` to raise an exception in `default_sources_for_target_type` because that function could not find a `sources` field.

## Solution 

Work-around the issue by teaching `tailor` to ignore the lack of a `sources` field if there are no owned sources or triggering sources for the `PutativeTarget`. The exception is now raised in `PutativeTarget.for_target_type` if there are any such sources. `default_sources_for_target_type` just returns an empty tuple if it could not find a `sources` field.

This is just a work-around and is not intended as a longer term solution for `tailor` to support target types without `sources`.

[ci skip-rust]

[ci skip-build-wheels]
Tom Dyas added 2 commits July 26, 2021 10:00
[ci skip-rust]

[ci skip-build-wheels]
[ci skip-rust]

[ci skip-build-wheels]
@tdyas
Copy link
Contributor Author

tdyas commented Jul 26, 2021

I rebased the PR now that both blocking PRs have landed on main.

@tdyas
Copy link
Contributor Author

tdyas commented Jul 26, 2021

ping on review

Tom Dyas added 3 commits July 26, 2021 13:54
[ci skip-rust]

[ci skip-build-wheels]
[ci skip-rust]

[ci skip-build-wheels]
[ci skip-rust]

[ci skip-build-wheels]
src/python/pants/backend/go/tailor.py Outdated Show resolved Hide resolved
src/python/pants/backend/go/tailor.py Show resolved Hide resolved
[ci skip-rust]

[ci skip-build-wheels]
Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thanks!

@benjyw
Copy link
Contributor

benjyw commented Jul 26, 2021

Taking a look now, sorry for the delay.

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.

Looks good! I am slightly confused about the sources field on go_package. I thought we were moving towards that target not having a sources field but always owning the entire directory, since that is more idiomatic in Go? Is that future work? It will affect all this.

@tdyas
Copy link
Contributor Author

tdyas commented Jul 26, 2021

Looks good! I am slightly confused about the sources field on go_package. I thought we were moving towards that target not having a sources field but always owning the entire directory, since that is more idiomatic in Go? Is that future work? It will affect all this.

Yes, that change is coming in a future PR.

@tdyas tdyas merged commit b4febf1 into pantsbuild:main Jul 26, 2021
@tdyas tdyas deleted the golang_tailor_support branch July 26, 2021 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants