Skip to content

Commit

Permalink
scala source file should not depend on scalac plugins (pantsbuild#20971)
Browse files Browse the repository at this point in the history
These are compile-time-only dependencies and should not be packaged with
the source file or included in the classpath for the source file.

In our repository, this caused a conflict because the semanticdb package
internally wraps the fastparse library with a different version than the
one we are using. There is no need for semanticdb to be packaged or run
within a test of a target; it is a compile-time-only dependency.
Moreover, the scalac compile goal handles the scalac plugins on its own
and doesn't use that.

I ran this on our Scala repository, and it compiled successfully.
  • Loading branch information
somdoron authored Jun 6, 2024
1 parent 0969267 commit 30edea6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 33 deletions.
4 changes: 4 additions & 0 deletions docs/notes/2.23.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ We offer [formal sponsorship tiers for companies](https://www.pantsbuild.org/spo

### Backends

#### Scala

Source files no longer produce a dependency on Scala plugins. If you are using a Scala plugin that is also required by the source code (such as acyclic), please add an explicit dependency or set the `packages` field on the artifact.

#### Python

[The `pants.backend.experimental.python.typecheck.pyright` backend](https://www.pantsbuild.org/2.23/reference/subsystems/pyright) now uses version 1.1.365 by default.
Expand Down
2 changes: 2 additions & 0 deletions src/python/pants/backend/scala/compile/scalac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ def test_compile_with_local_scalac_plugin(
scala_sources(
scalac_plugins=["acyclic"],
dependencies=["3rdparty/jvm:com.lihaoyi_acyclic_2.13"],
)
"""
),
Expand Down Expand Up @@ -1028,6 +1029,7 @@ def test_cross_compile_with_scalac_plugin(
scala_sources(
name="main",
scalac_plugins=["acyclic"],
dependencies=["3rdparty/jvm:acyclic"],
resolve=parametrize("scala2.12", "scala2.13")
)
"""
Expand Down
33 changes: 0 additions & 33 deletions src/python/pants/backend/scala/dependency_inference/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
from dataclasses import dataclass

from pants.backend.scala.compile import scalac_plugins
from pants.backend.scala.compile.scalac_plugins import (
ScalaPluginsForTargetWithoutResolveRequest,
ScalaPluginTargetsForTarget,
)
from pants.backend.scala.dependency_inference import scala_parser, symbol_mapper
from pants.backend.scala.dependency_inference.scala_parser import ScalaSourceDependencyAnalysis
from pants.backend.scala.subsystems.scala import ScalaSubsystem
Expand All @@ -28,8 +24,6 @@
FieldSet,
InferDependenciesRequest,
InferredDependencies,
WrappedTarget,
WrappedTargetRequest,
)
from pants.engine.unions import UnionRule
from pants.jvm.dependency_inference import artifact_mapper
Expand Down Expand Up @@ -119,10 +113,6 @@ class ScalaPluginDependencyInferenceFieldSet(FieldSet):
resolve: JvmResolveField


class InferScalaPluginDependenciesRequest(InferDependenciesRequest):
infer_from = ScalaPluginDependencyInferenceFieldSet


@dataclass(frozen=True)
class ScalaRuntimeForResolveRequest:
resolve_name: str
Expand Down Expand Up @@ -172,28 +162,6 @@ async def infer_scala_library_dependency(
return InferredDependencies(scala_library_target_info.addresses)


@rule(desc="Infer dependency on scala plugin artifacts for Scala target.")
async def infer_scala_plugin_dependencies(
request: InferScalaPluginDependenciesRequest,
) -> InferredDependencies:
"""Adds dependencies on plugins for scala source files, so that they get included in the
target's resolve."""

wrapped_target = await Get(
WrappedTarget,
WrappedTargetRequest(request.field_set.address, description_of_origin="<infallible>"),
)
target = wrapped_target.target

scala_plugins = await Get(
ScalaPluginTargetsForTarget, ScalaPluginsForTargetWithoutResolveRequest(target)
)

plugin_addresses = [target.address for target in scala_plugins.artifacts]

return InferredDependencies(plugin_addresses)


def rules():
return [
*collect_rules(),
Expand All @@ -204,5 +172,4 @@ def rules():
*versions.rules(),
UnionRule(InferDependenciesRequest, InferScalaSourceDependencies),
UnionRule(InferDependenciesRequest, InferScalaLibraryDependencyRequest),
UnionRule(InferDependenciesRequest, InferScalaPluginDependenciesRequest),
]

0 comments on commit 30edea6

Please sign in to comment.