Skip to content

Commit

Permalink
[jvm] Support mixed-language firstparty inference (pantsbuild#13579)
Browse files Browse the repository at this point in the history
Merges the firstparty symbol mappings behind a single `@union` (`FirstPartyMappingRequest`), and moves generic inference code to `pants.jvm.dependency_inference`. The smoketest code in `testprojects/src/jvm` now properly infers across languages.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
stuhood authored Nov 11, 2021
1 parent d85a14a commit 881f877
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 292 deletions.
2 changes: 2 additions & 0 deletions build-support/bin/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def run_pants_help_all() -> dict[str, Any]:
"internal_plugins.releases",
"pants.backend.experimental.java",
"pants.backend.experimental.java.debug_goals",
"pants.backend.experimental.scala",
"pants.backend.experimental.scala.debug_goals",
]
activated_backends = [
"pants.backend.codegen.protobuf.python",
Expand Down
2 changes: 1 addition & 1 deletion build-support/githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if git diff "${MERGE_BASE}" --name-only | grep '\.rs$' > /dev/null; then
./build-support/bin/check_rust_pre_commit.sh
fi

echo "* Checking MyPy"
echo "* Typechecking"
./pants --changed-since="${MERGE_BASE}" --changed-dependees=transitive check

echo "* Checking linters, formatters, and headers"
Expand Down
10 changes: 3 additions & 7 deletions src/python/pants/backend/experimental/java/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.java.compile import javac
from pants.backend.java.dependency_inference import (
import_parser,
java_parser,
java_parser_launcher,
package_mapper,
)
from pants.backend.java.dependency_inference import import_parser, java_parser, java_parser_launcher
from pants.backend.java.dependency_inference import rules as dependency_inference_rules
from pants.backend.java.goals import check, tailor
from pants.backend.java.package import deploy_jar
Expand All @@ -22,6 +17,7 @@
from pants.backend.java.test import junit
from pants.jvm import classpath, jdk_rules
from pants.jvm import util_rules as jvm_util_rules
from pants.jvm.dependency_inference import symbol_mapper
from pants.jvm.goals import coursier
from pants.jvm.resolve import coursier_fetch, coursier_setup
from pants.jvm.target_types import JvmArtifact, JvmDependencyLockfile
Expand Down Expand Up @@ -52,7 +48,7 @@ def rules():
*import_parser.rules(),
*java_parser.rules(),
*java_parser_launcher.rules(),
*package_mapper.rules(),
*symbol_mapper.rules(),
*dependency_inference_rules.rules(),
*tailor.rules(),
*jvm_util_rules.rules(),
Expand Down
114 changes: 0 additions & 114 deletions src/python/pants/backend/java/dependency_inference/package_mapper.py

This file was deleted.

This file was deleted.

This file was deleted.

30 changes: 13 additions & 17 deletions src/python/pants/backend/java/dependency_inference/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@

import logging

from pants.backend.java.dependency_inference import (
artifact_mapper,
import_parser,
java_parser,
package_mapper,
)
from pants.backend.java.dependency_inference.artifact_mapper import (
AvailableThirdPartyArtifacts,
ThirdPartyJavaPackageToArtifactMapping,
find_artifact_mapping,
)
from pants.backend.java.dependency_inference.package_mapper import FirstPartyJavaPackageMapping
from pants.backend.java.dependency_inference import import_parser, java_parser, symbol_mapper
from pants.backend.java.dependency_inference.types import JavaSourceDependencyAnalysis
from pants.backend.java.subsystems.java_infer import JavaInferSubsystem
from pants.backend.java.target_types import JavaSourceField
Expand All @@ -32,6 +21,13 @@
WrappedTarget,
)
from pants.engine.unions import UnionRule
from pants.jvm.dependency_inference import artifact_mapper
from pants.jvm.dependency_inference.artifact_mapper import (
AvailableThirdPartyArtifacts,
ThirdPartyPackageToArtifactMapping,
find_artifact_mapping,
)
from pants.jvm.dependency_inference.symbol_mapper import FirstPartySymbolMapping
from pants.util.ordered_set import FrozenOrderedSet, OrderedSet

