Skip to content

Commit

Permalink
[internal] Refactor go_mod.py (#13039)
Browse files Browse the repository at this point in the history
* Rename `GoModule` -> `GoModTarget` to make clear it's a target.
* Rename `ResolvedGoModule` -> `GoModInfo`
* Rename `ResolvedOwningGoModule` -> `OwningGoMod`
* Validate `sources` are correct for `go_module` target
* Don't store `Target` and `minimum_go_version` on `GoModInfo` because they're not needed.
* Centralize all `Digest` setup for the `go.mod` to `GoModInfo`, rather than callers handling it.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Sep 30, 2021
1 parent 5ff76bf commit 761533e
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 180 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/backend/experimental/go/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pants.backend.go.lint.gofmt import skip_field as gofmt_skip_field
from pants.backend.go.lint.gofmt.rules import rules as gofmt_rules
from pants.backend.go.subsystems import golang
from pants.backend.go.target_types import GoBinary, GoExternalPackageTarget, GoModule, GoPackage
from pants.backend.go.target_types import GoBinary, GoExternalPackageTarget, GoModTarget, GoPackage
from pants.backend.go.util_rules import (
assembly,
build_go_pkg,
Expand All @@ -23,7 +23,7 @@


def target_types():
return [GoBinary, GoPackage, GoModule, GoExternalPackageTarget]
return [GoBinary, GoPackage, GoModTarget, GoExternalPackageTarget]


def rules():
Expand Down
27 changes: 14 additions & 13 deletions src/python/pants/backend/go/goals/custom_goals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

import logging

from pants.backend.go.target_types import GoModuleSources
from pants.backend.go.target_types import GoModSourcesField
from pants.backend.go.util_rules.build_go_pkg import BuildGoPackageRequest, BuiltGoPackage
from pants.backend.go.util_rules.external_module import ResolveExternalGoPackageRequest
from pants.backend.go.util_rules.go_mod import ResolvedGoModule, ResolveGoModuleRequest
from pants.backend.go.util_rules.go_mod import GoModInfo, GoModInfoRequest
from pants.backend.go.util_rules.go_pkg import (
ResolvedGoPackage,
ResolveGoPackageRequest,
is_first_party_package_target,
is_third_party_package_target,
)
from pants.engine.console import Console
from pants.engine.fs import Workspace
from pants.engine.fs import MergeDigests, Snapshot, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.internals.selectors import Get, MultiGet
from pants.engine.rules import collect_rules, goal_rule
Expand All @@ -38,16 +38,17 @@ class GoResolveGoal(Goal):

@goal_rule
async def run_go_resolve(targets: UnexpandedTargets, workspace: Workspace) -> GoResolveGoal:
# TODO: Use MultiGet to resolve the go_module targets.
# TODO: Combine all of the go.sum's into a single Digest to write.
for target in targets:
if target.has_field(GoModuleSources):
resolved_go_module = await Get(ResolvedGoModule, ResolveGoModuleRequest(target.address))
# TODO: Only update the files if they actually changed.
workspace.write_digest(resolved_go_module.digest, path_prefix=target.address.spec_path)
logger.info(f"{target.address}: Updated go.mod and go.sum.\n")
else:
logger.info(f"{target.address}: Skipping because target is not a `go_module`.\n")
all_go_mod_info = await MultiGet(
Get(GoModInfo, GoModInfoRequest(target.address))
for target in targets
if target.has_field(GoModSourcesField)
)
result = await Get(
Snapshot, MergeDigests(go_mod_info.digest for go_mod_info in all_go_mod_info)
)
logger.info(f"Updating these files: {list(result.files)}")
# TODO: Only update the files if they actually changed.
workspace.write_digest(result.digest)
return GoResolveGoal(exit_code=0)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pants.backend.go import target_type_rules
from pants.backend.go.goals import package_binary
from pants.backend.go.goals.package_binary import GoBinaryFieldSet
from pants.backend.go.target_types import GoBinary, GoModule, GoPackage
from pants.backend.go.target_types import GoBinary, GoModTarget, GoPackage
from pants.backend.go.util_rules import (
assembly,
build_go_pkg,
Expand All @@ -33,7 +33,7 @@
@pytest.fixture()
def rule_runner() -> RuleRunner:
rule_runner = RuleRunner(
target_types=[GoBinary, GoPackage, GoModule],
target_types=[GoBinary, GoPackage, GoModTarget],
rules=[
*assembly.rules(),
*compile.rules(),
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/go/goals/tailor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from dataclasses import dataclass

from pants.backend.go.target_types import GoModule, GoPackage
from pants.backend.go.target_types import GoModTarget, GoPackage
from pants.core.goals.tailor import (
AllOwnedSources,
PutativeTarget,
Expand Down Expand Up @@ -61,7 +61,7 @@ async def find_putative_go_module_targets(
for dirname, filenames in group_by_dir(unowned_go_mod_files).items():
putative_targets.append(
PutativeTarget.for_target_type(
GoModule,
GoModTarget,
dirname,
os.path.basename(dirname),
sorted(filenames),
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/backend/go/goals/tailor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PutativeGoPackageTargetsRequest,
)
from pants.backend.go.goals.tailor import rules as go_tailor_rules
from pants.backend.go.target_types import GoModule, GoPackage
from pants.backend.go.target_types import GoModTarget, GoPackage
from pants.backend.go.util_rules import external_module, go_mod, sdk
from pants.core.goals.tailor import (
AllOwnedSources,
Expand Down Expand Up @@ -38,7 +38,7 @@ def rule_runner() -> RuleRunner:
QueryRule(PutativeTargets, [PutativeGoPackageTargetsRequest, AllOwnedSources]),
QueryRule(PutativeTargets, [PutativeGoModuleTargetsRequest, AllOwnedSources]),
],
target_types=[GoPackage, GoModule],
target_types=[GoPackage, GoModTarget],
)
rule_runner.set_options(["--backend-packages=pants.backend.experimental.go"])
return rule_runner
Expand Down Expand Up @@ -80,5 +80,5 @@ def test_find_putative_go_module_targets(rule_runner: RuleRunner) -> None:
],
)
assert putative_targets == PutativeTargets(
[PutativeTarget.for_target_type(GoModule, "src/go/unowned", "unowned", ["go.mod"])]
[PutativeTarget.for_target_type(GoModTarget, "src/go/unowned", "unowned", ["go.mod"])]
)
32 changes: 16 additions & 16 deletions src/python/pants/backend/go/target_type_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
GoExternalPackageImportPathField,
GoExternalPackageTarget,
GoImportPath,
GoModule,
GoModTarget,
GoPackageDependencies,
GoPackageSources,
)
Expand All @@ -25,10 +25,10 @@
ResolveExternalGoPackageRequest,
)
from pants.backend.go.util_rules.go_mod import (
FindNearestGoModuleRequest,
ResolvedGoModule,
ResolvedOwningGoModule,
ResolveGoModuleRequest,
GoModInfo,
GoModInfoRequest,
OwningGoMod,
OwningGoModRequest,
)
from pants.backend.go.util_rules.go_pkg import ResolvedGoPackage, ResolveGoPackageRequest
from pants.backend.go.util_rules.import_analysis import GoStdLibImports
Expand Down Expand Up @@ -66,14 +66,14 @@ class InjectGoPackageDependenciesRequest(InjectDependenciesRequest):
async def inject_go_package_dependencies(
request: InjectGoPackageDependenciesRequest,
) -> InjectedDependencies:
owning_go_module_result = await Get(
ResolvedOwningGoModule,
FindNearestGoModuleRequest(request.dependencies_field.address.spec_path),
owning_go_mod = await Get(
OwningGoMod, OwningGoModRequest(request.dependencies_field.address.spec_path)
)
return (
InjectedDependencies([owning_go_mod.address])
if owning_go_mod.address
else InjectedDependencies()
)
if owning_go_module_result.module_address:
return InjectedDependencies([owning_go_module_result.module_address])
else:
return InjectedDependencies()


# TODO: Figure out how to merge (or not) this with ResolvedImportPaths as a base class.
Expand Down Expand Up @@ -232,25 +232,25 @@ async def inject_go_external_package_dependencies(


class GenerateGoExternalPackageTargetsRequest(GenerateTargetsRequest):
generate_from = GoModule
generate_from = GoModTarget


@rule(desc="Generate targets for each external package in `go.mod`", level=LogLevel.DEBUG)
async def generate_go_external_package_targets(
request: GenerateGoExternalPackageTargetsRequest,
) -> GeneratedTargets:
generator_addr = request.generator.address
resolved_module = await Get(ResolvedGoModule, ResolveGoModuleRequest(generator_addr))
go_mod_info = await Get(GoModInfo, GoModInfoRequest(generator_addr))
all_resolved_packages = await MultiGet(
Get(
ResolveExternalGoModuleToPackagesResult,
ResolveExternalGoModuleToPackagesRequest(
path=module_descriptor.path,
version=module_descriptor.version,
go_sum_digest=resolved_module.digest,
go_sum_digest=go_mod_info.go_sum_stripped_digest,
),
)
for module_descriptor in resolved_module.modules
for module_descriptor in go_mod_info.modules
)

def create_tgt(pkg: ResolvedGoPackage) -> GoExternalPackageTarget:
Expand Down
8 changes: 4 additions & 4 deletions src/python/pants/backend/go/target_type_rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
GoExternalModuleVersionField,
GoExternalPackageImportPathField,
GoExternalPackageTarget,
GoModule,
GoModuleSources,
GoModSourcesField,
GoModTarget,
GoPackage,
GoPackageSources,
)
Expand Down Expand Up @@ -52,7 +52,7 @@ def rule_runner() -> RuleRunner:
QueryRule(InferredDependencies, [InferGoPackageDependenciesRequest]),
QueryRule(GeneratedTargets, [GenerateGoExternalPackageTargetsRequest]),
],
target_types=[GoPackage, GoModule, GoExternalPackageTarget],
target_types=[GoPackage, GoModTarget, GoExternalPackageTarget],
)
rule_runner.set_options([], env_inherit={"PATH"})
return rule_runner
Expand All @@ -61,7 +61,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(GoModuleSources)]
go_module_targets = [tgt for tgt in targets if tgt.has_field(GoModSourcesField)]
assert len(go_module_targets) == 1
assert go_module_targets[0].address == expected_address

Expand Down
39 changes: 31 additions & 8 deletions src/python/pants/backend/go/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,46 @@ class GoPackage(Target):
# -----------------------------------------------------------------------------------------------


class GoModuleSources(Sources):
class GoModSourcesField(Sources):
alias = "_sources"
default = ("go.mod", "go.sum")
expected_num_files = range(1, 3)
expected_num_files = range(1, 3) # i.e. 1 or 2.

def validate_resolved_files(self, files: Sequence[str]) -> None:
super().validate_resolved_files(files)
if "go.mod" not in [os.path.basename(f) for f in files]:
raise InvalidFieldException(f"""No go.mod file was found for target {self.address}.""")
@property
def go_mod_path(self) -> str:
return os.path.join(self.address.spec_path, "go.mod")

@property
def go_sum_path(self) -> str:
return os.path.join(self.address.spec_path, "go.sum")

class GoModule(Target):
def validate_resolved_files(self, files: Sequence[str]) -> None:
super().validate_resolved_files(files)
if self.go_mod_path not in files:
raise InvalidFieldException(
f"The {repr(self.alias)} field in target {self.address} must include "
f"{self.go_mod_path}, but only had: {list(files)}\n\n"
f"Make sure that you're declaring the `{GoModTarget.alias}` target in the same "
"directory as your `go.mod` file."
)
invalid_files = set(files) - {self.go_mod_path, self.go_sum_path}
if invalid_files:
raise InvalidFieldException(
f"The {repr(self.alias)} field in target {self.address} must only include "
f"`{self.go_mod_path}` and optionally {self.go_sum_path}, but had: "
f"{sorted(invalid_files)}\n\n"
f"Make sure that you're declaring the `{GoModTarget.alias}` target in the same "
f"directory as your `go.mod` file and that you don't override the `{self.alias}` "
"field."
)


class GoModTarget(Target):
alias = "go_module"
core_fields = (
*COMMON_TARGET_FIELDS,
Dependencies,
GoModuleSources,
GoModSourcesField,
)
help = "First-party Go module."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from pants.backend.go import target_type_rules
from pants.backend.go.target_types import GoExternalPackageTarget, GoModule, GoPackage
from pants.backend.go.target_types import GoExternalPackageTarget, GoModTarget, GoPackage
from pants.backend.go.util_rules import (
assembly,
build_go_pkg,
Expand Down Expand Up @@ -40,7 +40,7 @@ def rule_runner() -> RuleRunner:
*target_type_rules.rules(),
QueryRule(BuiltGoPackage, [BuildGoPackageRequest]),
],
target_types=[GoPackage, GoModule, GoExternalPackageTarget],
target_types=[GoPackage, GoModTarget, GoExternalPackageTarget],
)
rule_runner.set_options([], env_inherit={"PATH"})
return rule_runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from pants.backend.go.target_types import GoModule, GoPackage
from pants.backend.go.target_types import GoModTarget, GoPackage
from pants.backend.go.util_rules import compile, sdk
from pants.backend.go.util_rules.compile import CompiledGoSources, CompileGoSourcesRequest
from pants.engine.fs import CreateDigest, Digest, FileContent, Snapshot
Expand Down Expand Up @@ -36,7 +36,7 @@ def rule_runner() -> RuleRunner:
QueryRule(Snapshot, [Digest]),
QueryRule(CompiledGoSources, [CompileGoSourcesRequest]),
],
target_types=[GoPackage, GoModule],
target_types=[GoPackage, GoModTarget],
)
rule_runner.set_options([], env_inherit={"PATH"})
return rule_runner
Expand Down
5 changes: 1 addition & 4 deletions src/python/pants/backend/go/util_rules/external_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ async def resolve_external_module_to_go_packages(
left_go_sum_contents = fc.content
break

go_sum_only_digest = await Get(
Digest, DigestSubset(request.go_sum_digest, PathGlobs(["go.sum"]))
)
go_sum_prefixed_digest = await Get(Digest, AddPrefix(go_sum_only_digest, "__sources__"))
go_sum_prefixed_digest = await Get(Digest, AddPrefix(request.go_sum_digest, "__sources__"))
right_digest_contents = await Get(DigestContents, Digest, go_sum_prefixed_digest)
right_go_sum_contents = b""
for fc in right_digest_contents:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from pants.backend.go.target_types import GoModule, GoPackage
from pants.backend.go.target_types import GoModTarget, GoPackage
from pants.backend.go.util_rules import external_module, sdk
from pants.backend.go.util_rules.external_module import (
DownloadedExternalModule,
Expand Down Expand Up @@ -33,7 +33,7 @@ def rule_runner() -> RuleRunner:
),
QueryRule(DigestContents, [Digest]),
],
target_types=[GoPackage, GoModule],
target_types=[GoPackage, GoModTarget],
)
rule_runner.set_options([], env_inherit={"PATH"})
return rule_runner
Expand Down
Loading

0 comments on commit 761533e

Please sign in to comment.