Skip to content

Commit

Permalink
Set a default for [jvm].resolves and [jvm].default_resolve (#13925)
Browse files Browse the repository at this point in the history
Part of #12742.

We set a default because it's important to the Pants project that onboarding is as effortless as possible, e.g. via sensible defaults.

An additional benefit is that we can now be confident a resolve is set up, as it is impossible to override an option to be `None` (#11719). We decided that we want to require lockfiles when using Pants, so this is seen as a benefit.

--

This uses the resolve name `jvm-default` because the namespace will be shared with Python, e.g. `py-default`. 

It uses the lockfile path `3rdparty/jvm/default.lock`. We use the file extension `.lock` instead of `.json` to better express the semantics, including that you should not hand-edit the file. Using the `3rdparty/jvm` folder is a sensible default for monorepos, and people can override it to where they want.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Dec 20, 2021
1 parent 5774cdc commit 561cfab
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 230 deletions.
72 changes: 15 additions & 57 deletions src/python/pants/backend/java/compile/javac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
from pants.jvm.util_rules import rules as util_rules
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, QueryRule, RuleRunner, logging

NAMED_RESOLVE_OPTIONS = '--jvm-resolves={"test": "coursier_resolve.lockfile"}'
DEFAULT_RESOLVE_OPTION = "--jvm-default-resolve=test"


@pytest.fixture
def rule_runner() -> RuleRunner:
Expand Down Expand Up @@ -73,14 +70,8 @@ def rule_runner() -> RuleRunner:
QueryRule(RenderedClasspath, (CompileJavaSourceRequest,)),
],
target_types=[JavaSourcesGeneratorTarget, JvmArtifactTarget],
bootstrap_args=[
NAMED_RESOLVE_OPTIONS,
DEFAULT_RESOLVE_OPTION,
],
)
rule_runner.set_options(
args=[NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION], env_inherit=PYTHON_BOOTSTRAP_ENV
)
rule_runner.set_options([], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down Expand Up @@ -115,17 +106,8 @@ def rule_runner() -> RuleRunner:
def test_compile_no_deps(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"BUILD": dedent(
"""\
java_sources(
name = 'lib',
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"BUILD": "java_sources(name='lib')",
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"ExampleLib.java": JAVA_LIB_SOURCE,
}
)
Expand Down Expand Up @@ -165,9 +147,7 @@ def test_compile_jdk_versions(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"ExampleLib.java": JAVA_LIB_SOURCE,
}
)
Expand All @@ -178,19 +158,13 @@ def test_compile_jdk_versions(rule_runner: RuleRunner) -> None:
),
resolve=make_resolve(rule_runner),
)
rule_runner.set_options(
["--javac-jdk=zulu:8.0.312", NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
rule_runner.set_options(["--javac-jdk=zulu:8.0.312"], env_inherit=PYTHON_BOOTSTRAP_ENV)
classpath = rule_runner.request(RenderedClasspath, [request])
assert classpath.content == {
".ExampleLib.java.lib.javac.jar": {"org/pantsbuild/example/lib/ExampleLib.class"}
}

rule_runner.set_options(
["--javac-jdk=bogusjdk:999", NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
rule_runner.set_options(["--javac-jdk=bogusjdk:999"], env_inherit=PYTHON_BOOTSTRAP_ENV)
expected_exception_msg = r".*?JVM bogusjdk:999 not found in index.*?"
with pytest.raises(ExecutionError, match=expected_exception_msg):
rule_runner.request(ClasspathEntry, [request])
Expand All @@ -208,9 +182,7 @@ def test_compile_multiple_source_files(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"ExampleLib.java": JAVA_LIB_SOURCE,
"OtherLib.java": dedent(
"""\
Expand Down Expand Up @@ -288,9 +260,7 @@ def test_compile_with_cycle(rule_runner: RuleRunner) -> None:
"""\
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"a/BUILD": dedent(
"""\
java_sources(
Expand Down Expand Up @@ -376,9 +346,7 @@ def test_compile_with_transitive_cycle(rule_runner: RuleRunner) -> None:
public class Main implements A {}
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"a/BUILD": dedent(
"""\
java_sources(
Expand Down Expand Up @@ -462,9 +430,7 @@ def test_compile_with_transitive_multiple_sources(rule_runner: RuleRunner) -> No
class Other implements B {}
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"lib/BUILD": dedent(
"""\
java_sources(
Expand Down Expand Up @@ -523,9 +489,7 @@ def test_compile_with_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"Example.java": JAVA_LIB_MAIN_SOURCE,
"lib/BUILD": dedent(
"""\
Expand Down Expand Up @@ -566,9 +530,7 @@ def test_compile_of_package_info(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"package-info.java": dedent(
"""
package org.pantsbuild.example;
Expand Down Expand Up @@ -607,9 +569,7 @@ def test_compile_with_missing_dep_fails(rule_runner: RuleRunner) -> None:
"""
),
"Example.java": JAVA_LIB_MAIN_SOURCE,
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
}
)
request = CompileJavaSourceRequest(
Expand Down Expand Up @@ -659,7 +619,7 @@ def test_compile_with_maven_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": resolved_joda_lockfile.to_json().decode("utf-8"),
"3rdparty/jvm/default.lock": resolved_joda_lockfile.to_json().decode(),
"Example.java": dedent(
"""
package org.pantsbuild.example;
Expand Down Expand Up @@ -700,9 +660,7 @@ def test_compile_with_missing_maven_dep_fails(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(entries=()).to_json().decode(),
"Example.java": dedent(
"""
package org.pantsbuild.example;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, QueryRule, RuleRunner
from pants.util.ordered_set import FrozenOrderedSet

NAMED_RESOLVE_OPTIONS = '--jvm-resolves={"test": "coursier_resolve.lockfile"}'
DEFAULT_RESOLVE_OPTION = "--jvm-default-resolve=test"


@pytest.fixture
def rule_runner() -> RuleRunner:
Expand All @@ -65,9 +62,7 @@ def rule_runner() -> RuleRunner:
],
target_types=[JavaSourcesGeneratorTarget, JunitTestsGeneratorTarget, JvmArtifactTarget],
)
rule_runner.set_options(
args=[NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION], env_inherit=PYTHON_BOOTSTRAP_ENV
)
rule_runner.set_options(args=[], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down
15 changes: 4 additions & 11 deletions src/python/pants/backend/java/package/deploy_jar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def rule_runner() -> RuleRunner:
DeployJarTarget,
],
)
rule_runner.set_options(
args=['--jvm-resolves={"test": "coursier_resolve.lockfile"}', "--jvm-default-resolve=test"],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
rule_runner.set_options(args=[], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down Expand Up @@ -230,9 +227,7 @@ def test_deploy_jar_no_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
"Example.java": JAVA_MAIN_SOURCE_NO_DEPS,
}
)
Expand Down Expand Up @@ -261,9 +256,7 @@ def test_deploy_jar_local_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
"Example.java": JAVA_MAIN_SOURCE,
"lib/ExampleLib.java": JAVA_LIB_SOURCE,
}
Expand Down Expand Up @@ -303,7 +296,7 @@ def test_deploy_jar_coursier_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": COURSIER_LOCKFILE_SOURCE,
"3rdparty/jvm/default.lock": COURSIER_LOCKFILE_SOURCE,
"Example.java": JAVA_MAIN_SOURCE,
"lib/ExampleLib.java": JAVA_JSON_MANGLING_LIB_SOURCE,
}
Expand Down
39 changes: 9 additions & 30 deletions src/python/pants/backend/scala/compile/scalac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
from pants.jvm.util_rules import rules as util_rules
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, QueryRule, RuleRunner, logging

NAMED_RESOLVE_OPTIONS = '--jvm-resolves={"test": "coursier_resolve.lockfile"}'
DEFAULT_RESOLVE_OPTION = "--jvm-default-resolve=test"


@pytest.fixture
def rule_runner() -> RuleRunner:
Expand All @@ -63,13 +60,7 @@ def rule_runner() -> RuleRunner:
],
target_types=[JvmArtifactTarget, ScalaSourcesGeneratorTarget, ScalacPluginTarget],
)
rule_runner.set_options(
args=[
NAMED_RESOLVE_OPTIONS,
DEFAULT_RESOLVE_OPTION,
],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
rule_runner.set_options(args=[], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down Expand Up @@ -111,9 +102,7 @@ def test_compile_no_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
"ExampleLib.scala": SCALA_LIB_SOURCE,
}
)
Expand Down Expand Up @@ -163,9 +152,7 @@ def test_compile_with_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
"Example.scala": SCALA_LIB_MAIN_SOURCE,
"lib/BUILD": dedent(
"""\
Expand Down Expand Up @@ -210,9 +197,7 @@ def test_compile_with_missing_dep_fails(rule_runner: RuleRunner) -> None:
"""
),
"Example.scala": SCALA_LIB_MAIN_SOURCE,
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
}
)
request = CompileScalaSourceRequest(
Expand Down Expand Up @@ -261,7 +246,7 @@ def test_compile_with_maven_deps(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": resolved_joda_lockfile.to_json().decode("utf-8"),
"3rdparty/jvm/default.lock": resolved_joda_lockfile.to_json().decode(),
"Example.scala": dedent(
"""
package org.pantsbuild.example
Expand Down Expand Up @@ -309,9 +294,7 @@ def test_compile_with_undeclared_jvm_artifact_target_fails(rule_runner: RuleRunn
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode("utf-8"),
"Example.scala": dedent(
"""
package org.pantsbuild.example
Expand Down Expand Up @@ -358,9 +341,7 @@ def test_compile_with_undeclared_jvm_artifact_dependency_fails(rule_runner: Rule
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(entries=())
.to_json()
.decode("utf-8"),
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(()).to_json().decode(),
"Example.scala": dedent(
"""
package org.pantsbuild.example
Expand Down Expand Up @@ -414,7 +395,7 @@ def test_compile_with_scalac_plugins(rule_runner: RuleRunner) -> None:
)
"""
),
"coursier_resolve.lockfile": CoursierResolvedLockfile(
"3rdparty/jvm/default.lock": CoursierResolvedLockfile(
entries=(
CoursierLockfileEntry(
coord=Coordinate(
Expand Down Expand Up @@ -455,10 +436,8 @@ class B {
)
rule_runner.set_options(
args=[
NAMED_RESOLVE_OPTIONS,
DEFAULT_RESOLVE_OPTION,
"--scalac-plugins-global=lib:acyclic",
"--scalac-plugins-lockfile=coursier_resolve.lockfile",
"--scalac-plugins-lockfile=3rdparty/jvm/default.lock",
],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
from pants.jvm.util_rules import rules as util_rules
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, QueryRule, RuleRunner

NAMED_RESOLVE_OPTIONS = '--jvm-resolves={"test": "coursier_resolve.lockfile"}'
DEFAULT_RESOLVE_OPTION = "--jvm-default-resolve=test"


@pytest.fixture
def rule_runner() -> RuleRunner:
Expand All @@ -53,9 +50,7 @@ def rule_runner() -> RuleRunner:
],
target_types=[ScalaSourcesGeneratorTarget],
)
rule_runner.set_options(
args=[NAMED_RESOLVE_OPTIONS, DEFAULT_RESOLVE_OPTION], env_inherit=PYTHON_BOOTSTRAP_ENV
)
rule_runner.set_options(args=[], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
from pants.jvm.util_rules import rules as util_rules
from pants.testutil.rule_runner import PYTHON_BOOTSTRAP_ENV, RuleRunner

NAMED_RESOLVE_OPTIONS = '--jvm-resolves={"test": "coursier_resolve.lockfile"}'
DEFAULT_RESOLVE_OPTION = "--jvm-default-resolve=test"


@pytest.fixture
def rule_runner() -> RuleRunner:
Expand All @@ -64,13 +61,7 @@ def rule_runner() -> RuleRunner:
],
target_types=[ScalaSourceTarget, ScalaSourcesGeneratorTarget],
)
rule_runner.set_options(
[
NAMED_RESOLVE_OPTIONS,
DEFAULT_RESOLVE_OPTION,
],
env_inherit=PYTHON_BOOTSTRAP_ENV,
)
rule_runner.set_options([], env_inherit=PYTHON_BOOTSTRAP_ENV)
return rule_runner


Expand Down
Loading

0 comments on commit 561cfab

Please sign in to comment.