Skip to content

Commit 9fccc25

Browse files
Port DependencyAnalyzer plugin to Scala 3 (bazel-contrib#1640)
* Adapt DependencyAnalyzer/OptionsParsers.tokenize tailrec to a bug when compiling under Scala 3.1.3 (fixed in 3.2) * Setup dt_patches for Scala 3 * Port dependency-analyzer compiler plugin, setup and unify tests. * Add .bazelbsp and .bsp to .gitignore * Add test_thridparty_version.sh as (actually working) subset of it's dangerous counterpart * Show errors of failing tests * Use Java17 for test_thirdparty_version - latest supported JDK version by Scala 3.1.3 is JDK 18 * Remove debug printlns * Simplify resolving version of Scala 3 compiler
1 parent 716210f commit 9fccc25

File tree

34 files changed

+1137
-496
lines changed

34 files changed

+1137
-496
lines changed

.bazelci/presubmit.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ tasks:
8080
platform: macos
8181
shell_commands:
8282
- "./test_version.sh"
83+
thirdparty_version_linux:
84+
name: "./test_thirdparty_version.sh"
85+
platform: ubuntu2204_java17
86+
shell_commands:
87+
- "./test_thirdparty_version.sh"
8388
examples_linux:
8489
name: "./test_examples"
8590
platform: ubuntu2004

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ hash2
66
.DS_store
77
.bazel_cache
88
.ijwb
9+
.bazelbsp
10+
.bsp
911
.metals
1012
.vscode
1113
unformatted-*.backup.scala

dt_patches/dt_compiler_3.patch

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
diff --git a/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala b/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala
3+
--- dotty/tools/dotc/core/SymbolLoaders.scala (revision 91ef92159c628eaeab8311dc82bed7ed4fe03c63)
4+
+++ dotty/tools/dotc/core/SymbolLoaders.scala (date 1730331517451)
5+
@@ -353,6 +353,14 @@
6+
try {
7+
val start = System.currentTimeMillis
8+
trace.onDebug("loading") {
9+
+ root.symbol.associatedFile match {
10+
+ case assocFile: AbstractFile => assocFile.underlyingSource.foreach {
11+
+ case jar: dotty.tools.io.ZipArchive =>
12+
+ report.echo(s"DT:${jar.path}")
13+
+ case _ => ()
14+
+ }
15+
+ case null => ()
16+
+ }
17+
doComplete(root)
18+
}
19+
report.informTime("loaded " + description, start)

dt_patches/dt_patch_test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ run_test_local test_compiler_patch 2.13.12
116116
run_test_local test_compiler_patch 2.13.14
117117
run_test_local test_compiler_patch 2.13.15
118118

119+
run_test_local test_compiler_patch 3.1.0 # Minimal supported version
120+
run_test_local test_compiler_patch 3.1.3
121+
run_test_local test_compiler_patch 3.2.2
122+
run_test_local test_compiler_patch 3.3.4
123+
run_test_local test_compiler_patch 3.4.3
124+
run_test_local test_compiler_patch 3.5.2
125+
119126
run_test_local test_compiler_srcjar_error 2.12.11
120127
run_test_local test_compiler_srcjar_error 2.12.12
121128
run_test_local test_compiler_srcjar_error 2.12.13
@@ -139,3 +146,9 @@ run_test_local test_compiler_srcjar_nonhermetic 2.13.11
139146
run_test_local test_compiler_srcjar_nonhermetic 2.13.12
140147
run_test_local test_compiler_srcjar_nonhermetic 2.13.14
141148
run_test_local test_compiler_srcjar_nonhermetic 2.13.15
149+
150+
run_test_local test_compiler_srcjar 3.1.3
151+
run_test_local test_compiler_srcjar 3.2.2
152+
run_test_local test_compiler_srcjar_nonhermetic 3.3.4
153+
run_test_local test_compiler_srcjar 3.4.3
154+
run_test_local test_compiler_srcjar_nonhermetic 3.5.2

dt_patches/test_dt_patches/BUILD

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ load(
22
"@io_bazel_rules_scala//scala:scala.bzl",
33
"setup_scala_toolchain",
44
)
5+
load("@io_bazel_rules_scala//scala:scala_cross_version_select.bzl", "select_for_scala_version")
56

6-
SCALA_LIBS = [
7-
"@scala_library",
8-
"@scala_reflect",
9-
]
7+
SCALA_LIBS = ["@scala_library"] + select_for_scala_version(
8+
any_2 = ["@scala_reflect"],
9+
any_3 = [
10+
"@scala3_interfaces",
11+
"@scala2_library",
12+
"@tasty_core",
13+
"@scala_asm",
14+
],
15+
since_3_4 = ["@sbt_compiler_interface"],
16+
)
1017

1118
setup_scala_toolchain(
1219
name = "dt_scala_toolchain",

dt_patches/test_dt_patches/WORKSPACE

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,62 @@ load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
4848

4949
scala_maven_import_external(
5050
name = "scala_library",
51-
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION,
51+
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
5252
licenses = ["notice"],
5353
server_urls = default_maven_server_urls(),
5454
)
5555

5656
scala_maven_import_external(
5757
name = "scala_compiler",
58-
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION,
58+
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
5959
licenses = ["notice"],
6060
server_urls = default_maven_server_urls(),
6161
)
6262

63+
# Scala 2 only
6364
scala_maven_import_external(
6465
name = "scala_reflect",
6566
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
6667
licenses = ["notice"],
6768
server_urls = default_maven_server_urls(),
6869
)
6970

71+
# Scala 3 only
72+
scala_maven_import_external(
73+
name = "scala3_interfaces",
74+
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
75+
licenses = ["notice"],
76+
server_urls = default_maven_server_urls(),
77+
)
78+
79+
scala_maven_import_external(
80+
name = "scala2_library",
81+
artifact = "org.scala-lang:scala-library:2.13.15",
82+
licenses = ["notice"],
83+
server_urls = default_maven_server_urls(),
84+
)
85+
86+
scala_maven_import_external(
87+
name = "tasty_core",
88+
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
89+
licenses = ["notice"],
90+
server_urls = default_maven_server_urls(),
91+
)
92+
93+
scala_maven_import_external(
94+
name = "scala_asm",
95+
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
96+
licenses = ["notice"],
97+
server_urls = default_maven_server_urls(),
98+
)
99+
100+
scala_maven_import_external(
101+
name = "sbt_compiler_interface",
102+
artifact = "org.scala-sbt:compiler-interface:1.9.6",
103+
licenses = ["notice"],
104+
server_urls = default_maven_server_urls(),
105+
)
106+
70107
rules_scala_setup()
71108

72109
rules_scala_toolchain_deps_repositories(

dt_patches/test_dt_patches_user_srcjar/BUILD

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ load(
22
"@io_bazel_rules_scala//scala:scala.bzl",
33
"setup_scala_toolchain",
44
)
5+
load("@io_bazel_rules_scala//scala:scala_cross_version_select.bzl", "select_for_scala_version")
56

6-
SCALA_LIBS = [
7-
"@scala_library",
8-
"@scala_reflect",
9-
]
7+
SCALA_LIBS = ["@scala_library"] + select_for_scala_version(
8+
any_2 = ["@scala_reflect"],
9+
any_3 = [
10+
"@scala3_interfaces",
11+
"@scala2_library",
12+
"@tasty_core",
13+
"@scala_asm",
14+
],
15+
since_3_4 = ["@sbt_compiler_interface"],
16+
)
1017

1118
setup_scala_toolchain(
1219
name = "dt_scala_toolchain",

dt_patches/test_dt_patches_user_srcjar/WORKSPACE

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,70 @@ http_jar(
5252
url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.16/scala-compiler-2.12.16-sources.jar",
5353
)
5454

55+
http_jar(
56+
name = "scala3_compiler_srcjar",
57+
sha256 = "3c413efa9a2921ef59da7f065c445ae1b6b97057cbbc6b16957ad052a575a3ce",
58+
url = "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.4.3/scala3-compiler_3-3.4.3-sources.jar",
59+
)
60+
5561
scala_maven_import_external(
5662
name = "scala_library",
57-
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION,
63+
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
5864
licenses = ["notice"],
5965
server_urls = default_maven_server_urls(),
6066
)
6167

6268
scala_maven_import_external(
6369
name = "scala_compiler",
64-
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION,
70+
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
6571
licenses = ["notice"],
6672
server_urls = default_maven_server_urls(),
6773
)
6874

75+
# Scala 2 only
6976
scala_maven_import_external(
7077
name = "scala_reflect",
7178
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
7279
licenses = ["notice"],
7380
server_urls = default_maven_server_urls(),
7481
)
7582

83+
# Scala 3 only
84+
scala_maven_import_external(
85+
name = "scala3_interfaces",
86+
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
87+
licenses = ["notice"],
88+
server_urls = default_maven_server_urls(),
89+
)
90+
91+
scala_maven_import_external(
92+
name = "scala2_library",
93+
artifact = "org.scala-lang:scala-library:2.13.15",
94+
licenses = ["notice"],
95+
server_urls = default_maven_server_urls(),
96+
)
97+
98+
scala_maven_import_external(
99+
name = "tasty_core",
100+
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
101+
licenses = ["notice"],
102+
server_urls = default_maven_server_urls(),
103+
)
104+
105+
scala_maven_import_external(
106+
name = "scala_asm",
107+
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
108+
licenses = ["notice"],
109+
server_urls = default_maven_server_urls(),
110+
)
111+
112+
scala_maven_import_external(
113+
name = "sbt_compiler_interface",
114+
artifact = "org.scala-sbt:compiler-interface:1.9.6",
115+
licenses = ["notice"],
116+
server_urls = default_maven_server_urls(),
117+
)
118+
76119
srcjars_by_version = {
77120
# Invalid
78121
"2.12.11": [],
@@ -120,6 +163,23 @@ srcjars_by_version = {
120163
"2.13.15": {
121164
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.15/scala-compiler-2.13.15-sources.jar?foo",
122165
},
166+
"3.1.3": {
167+
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.1.3/scala3-compiler_3-3.1.3-sources.jar",
168+
"integrity": "sha384-4J2ihR1QSdP5cvL3y2OUfw4uUX/hsQqcPlJV+IrQdsM/soiIAYfoEeIEt6vl3xBk",
169+
},
170+
"3.2.2": {
171+
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.2.2/scala3-compiler_3-3.2.2-sources.jar",
172+
"sha256": "669d580fc4a8d3c2e2d13d5735ae9be05d567613fe44482de5bcc5e2e2ee89ea",
173+
},
174+
"3.3.4": {
175+
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.3.4/scala3-compiler_3-3.3.4-sources.jar",
176+
},
177+
"3.4.3": {
178+
"label": "@scala3_compiler_srcjar//jar:downloaded.jar",
179+
},
180+
"3.5.2": {
181+
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.5.2/scala3-compiler_3-3.5.2-sources.jar",
182+
},
123183
}
124184

125185
rules_scala_setup(scala_compiler_srcjar = srcjars_by_version[SCALA_VERSION])

scala/private/macros/scala_repositories.bzl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,21 @@ def dt_patched_compiler_setup(scala_version, scala_compiler_srcjar = None):
7575

7676
if scala_major_version == "2.12":
7777
if minor_version >= 1 and minor_version <= 7:
78-
patch = Label(
79-
"//dt_patches:dt_compiler_%s.1.patch" % scala_major_version,
80-
)
78+
patch = Label("//dt_patches:dt_compiler_%s.1.patch" % scala_major_version)
8179
elif minor_version <= 11:
82-
patch = Label(
83-
"//dt_patches:dt_compiler_%s.8.patch" % scala_major_version,
84-
)
80+
patch = Label("//dt_patches:dt_compiler_%s.8.patch" % scala_major_version)
81+
elif scala_major_version.startswith("3."):
82+
patch = Label("//dt_patches:dt_compiler_3.patch")
8583

8684
build_file_content = "\n".join([
8785
"package(default_visibility = [\"//visibility:public\"])",
8886
"filegroup(",
8987
" name = \"src\",",
90-
" srcs=[\"scala/tools/nsc/symtab/SymbolLoaders.scala\"],",
88+
" srcs=[\"scala/tools/nsc/symtab/SymbolLoaders.scala\"]," if scala_major_version.startswith("2.") else " srcs=[\"dotty/tools/dotc/core/SymbolLoaders.scala\"],",
9189
")",
9290
])
9391
default_scalac_srcjar = {
94-
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/%s/scala-compiler-%s-sources.jar" % (scala_version, scala_version),
92+
"url": "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/%s/scala-compiler-%s-sources.jar" % (scala_version, scala_version) if scala_major_version.startswith("2.") else "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/%s/scala3-compiler_3-%s-sources.jar" % (scala_version, scala_version),
9593
}
9694
srcjar = scala_compiler_srcjar if scala_compiler_srcjar != None else default_scalac_srcjar
9795
_validate_scalac_srcjar(srcjar) or fail(

src/java/io/bazel/rulesscala/scalac/BUILD

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ java_binary(
2424
],
2525
main_class = "io.bazel.rulesscala.scalac.ScalacWorker",
2626
visibility = ["//visibility:public"],
27-
deps = (select_for_scala_version(
28-
any_2 = [
29-
"//third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/compiler:dep_reporting_compiler",
30-
],
31-
) if ENABLE_COMPILER_DEPENDENCY_TRACKING else []) + SCALAC_DEPS,
27+
deps = ([
28+
"//third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/compiler:dep_reporting_compiler",
29+
] if ENABLE_COMPILER_DEPENDENCY_TRACKING else []) + SCALAC_DEPS,
3230
)
3331

3432
java_binary(

test_thirdparty_version.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
test_scala_version() {
6+
SCALA_VERSION=$1
7+
bazel test --test_output=errors //third_party/... --repo_env=SCALA_VERSION=${SCALA_VERSION}
8+
}
9+
10+
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
11+
test_dir=$dir/test/shell
12+
# shellcheck source=./test_runner.sh
13+
. "${test_dir}"/test_runner.sh
14+
runner=$(get_test_runner "${1:-local}")
15+
16+
17+
# Latest version of each major version
18+
$runner test_scala_version "3.5.2" # Latest Next version
19+
$runner test_scala_version "3.3.4" # Latest LTS version
20+
$runner test_scala_version "3.1.3" # First supported major for Scala 3, max supported JDK=18
21+
$runner test_scala_version "2.13.15"
22+
$runner test_scala_version "2.12.20"
23+
$runner test_scala_version "2.11.12"
24+
25+
# Tests for other versions should be placed in dangerous_test_thirdparty_version.sh
26+
# However that script is outdated and uses only default Scala version for each minor

0 commit comments

Comments
 (0)