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] BSP: update compile rules to work again #14906

Merged
merged 1 commit into from
Mar 25, 2022
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
69 changes: 41 additions & 28 deletions src/python/pants/backend/java/bsp/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from dataclasses import dataclass

from pants.backend.java.bsp.spec import JavacOptionsItem, JavacOptionsParams, JavacOptionsResult
from pants.backend.java.target_types import JavaSourceField
from pants.backend.java.target_types import JavaFieldSet, JavaSourceField
from pants.base.build_root import BuildRoot
from pants.base.specs import AddressSpecs
from pants.bsp.protocol import BSPHandlerMapping
from pants.bsp.spec.base import BuildTargetIdentifier, StatusCode
from pants.bsp.util_rules.compile import BSPCompileFieldSet, BSPCompileResult
from pants.bsp.util_rules.compile import BSPCompileRequest, BSPCompileResult
from pants.bsp.util_rules.lifecycle import BSPLanguageSupport
from pants.bsp.util_rules.targets import (
BSPBuildTargets,
Expand All @@ -20,7 +20,7 @@
)
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.native_engine import EMPTY_DIGEST, Digest
from pants.engine.internals.selectors import Get, MultiGet
from pants.engine.rules import collect_rules, rule
from pants.engine.target import CoarsenedTargets, FieldSet, Targets
Expand Down Expand Up @@ -132,39 +132,52 @@ async def bsp_javac_options_request(request: JavacOptionsParams) -> JavacOptions


@dataclass(frozen=True)
class JavaBSPCompileFieldSet(BSPCompileFieldSet):
required_fields = (JavaSourceField,)
source: JavaSourceField
class JavaBSPCompileRequest(BSPCompileRequest):
field_set_type = JavaFieldSet


@rule
async def bsp_java_compile_request(
request: JavaBSPCompileFieldSet, classpath_entry_request: ClasspathEntryRequestFactory
request: JavaBSPCompileRequest, classpath_entry_request: ClasspathEntryRequestFactory
) -> BSPCompileResult:
coarsened_targets = await Get(CoarsenedTargets, Addresses([request.source.address]))
assert len(coarsened_targets) == 1
coarsened_target = coarsened_targets[0]
resolve = await Get(CoursierResolveKey, CoarsenedTargets([coarsened_target]))

