-
-
Notifications
You must be signed in to change notification settings - Fork 636
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] BSP: switch to aggregate build targets #14835
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b112013
checkpoint
c4065f0
checkpoint
3641776
checkpoint
d86a7f4
checkpoint
526ac10
bug fix
96fb0d1
checkpoint
5e6a244
use source root for `roots` in Build Target Sources Request
cbcd620
remove commented-out code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,42 +6,32 @@ | |
from dataclasses import dataclass | ||
|
||
from pants.backend.java.bsp.spec import JavacOptionsItem, JavacOptionsParams, JavacOptionsResult | ||
from pants.backend.java.compile.javac import compute_output_jar_filename | ||
from pants.backend.java.dependency_inference.symbol_mapper import AllJavaTargets | ||
from pants.backend.java.target_types import JavaSourceField | ||
from pants.base.build_root import BuildRoot | ||
from pants.bsp.context import BSPContext | ||
from pants.base.specs import AddressSpecs | ||
from pants.bsp.protocol import BSPHandlerMapping | ||
from pants.bsp.spec.base import ( | ||
BuildTarget, | ||
BuildTargetCapabilities, | ||
BuildTargetIdentifier, | ||
StatusCode, | ||
) | ||
from pants.bsp.spec.base import BuildTargetIdentifier, StatusCode | ||
from pants.bsp.util_rules.compile import BSPCompileFieldSet, BSPCompileResult | ||
from pants.bsp.util_rules.lifecycle import BSPLanguageSupport | ||
from pants.bsp.util_rules.targets import BSPBuildTargets, BSPBuildTargetsRequest | ||
from pants.build_graph.address import Address, AddressInput | ||
from pants.bsp.util_rules.targets import ( | ||
BSPBuildTargetsMetadataRequest, | ||
BSPBuildTargetsMetadataResult, | ||
BSPBuildTargetsNew, | ||
) | ||
from pants.engine.addresses import Addresses | ||
from pants.engine.fs import CreateDigest, DigestEntries | ||
from pants.engine.internals.native_engine import EMPTY_DIGEST, AddPrefix, Digest | ||
from pants.engine.internals.selectors import Get, MultiGet | ||
from pants.engine.rules import collect_rules, rule | ||
from pants.engine.target import ( | ||
CoarsenedTargets, | ||
Dependencies, | ||
DependenciesRequest, | ||
Target, | ||
WrappedTarget, | ||
) | ||
from pants.engine.unions import UnionMembership, UnionRule | ||
from pants.jvm.bsp.spec import JvmBuildTarget | ||
from pants.engine.target import CoarsenedTargets, FieldSet, Targets | ||
from pants.engine.unions import UnionRule | ||
from pants.jvm.compile import ( | ||
ClasspathEntryRequest, | ||
ClasspathEntryRequestFactory, | ||
FallibleClasspathEntry, | ||
) | ||
from pants.jvm.resolve.key import CoursierResolveKey | ||
from pants.jvm.target_types import JvmResolveField | ||
|
||
LANGUAGE_ID = "java" | ||
|
||
|
@@ -53,62 +43,27 @@ class JavaBSPLanguageSupport(BSPLanguageSupport): | |
can_compile = True | ||
|
||
|
||
class JavaBSPBuildTargetsRequest(BSPBuildTargetsRequest): | ||
pass | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ResolveJavaBSPBuildTargetRequest: | ||
target: Target | ||
class JavaMetadataFieldSet(FieldSet): | ||
required_fields = (JavaSourceField, JvmResolveField) | ||
|
||
source: JavaSourceField | ||
resolve: JvmResolveField | ||
|
||
@rule | ||
async def bsp_resolve_one_java_build_target( | ||
request: ResolveJavaBSPBuildTargetRequest, | ||
union_membership: UnionMembership, | ||
) -> BuildTarget: | ||
dep_addrs = await Get(Addresses, DependenciesRequest(request.target[Dependencies])) | ||
impls = union_membership.get(BSPCompileFieldSet) | ||
|
||
reported_deps = [] | ||
for dep_addr in dep_addrs: | ||
if dep_addr == request.target.address: | ||
continue | ||
|
||
wrapped_dep_tgt = await Get(WrappedTarget, Address, dep_addr) | ||
dep_tgt = wrapped_dep_tgt.target | ||
for impl in impls: | ||
if impl.is_applicable(dep_tgt): | ||
reported_deps.append(BuildTargetIdentifier.from_address(dep_tgt.address)) | ||
break | ||
|
||
return BuildTarget( | ||
id=BuildTargetIdentifier.from_address(request.target.address), | ||
display_name=str(request.target.address), | ||
base_directory=None, | ||
tags=(), | ||
capabilities=BuildTargetCapabilities( | ||
can_compile=True, | ||
), | ||
language_ids=(LANGUAGE_ID,), | ||
dependencies=tuple(reported_deps), | ||
data_kind="jvm", | ||
data=JvmBuildTarget(), | ||
) | ||
|
||
class JavaBSPBuildTargetsMetadataRequest(BSPBuildTargetsMetadataRequest): | ||
language_id = LANGUAGE_ID | ||
can_merge_metadata_from = () | ||
field_set_type = JavaMetadataFieldSet | ||
|
||
|
||
@rule | ||
async def bsp_resolve_all_java_build_targets( | ||
_: JavaBSPBuildTargetsRequest, | ||
all_java_targets: AllJavaTargets, | ||
bsp_context: BSPContext, | ||
) -> BSPBuildTargets: | ||
if LANGUAGE_ID not in bsp_context.client_params.capabilities.language_ids: | ||
return BSPBuildTargets() | ||
build_targets = await MultiGet( | ||
Get(BuildTarget, ResolveJavaBSPBuildTargetRequest(tgt)) for tgt in all_java_targets | ||
async def bsp_resolve_java_metadata( | ||
_: JavaBSPBuildTargetsMetadataRequest, | ||
) -> BSPBuildTargetsMetadataResult: | ||
return BSPBuildTargetsMetadataResult( | ||
can_compile=True, | ||
) | ||
return BSPBuildTargets(targets=tuple(build_targets)) | ||
|
||
|
||
# ----------------------------------------------------------------------------------------------- | ||
|
@@ -137,23 +92,33 @@ class HandleJavacOptionsResult: | |
async def handle_bsp_java_options_request( | ||
request: HandleJavacOptionsRequest, | ||
build_root: BuildRoot, | ||
bsp_build_targets: BSPBuildTargetsNew, | ||
) -> HandleJavacOptionsResult: | ||
wrapped_target = await Get(WrappedTarget, AddressInput, request.bsp_target_id.address_input) | ||
coarsened_targets = await Get(CoarsenedTargets, Addresses([wrapped_target.target.address])) | ||
assert len(coarsened_targets) == 1 | ||
coarsened_target = coarsened_targets[0] | ||
resolve = await Get(CoursierResolveKey, CoarsenedTargets([coarsened_target])) | ||
output_file = compute_output_jar_filename(coarsened_target) | ||
bsp_target_name = request.bsp_target_id.uri[len("pants:") :] | ||
if bsp_target_name not in bsp_build_targets.targets_mapping: | ||
raise ValueError(f"Invalid BSP target name: {request.bsp_target_id}") | ||
targets = await Get( | ||
Targets, | ||
AddressSpecs, | ||
bsp_build_targets.targets_mapping[bsp_target_name].specs.address_specs, | ||
) | ||
|
||
coarsened_targets = await Get(CoarsenedTargets, Addresses(tgt.address for tgt in targets)) | ||
# assert len(coarsened_targets) == 1 | ||
# coarsened_target = coarsened_targets[0] | ||
resolve = await Get(CoursierResolveKey, CoarsenedTargets, coarsened_targets) | ||
# output_file = compute_output_jar_filename(coarsened_target) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be deleted? |
||
|
||
return HandleJavacOptionsResult( | ||
JavacOptionsItem( | ||
target=request.bsp_target_id, | ||
options=(), | ||
classpath=( | ||
build_root.pathlib_path.joinpath( | ||
f".pants.d/bsp/jvm/resolves/{resolve.name}/lib/{output_file}" | ||
).as_uri(), | ||
), | ||
# classpath=( | ||
# build_root.pathlib_path.joinpath( | ||
# f".pants.d/bsp/jvm/resolves/{resolve.name}/lib/{output_file}" | ||
# ).as_uri(), | ||
# ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be deleted? |
||
classpath=(), | ||
class_directory=build_root.pathlib_path.joinpath( | ||
f".pants.d/bsp/jvm/resolves/{resolve.name}/classes" | ||
).as_uri(), | ||
|
@@ -216,7 +181,7 @@ def rules(): | |
return ( | ||
*collect_rules(), | ||
UnionRule(BSPLanguageSupport, JavaBSPLanguageSupport), | ||
UnionRule(BSPBuildTargetsRequest, JavaBSPBuildTargetsRequest), | ||
UnionRule(BSPBuildTargetsMetadataRequest, JavaBSPBuildTargetsMetadataRequest), | ||
UnionRule(BSPHandlerMapping, JavacOptionsHandlerMapping), | ||
UnionRule(BSPCompileFieldSet, JavaBSPCompileFieldSet), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be deleted?