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

add scala_platform to export output for metals! #8370

Merged
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
13 changes: 13 additions & 0 deletions src/docs/export.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ The following is an abbreviated export file from a command in the pants repo:
},
"default_interpreter": "CPython-2.7.10"
},
"scala_platform": {
"scala_version": "2.12",
"compiler_classpath": [
"/Users/dmcclanahan/tools/pants-v4/.pants.d/bootstrap/bootstrap-jvm-tools/a0ebe8e0b001/ivy/jars/org.scala-lang/scala-compiler/jars/scala-compiler-2.12.8.jar",
"/Users/dmcclanahan/tools/pants-v4/.pants.d/bootstrap/bootstrap-jvm-tools/a0ebe8e0b001/ivy/jars/org.scala-lang/scala-library/jars/scala-library-2.12.8.jar",
"/Users/dmcclanahan/tools/pants-v4/.pants.d/bootstrap/bootstrap-jvm-tools/a0ebe8e0b001/ivy/jars/org.scala-lang/scala-reflect/jars/scala-reflect-2.12.8.jar",
"/Users/dmcclanahan/tools/pants-v4/.pants.d/bootstrap/bootstrap-jvm-tools/a0ebe8e0b001/ivy/jars/org.scala-lang.modules/scala-xml_2.12/jars/scala-xml_2.12-1.0.6.jar"
]
},
"targets": {
"examples/tests/java/org/pantsbuild/example/usethrift:usethrift": {
"is_code_gen": false,
Expand Down Expand Up @@ -168,6 +177,10 @@ The following is an abbreviated export file from a command in the pants repo:

# Export Format Changes

## 1.0.11

The 'scala_platform' field is added to the top-level keys, containing the 'scala_version' string (without patch version, e.g. "2.12") and the 'compiler_classpath' jars (a list of absolute paths to jar files).

## 1.0.10

Coursier is added to be an option for the resolve path which ignores the confs for library sources and javadoc yet.
Expand Down
13 changes: 12 additions & 1 deletion src/python/pants/backend/project_info/tasks/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pants.backend.jvm.subsystems.jvm_platform import JvmPlatform
from pants.backend.jvm.subsystems.resolve_subsystem import JvmResolveSubsystem
from pants.backend.jvm.subsystems.scala_platform import ScalaPlatform
from pants.backend.jvm.targets.jar_library import JarLibrary
from pants.backend.jvm.targets.junit_tests import JUnitTests
from pants.backend.jvm.targets.jvm_app import JvmApp
Expand Down Expand Up @@ -55,7 +56,7 @@ class ExportTask(ResolveRequirementsTaskBase, IvyTaskMixin, CoursierMixin):
#
# Note format changes in src/docs/export.md and update the Changelog section.
#
DEFAULT_EXPORT_VERSION = '1.0.10'
DEFAULT_EXPORT_VERSION = '1.0.11'

@classmethod
def subsystem_dependencies(cls):
Expand Down Expand Up @@ -282,6 +283,15 @@ def iter_transitive_jars(jar_lib):
for target in targets:
process_target(target)

scala_platform = ScalaPlatform.global_instance()
scala_platform_map = {
'scala_version': scala_platform.version,
'compiler_classpath': [
cp_entry.path
for cp_entry in scala_platform.compiler_classpath_entries(self.context.products)
],
}

jvm_platforms_map = {
'default_platform' : JvmPlatform.global_instance().default_platform.name,
'platforms': {
Expand All @@ -296,6 +306,7 @@ def iter_transitive_jars(jar_lib):
'version': self.DEFAULT_EXPORT_VERSION,
'targets': targets_map,
'jvm_platforms': jvm_platforms_map,
'scala_platform': scala_platform_map,
# `jvm_distributions` are static distribution settings from config,
# `preferred_jvm_distributions` are distributions that pants actually uses for the
# given platform setting.
Expand Down
71 changes: 66 additions & 5 deletions tests/python/pants_test/backend/project_info/tasks/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pants.backend.jvm.targets.jvm_binary import JvmBinary
from pants.backend.jvm.targets.jvm_target import JvmTarget
from pants.backend.jvm.targets.scala_library import ScalaLibrary
from pants.backend.jvm.tasks.bootstrap_jvm_tools import BootstrapJvmTools
from pants.backend.jvm.tasks.classpath_products import ClasspathProducts
from pants.backend.project_info.tasks.export import Export
from pants.backend.python.register import build_file_aliases as register_python
Expand All @@ -45,6 +46,9 @@ def task_type(cls):
def alias_groups(cls):
return register_core().merge(register_jvm()).merge(register_python())

# Version of the scala compiler and libraries used for this test.
_scala_toolchain_version = '2.10.5'

def setUp(self):
super().setUp()

Expand All @@ -56,10 +60,40 @@ def setUp(self):
}
init_subsystems([JUnit, ScalaPlatform, ScoveragePlatform], scala_options)

self.make_target(
':jar-tool',
JarLibrary,
jars=[JarDependency('org.pantsbuild', 'jar-tool', '0.0.10')]
)

self.make_target(
':jarjar',
JarLibrary,
jars=[JarDependency(org='org.pantsbuild', name='jarjar', rev='1.7.2')]
)

self.make_target(
':scala-library',
JarLibrary,
jars=[JarDependency('org.scala-lang', 'scala-library', '2.10.5')]
jars=[JarDependency('org.scala-lang', 'scala-library', self._scala_toolchain_version)]
)

self.make_target(
':scalac',
JarLibrary,
jars=[JarDependency('org.scala-lang', 'scala-compiler', self._scala_toolchain_version)]
)

self.make_target(
':scala-reflect',
JarLibrary,
jars=[JarDependency('org.scala-lang', 'scala-reflect', self._scala_toolchain_version)]
)

self.make_target(
':scala-repl',
JarLibrary,
jars=[JarDependency('org.scala-lang', 'scala-repl', self._scala_toolchain_version)]
)

self.make_target(
Expand Down Expand Up @@ -181,17 +215,20 @@ def setUp(self):

self.add_to_build_file('src/python/has_reqs/BUILD', textwrap.dedent("""
python_library(name="has_reqs", sources=globs("*.py"), dependencies=[':six'])

python_requirement_library(
name='six',
requirements=[
python_requirement('six==1.9.0')
]
)
)
"""))

def execute_export(self, *specs, **options_overrides):
options = {
ScalaPlatform.options_scope: {
'version': 'custom'
},
JvmResolveSubsystem.options_scope: {
'resolver': 'ivy'
},
Expand All @@ -204,10 +241,14 @@ def execute_export(self, *specs, **options_overrides):
}
options.update(options_overrides)

BootstrapJvmTools.options_scope = 'bootstrap-jvm-tools'
context = self.context(options=options, target_roots=[self.target(spec) for spec in specs],
for_subsystems=[JvmPlatform])
for_subsystems=[JvmPlatform],
for_task_types=[BootstrapJvmTools])
context.products.safe_create_data('compile_classpath',
init_func=ClasspathProducts.init_func(self.pants_workdir))
bootstrap_task = BootstrapJvmTools(context, self.pants_workdir)
bootstrap_task.execute()
task = self.create_task(context)
return list(task.console_output(list(task.context.targets()),
context.products.get_data('compile_classpath')))
Expand Down Expand Up @@ -249,7 +290,27 @@ def test_without_dependencies(self):
def test_version(self):
result = self.execute_export_json('project_info:first')
# If you have to update this test, make sure export.md is updated with changelog notes
self.assertEqual('1.0.10', result['version'])
self.assertEqual('1.0.11', result['version'])

def test_scala_platform_custom(self):
result = self.execute_export_json('project_info:first')
scala_platform = result['scala_platform']
scala_version = scala_platform['scala_version']
self.assertEqual(scala_version, 'custom')
scala_jars = scala_platform['compiler_classpath']
self.assertTrue(any(self._scala_toolchain_version in jar_path for jar_path in scala_jars))

def test_scala_platform_standard(self):
result = self.execute_export_json('project_info:first', **{
ScalaPlatform.options_scope: {
'version': '2.12'
}
})
scala_platform = result['scala_platform']
scala_version = scala_platform['scala_version']
self.assertEqual(scala_version, '2.12')
scala_jars = scala_platform['compiler_classpath']
self.assertTrue(any('2.12' in jar_path for jar_path in scala_jars))

def test_sources(self):
self.set_options(sources=True)
Expand Down