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

[internal] Refactor go/util_rules/external_module.py #13051

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/python/pants/backend/go/goals/custom_goals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging

from pants.backend.go.target_types import GoModSourcesField
from pants.backend.go.target_types import GoExternalPackageTarget, 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 GoModInfo, GoModInfoRequest
Expand Down Expand Up @@ -82,16 +82,16 @@ class GoPkgDebugGoal(Goal):

@goal_rule
async def run_go_pkg_debug(targets: UnexpandedTargets, console: Console) -> GoPkgDebugGoal:
first_party_package_targets = [tgt for tgt in targets if is_first_party_package_target(tgt)]
first_party_requests = [
Get(ResolvedGoPackage, ResolveGoPackageRequest(address=tgt.address))
for tgt in first_party_package_targets
for tgt in targets
if is_first_party_package_target(tgt)
]

third_party_package_targets = [tgt for tgt in targets if is_third_party_package_target(tgt)]
third_party_requests = [
Get(ResolvedGoPackage, ResolveExternalGoPackageRequest(address=tgt.address))
for tgt in third_party_package_targets
Get(ResolvedGoPackage, ResolveExternalGoPackageRequest(tgt))
for tgt in targets
if isinstance(tgt, GoExternalPackageTarget)
]

resolved_packages = await MultiGet([*first_party_requests, *third_party_requests]) # type: ignore
Expand Down
24 changes: 11 additions & 13 deletions src/python/pants/backend/go/target_type_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
)
from pants.backend.go.util_rules import go_pkg, import_analysis
from pants.backend.go.util_rules.external_module import (
ResolveExternalGoModuleToPackagesRequest,
ResolveExternalGoModuleToPackagesResult,
PackagesFromExternalModule,
PackagesFromExternalModuleRequest,
ResolveExternalGoPackageRequest,
)
from pants.backend.go.util_rules.go_mod import (
Expand Down Expand Up @@ -49,6 +49,7 @@
InjectDependenciesRequest,
InjectedDependencies,
Targets,
WrappedTarget,
)
from pants.engine.unions import UnionRule
from pants.util.frozendict import FrozenDict
Expand Down Expand Up @@ -190,9 +191,10 @@ async def inject_go_external_package_dependencies(
std_lib_imports: GoStdLibImports,
package_mapping: GoImportPathToPackageMapping,
) -> InjectedDependencies:
this_go_package = await Get(
ResolvedGoPackage, ResolveExternalGoPackageRequest(request.dependencies_field.address)
)
wrapped_target = await Get(WrappedTarget, Address, request.dependencies_field.address)
tgt = wrapped_target.target
assert isinstance(tgt, GoExternalPackageTarget)
this_go_package = await Get(ResolvedGoPackage, ResolveExternalGoPackageRequest(tgt))

# Loop through all of the imports of this package and add dependencies on other packages and
# external modules.
Expand Down Expand Up @@ -237,9 +239,9 @@ async def generate_go_external_package_targets(
go_mod_info = await Get(GoModInfo, GoModInfoRequest(generator_addr))
all_resolved_packages = await MultiGet(
Get(
ResolveExternalGoModuleToPackagesResult,
ResolveExternalGoModuleToPackagesRequest(
path=module_descriptor.path,
PackagesFromExternalModule,
PackagesFromExternalModuleRequest(
module_path=module_descriptor.path,
version=module_descriptor.version,
go_sum_digest=go_mod_info.go_sum_stripped_digest,
),
Expand All @@ -264,11 +266,7 @@ def create_tgt(pkg: ResolvedGoPackage) -> GoExternalPackageTarget:

return GeneratedTargets(
request.generator,
(
create_tgt(pkg)
for resolved_pkgs in all_resolved_packages
for pkg in resolved_pkgs.packages
),
(create_tgt(pkg) for resolved_pkgs in all_resolved_packages for pkg in resolved_pkgs),
)


Expand Down
4 changes: 3 additions & 1 deletion src/python/pants/backend/go/util_rules/build_go_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pants.backend.go.target_types import (
GoExternalModulePathField,
GoExternalModuleVersionField,
GoExternalPackageTarget,
GoPackageSources,
)
from pants.backend.go.util_rules.assembly import (
Expand Down Expand Up @@ -74,6 +75,7 @@ async def build_target(
source_files_digest = source_files.snapshot.digest
source_files_subpath = target.address.spec_path
elif is_third_party_package_target(target):
assert isinstance(target, GoExternalPackageTarget)
module_path = target[GoExternalModulePathField].value
module, resolved_package = await MultiGet(
Get(
Expand All @@ -83,7 +85,7 @@ async def build_target(
version=target[GoExternalModuleVersionField].value,
),
),
Get(ResolvedGoPackage, ResolveExternalGoPackageRequest(address=request.address)),
Get(ResolvedGoPackage, ResolveExternalGoPackageRequest(target)),
)

source_files_digest = module.digest
Expand Down
Loading