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 go_module and go_external_module targets #12377

Merged
merged 6 commits into from
Jul 20, 2021

Conversation

tdyas
Copy link
Contributor

@tdyas tdyas commented Jul 20, 2021

  • Add go_module target which represents a first-party Go module.
  • Add go_external_module target which is a third-party Go module.
  • Add a rule to infer a dependency between a go_package and the nearest sibling or ancestor go_module.

Note: This PR does not include a dependency inference rule to infer the external modules listed in a go.mod file as dependencies of the applicable go_module. That will be done in a follow-up.

[ci skip-rust]

[ci skip-build-wheels]

[ci skip-rust]

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

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

tdyas commented Jul 20, 2021

diff --git a/src/python/pants/backend/go/target_type_rules.py b/src/python/pants/backend/go/target_type_rules.py
index f22448740..9ba4b7a05 100644
--- a/src/python/pants/backend/go/target_type_rules.py
+++ b/src/python/pants/backend/go/target_type_rules.py
@@ -1,6 +1,6 @@
 # Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
 # Licensed under the Apache License, Version 2.0 (see LICENSE).
-from pants.backend.go.target_types import GoModuleGoVersion, GoPackageDependencies
+from pants.backend.go.target_types import GoModuleSources, GoPackageDependencies
 from pants.base.specs import AddressSpecs, AscendantAddresses, SiblingAddresses
 from pants.build_graph.address import Address
 from pants.engine.internals.selectors import Get
@@ -25,12 +25,13 @@ async def inject_go_module_dependency(request: InjectGoModuleDependency) -> Inje
     candidate_targets = await Get(
         Targets, AddressSpecs([AscendantAddresses(spec_path), SiblingAddresses(spec_path)])
     )
+    print(f"candidate_targets={candidate_targets}")
     go_module_targets = [
-        (tgt.address.spec_path, tgt)
-        for tgt in candidate_targets
-        if tgt.has_field(GoModuleGoVersion)
+        (tgt.address.spec_path, tgt) for tgt in candidate_targets if tgt.has_field(GoModuleSources) and not tgt.address.is_file_target
     ]
+    print(f"go_module_targets={go_module_targets}")
     sorted_go_module_targets = sorted(go_module_targets, key=lambda x: x[0], reverse=True)
+    print(f"sorted_go_module_targets={sorted_go_module_targets}")
     if sorted_go_module_targets:
         nearest_go_module_target = sorted_go_module_targets[0][1]
         return InjectedDependencies([nearest_go_module_target.address])
diff --git a/src/python/pants/backend/go/target_type_rules_test.py b/src/python/pants/backend/go/target_type_rules_test.py
index 9b2a03e21..265da7396 100644
--- a/src/python/pants/backend/go/target_type_rules_test.py
+++ b/src/python/pants/backend/go/target_type_rules_test.py
@@ -3,7 +3,7 @@
 import pytest
 
 from pants.backend.go import target_type_rules
-from pants.backend.go.target_types import GoModule, GoModuleGoVersion, GoPackage
+from pants.backend.go.target_types import GoModule, GoModuleSources, GoPackage
 from pants.build_graph.address import Address
 from pants.engine.addresses import Addresses
 from pants.engine.rules import QueryRule
@@ -25,7 +25,7 @@ def rule_runner() -> RuleRunner:
 def assert_go_module_address(rule_runner: RuleRunner, target: Target, expected_address: Address):
     addresses = rule_runner.request(Addresses, [DependenciesRequest(target[Dependencies])])
     targets = rule_runner.request(Targets, [addresses])
-    go_module_targets = [tgt for tgt in targets if tgt.has_field(GoModuleGoVersion)]
+    go_module_targets = [tgt for tgt in targets if tgt.has_field(GoModuleSources)]
     assert len(go_module_targets) == 1
     assert go_module_targets[0].address == expected_address
 
diff --git a/src/python/pants/backend/go/target_types.py b/src/python/pants/backend/go/target_types.py
index df16b9f6f..f94152979 100644
--- a/src/python/pants/backend/go/target_types.py
+++ b/src/python/pants/backend/go/target_types.py
@@ -31,33 +31,8 @@ class GoPackage(Target):
 # `go_module` target
 
 
-class GoModuleGoVersion(StringField):
-    alias = "go_version"
-    # TODO: Set default to match the active `GoLangDisribution`.
-    default = "1.16"
-
-
-# class GoModuleGoModSource(Sources):
-#     alias = "gomod"
-#     default = ["go.mod"]
-#     # TODO: This does not handle a missing go.mod file.
-#     expected_num_files = 1
-#
-#
-# class GoModuleGoSumSource(Sources):
-#     alias = "gosum"
-#     default = ["go.sum"]
-#     # TODO: This does not handle a missing go.sum file.
-#     expected_num_files = 1
-#     help = "The source file containing the go.sum file."
-#
-#
-# class GoModuleImportPathReplacements(StringSequenceField):
-#     alias = "replacements"
-#     help = (
-#         "Import path replacements where each item is in the form ORIGINAL,REPLACEMENT. "
-#         "This will be inferred from the go.mod file."
-#     )
+class GoModuleSources(Sources):
+    default = ("go.mod", "go.sum")
 
 
 class GoModule(Target):
@@ -65,10 +40,7 @@ class GoModule(Target):
     core_fields = (
         *COMMON_TARGET_FIELDS,
         Dependencies,
-        GoModuleGoVersion,
-        GoImportPath,
-        # GoModuleGoModSource,
-        # GoModuleGoSumSource,
+        GoModuleSources,
     )
 

I tried the above diff to add a Sources field to go_module but now I get a GoModule target per go.mod and go.sum file instead of a single GoModule target. How do I get a single GoModule target again?

[ci skip-rust]

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

tdyas commented Jul 20, 2021

I switched to requesting UnexpandedTargets in the rule. This seemed to prevent the file target expansion.

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!

for tgt in candidate_targets
if tgt.has_field(GoModuleSources) and not tgt.address.is_file_target
]
sorted_go_module_targets = sorted(go_module_targets, key=lambda x: x[0], reverse=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Simpler is probably to not store a tuple in the above collection, and sort by lambda tgt: tgt.address.spec_path. That should simplify a couple lines in this rule

)


# `go_binary` target
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using this style:

# -----------------------------------------------------------------------------------------------
# Address -> Target(s)
# -----------------------------------------------------------------------------------------------

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no rules in this file. This comment style doesn't make sense for just the target type definitions. It would make sense for the files with rules in them.

Copy link
Contributor

Choose a reason for hiding this comment

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

We use it for python/target_types.py too:

# -----------------------------------------------------------------------------------------------
# `pex_binary` target
# -----------------------------------------------------------------------------------------------

The general idea is to have section headers that more clearly delineate the file, sort of like chapters in a book.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I had thought you were referring to the Foo -> Bar notation and not the dashes. Yeah the dashes would be relevant to the comments here.

Tom Dyas added 3 commits July 20, 2021 14:19
[ci skip-rust]

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

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

[ci skip-build-wheels]
@tdyas tdyas merged commit a1dc162 into pantsbuild:main Jul 20, 2021
@tdyas tdyas deleted the golang_go_module_target branch July 20, 2021 19:18
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.

2 participants