logger = logging.getLogger(__name__)
Expand All @@ -45,8 +41,8 @@ class InferJavaSourceDependencies(InferDependenciesRequest):
async def infer_java_dependencies_via_imports(
request: InferJavaSourceDependencies,
java_infer_subsystem: JavaInferSubsystem,
first_party_dep_map: FirstPartyJavaPackageMapping,
third_party_artifact_mapping: ThirdPartyJavaPackageToArtifactMapping,
first_party_dep_map: FirstPartySymbolMapping,
third_party_artifact_mapping: ThirdPartyPackageToArtifactMapping,
available_artifacts: AvailableThirdPartyArtifacts,
) -> InferredDependencies:
if (
Expand Down Expand Up @@ -79,12 +75,12 @@ async def infer_java_dependencies_via_imports(

types.update(maybe_qualify_types)

dep_map = first_party_dep_map.package_rooted_dependency_map
dep_map = first_party_dep_map.symbols

dependencies: OrderedSet[Address] = OrderedSet()

for typ in types:
first_party_matches = dep_map.addresses_for_type(typ)
first_party_matches = dep_map.addresses_for_symbol(typ)
third_party_matches: FrozenOrderedSet[Address] = FrozenOrderedSet()
if java_infer_subsystem.third_party_imports:
third_party_matches = find_artifact_mapping(
Expand Down Expand Up @@ -120,7 +116,7 @@ def rules():
*artifact_mapper.rules(),
*java_parser.rules(),
*import_parser.rules(),
*package_mapper.rules(),
*symbol_mapper.rules(),
*source_files_rules(),
UnionRule(InferDependenciesRequest, InferJavaSourceDependencies),
]
14 changes: 7 additions & 7 deletions src/python/pants/backend/java/dependency_inference/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import pytest

from pants.backend.java.compile.javac import rules as javac_rules
from pants.backend.java.dependency_inference.artifact_mapper import (
FrozenTrieNode,
ThirdPartyJavaPackageToArtifactMapping,
UnversionedCoordinate,
)
from pants.backend.java.dependency_inference.java_parser import rules as java_parser_rules
from pants.backend.java.dependency_inference.java_parser_launcher import (
rules as java_parser_launcher_rules,
Expand All @@ -34,6 +29,11 @@
InferredDependencies,
Targets,
)
from pants.jvm.dependency_inference.artifact_mapper import (
FrozenTrieNode,
ThirdPartyPackageToArtifactMapping,
UnversionedCoordinate,
)
from pants.jvm.jdk_rules import rules as java_util_rules
from pants.jvm.resolve.coursier_fetch import rules as coursier_fetch_rules
from pants.jvm.resolve.coursier_setup import rules as coursier_setup_rules
Expand Down Expand Up @@ -68,7 +68,7 @@ def rule_runner() -> RuleRunner:
QueryRule(ExplicitlyProvidedDependencies, [DependenciesRequest]),
QueryRule(InferredDependencies, [InferJavaSourceDependencies]),
QueryRule(Targets, [UnparsedAddressInputs]),
QueryRule(ThirdPartyJavaPackageToArtifactMapping, []),
QueryRule(ThirdPartyPackageToArtifactMapping, []),
],
target_types=[JavaSourcesGeneratorTarget, JunitTestsGeneratorTarget, JvmArtifact],
)
Expand Down Expand Up @@ -568,7 +568,7 @@ def test_third_party_mapping_parsing(rule_runner: RuleRunner) -> None:
],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
mapping = rule_runner.request(ThirdPartyJavaPackageToArtifactMapping, [])
mapping = rule_runner.request(ThirdPartyPackageToArtifactMapping, [])
root_node = mapping.mapping_root

# Handy trie traversal function to placate mypy
Expand Down
Loading

0 comments on commit 881f877

Please sign in to comment.