result = await Get(
FallibleClasspathEntry,
ClasspathEntryRequest,
classpath_entry_request.for_targets(component=coarsened_target, resolve=resolve),
coarsened_targets = await Get(
CoarsenedTargets, Addresses([fs.address for fs in request.field_sets])
)
_logger.info(f"java compile result = {result}")
output_digest = EMPTY_DIGEST
if result.exit_code == 0 and result.output:
entries = await Get(DigestEntries, Digest, result.output.digest)
new_entires = [
dataclasses.replace(entry, path=os.path.basename(entry.path)) for entry in entries
]
flat_digest = await Get(Digest, CreateDigest(new_entires))
output_digest = await Get(
Digest, AddPrefix(flat_digest, f"jvm/resolves/{resolve.name}/lib")
resolve = await Get(CoursierResolveKey, CoarsenedTargets, coarsened_targets)

results = await MultiGet(
Get(
FallibleClasspathEntry,
ClasspathEntryRequest,
classpath_entry_request.for_targets(component=coarsened_target, resolve=resolve),
)
for coarsened_target in coarsened_targets
)

status = StatusCode.OK
if any(r.exit_code != 0 for r in results):
status = StatusCode.ERROR

output_digest = EMPTY_DIGEST
if status == StatusCode.OK:
output_entries = []
for result in results:
if not result.output:
continue
entries = await Get(DigestEntries, Digest, result.output.digest)
output_entries.extend(
[
dataclasses.replace(
entry,
path=f"jvm/resolves/{resolve.name}/lib/{os.path.basename(entry.path)}",
)
for entry in entries
]
)
output_digest = await Get(Digest, CreateDigest(output_entries))

return BSPCompileResult(
status=StatusCode.ERROR if result.exit_code != 0 else StatusCode.OK,
status=status,
output_digest=output_digest,
)

Expand All @@ -175,5 +188,5 @@ def rules():
UnionRule(BSPLanguageSupport, JavaBSPLanguageSupport),
UnionRule(BSPBuildTargetsMetadataRequest, JavaBSPBuildTargetsMetadataRequest),
UnionRule(BSPHandlerMapping, JavacOptionsHandlerMapping),
UnionRule(BSPCompileFieldSet, JavaBSPCompileFieldSet),
UnionRule(BSPCompileRequest, JavaBSPCompileRequest),
)
67 changes: 40 additions & 27 deletions src/python/pants/backend/scala/bsp/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
ScalaTestClassesResult,
)
from pants.backend.scala.subsystems.scala import ScalaSubsystem
from pants.backend.scala.target_types import ScalaSourceField
from pants.backend.scala.target_types import ScalaFieldSet, ScalaSourceField
from pants.base.build_root import BuildRoot
from pants.base.specs import AddressSpecs
from pants.bsp.protocol import BSPHandlerMapping
from pants.bsp.spec.base import BuildTarget, BuildTargetIdentifier, StatusCode
from pants.bsp.spec.targets import DependencyModule
from pants.bsp.util_rules.compile import BSPCompileFieldSet, BSPCompileResult
from pants.bsp.util_rules.compile import BSPCompileRequest, BSPCompileResult
from pants.bsp.util_rules.lifecycle import BSPLanguageSupport
from pants.bsp.util_rules.targets import (
BSPBuildTargetInternal,
Expand Down Expand Up @@ -405,40 +405,53 @@ async def scala_bsp_dependency_modules(


@dataclass(frozen=True)
class ScalaBSPCompileFieldSet(BSPCompileFieldSet):
required_fields = (ScalaSourceField,)
source: ScalaSourceField
class ScalaBSPCompileRequest(BSPCompileRequest):
field_set_type = ScalaFieldSet


@rule
async def bsp_scala_compile_request(
request: ScalaBSPCompileFieldSet,
request: ScalaBSPCompileRequest,
classpath_entry_request: ClasspathEntryRequestFactory,
) -> BSPCompileResult:
coarsened_targets = await Get(CoarsenedTargets, Addresses([request.source.address]))
assert len(coarsened_targets) == 1
coarsened_target = coarsened_targets[0]
resolve = await Get(CoursierResolveKey, CoarsenedTargets([coarsened_target]))

result = await Get(
FallibleClasspathEntry,
ClasspathEntryRequest,
classpath_entry_request.for_targets(component=coarsened_target, resolve=resolve),
coarsened_targets = await Get(
CoarsenedTargets, Addresses([fs.address for fs in request.field_sets])
)
_logger.info(f"scala compile result = {result}")
output_digest = EMPTY_DIGEST
if result.exit_code == 0 and result.output:
entries = await Get(DigestEntries, Digest, result.output.digest)
new_entires = [
dataclasses.replace(entry, path=os.path.basename(entry.path)) for entry in entries
]
flat_digest = await Get(Digest, CreateDigest(new_entires))
output_digest = await Get(
Digest, AddPrefix(flat_digest, f"jvm/resolves/{resolve.name}/lib")
resolve = await Get(CoursierResolveKey, CoarsenedTargets, coarsened_targets)

results = await MultiGet(
Get(
FallibleClasspathEntry,
ClasspathEntryRequest,
classpath_entry_request.for_targets(component=coarsened_target, resolve=resolve),
)
for coarsened_target in coarsened_targets
)

status = StatusCode.OK
if any(r.exit_code != 0 for r in results):
status = StatusCode.ERROR

output_digest = EMPTY_DIGEST
if status == StatusCode.OK:
output_entries = []
for result in results:
if not result.output:
continue
entries = await Get(DigestEntries, Digest, result.output.digest)
output_entries.extend(
[
dataclasses.replace(
entry,
path=f"jvm/resolves/{resolve.name}/lib/{os.path.basename(entry.path)}",
)
for entry in entries
]
)
output_digest = await Get(Digest, CreateDigest(output_entries))

return BSPCompileResult(
status=StatusCode.ERROR if result.exit_code != 0 else StatusCode.OK,
status=status,
output_digest=output_digest,
)

Expand All @@ -451,6 +464,6 @@ def rules():
UnionRule(BSPHandlerMapping, ScalacOptionsHandlerMapping),
UnionRule(BSPHandlerMapping, ScalaMainClassesHandlerMapping),
UnionRule(BSPHandlerMapping, ScalaTestClassesHandlerMapping),
UnionRule(BSPCompileFieldSet, ScalaBSPCompileFieldSet),
UnionRule(BSPCompileRequest, ScalaBSPCompileRequest),
UnionRule(BSPDependencyModulesRequest, ScalaBSPDependencyModulesRequest),
)
Loading