-
Notifications
You must be signed in to change notification settings - Fork 282
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
Justinass/dep analyzer #1370
Merged
simuons
merged 10 commits into
bazelbuild:master
from
justinass-wix:justinass/dep-analyzer
Mar 22, 2022
Merged
Justinass/dep analyzer #1370
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09d9325
dep analyzer 3
justinass-wix 1e91cd6
dep analyzer 3
justinass-wix 701d16f
dep analyzer 3
justinass-wix aa54377
dep analyzer 3
justinass-wix 848cbee
dep analyzer 3
justinass-wix 0a0caec
dep analyzer 3
justinass-wix 6001470
Cleanup
justinass-wix d705119
Commend about including Scala 2 standard library
justinass-wix a82ac94
Trigger rebuild
justinass-wix 3a16b44
Trigger rebuild
justinass-wix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
load(":analyzer_test.bzl", "analyzer_tests") | ||
load(":analyzer_test.bzl", "tests") | ||
|
||
analyzer_tests() | ||
tests() |
104 changes: 6 additions & 98 deletions
104
third_party/dependency_analyzer/src/test/analyzer_test.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,9 @@ | ||
load("//scala:scala.bzl", "scala_test") | ||
load(":analyzer_test_scala_2.bzl", "analyzer_tests_scala_2") | ||
load(":analyzer_test_scala_3.bzl", "analyzer_tests_scala_3") | ||
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_MAJOR_VERSION") | ||
|
||
def analyzer_tests(): | ||
def tests(): | ||
if SCALA_MAJOR_VERSION.startswith("2"): | ||
common_jvm_flags = [ | ||
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)", | ||
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)", | ||
"-Dscala.reflect.location=$(rootpath @io_bazel_rules_scala_scala_reflect)", | ||
] | ||
|
||
scala_test( | ||
name = "ast_used_jar_finder_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinderTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags, | ||
deps = [ | ||
"//src/java/io/bazel/rulesscala/io_utils", | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/dependency_analyzer/src/main:scala_version", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "scala_version_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/ScalaVersionTest.scala", | ||
], | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:scala_version", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "scalac_dependency_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/ScalacDependencyTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags, | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//src/java/io/bazel/rulesscala/io_utils", | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "strict_deps_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/StrictDepsTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags + [ | ||
"-Dguava.jar.location=$(rootpath @com_google_guava_guava_21_0_with_file//jar)", | ||
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)", | ||
], | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@com_google_guava_guava_21_0_with_file//jar", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "unused_dependency_checker_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/UnusedDependencyCheckerTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags + [ | ||
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)", | ||
], | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file", | ||
], | ||
) | ||
analyzer_tests_scala_2() | ||
else: | ||
analyzer_tests_scala_3() |
99 changes: 99 additions & 0 deletions
99
third_party/dependency_analyzer/src/test/analyzer_test_scala_2.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
load("//scala:scala.bzl", "scala_test") | ||
|
||
def analyzer_tests_scala_2(): | ||
common_jvm_flags = [ | ||
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)", | ||
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)", | ||
"-Dscala.reflect.location=$(rootpath @io_bazel_rules_scala_scala_reflect)", | ||
] | ||
|
||
scala_test( | ||
name = "ast_used_jar_finder_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinderTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags, | ||
deps = [ | ||
"//src/java/io/bazel/rulesscala/io_utils", | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/dependency_analyzer/src/main:scala_version", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "scala_version_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/ScalaVersionTest.scala", | ||
], | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:scala_version", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "scalac_dependency_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/ScalacDependencyTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags, | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//src/java/io/bazel/rulesscala/io_utils", | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "strict_deps_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/StrictDepsTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags + [ | ||
"-Dguava.jar.location=$(rootpath @com_google_guava_guava_21_0_with_file//jar)", | ||
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)", | ||
], | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@com_google_guava_guava_21_0_with_file//jar", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file", | ||
], | ||
) | ||
|
||
scala_test( | ||
name = "unused_dependency_checker_test", | ||
size = "small", | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer/UnusedDependencyCheckerTest.scala", | ||
], | ||
jvm_flags = common_jvm_flags + [ | ||
"-Dapache.commons.jar.location=$(location @org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file)", | ||
], | ||
unused_dependency_checker_mode = "off", | ||
deps = [ | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_compiler", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_reflect", | ||
"@org_apache_commons_commons_lang_3_5_without_file//:linkable_org_apache_commons_commons_lang_3_5_without_file", | ||
], | ||
) |
27 changes: 27 additions & 0 deletions
27
third_party/dependency_analyzer/src/test/analyzer_test_scala_3.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test") | ||
|
||
def analyzer_tests_scala_3(): | ||
common_jvm_flags = [ | ||
"-Dplugin.jar.location=$(execpath //third_party/dependency_analyzer/src/main:dependency_analyzer)", | ||
"-Dscala.library.location=$(rootpath @io_bazel_rules_scala_scala_library)", | ||
# Scala 2 standard library is required for compilation. | ||
# Without it compilation fails with error: | ||
# class dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to class dotty.tools.dotc.core.Symbols$ClassSymbol | ||
"-Dscala.library2.location=$(rootpath @io_bazel_rules_scala_scala_library_2)", | ||
] | ||
|
||
scala_test( | ||
name = "test_that_tests_run", | ||
size = "small", | ||
jvm_flags = common_jvm_flags, | ||
srcs = [ | ||
"io/bazel/rulesscala/dependencyanalyzer3/CompileTest.scala", | ||
], | ||
deps = [ | ||
"//scala/private/toolchain_deps:scala_compile_classpath", | ||
"//third_party/dependency_analyzer/src/main:dependency_analyzer", | ||
"//third_party/utils/src/test:test_util", | ||
"@io_bazel_rules_scala_scala_library", | ||
"@io_bazel_rules_scala_scala_library_2", | ||
], | ||
) |
24 changes: 24 additions & 0 deletions
24
...ty/dependency_analyzer/src/test/io/bazel/rulesscala/dependencyanalyzer3/CompileTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.bazel.rulesscala.dependencyanalyzer | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import io.bazel.rulesscala.utils.Scala3CompilerUtils | ||
|
||
class CompileTest extends AnyFunSuite { | ||
|
||
test("Util compiles valid code") { | ||
val scalaCode = "class Foo { }" | ||
val messages = Scala3CompilerUtils.runCompiler(scalaCode) | ||
|
||
assert(messages.isEmpty, "No messages must be reported when valid code is compiled") | ||
} | ||
|
||
test("Util returns errors on invalid code") { | ||
val scalaCode = "!@#" | ||
val messages = Scala3CompilerUtils.runCompiler(scalaCode) | ||
|
||
assert( | ||
messages.exists(_.message.contains("Illegal start of toplevel definition")), | ||
"Error must be reported when valid code is compiled" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
third_party/utils/src/test/io/bazel/rulesscala/utils/Scala3CompilerUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.bazel.rulesscala.utils | ||
|
||
import dotty.tools.dotc.Compiler | ||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.reporting.{Diagnostic, StoreReporter} | ||
import dotty.tools.dotc.util.SourceFile | ||
|
||
import java.nio.file.Paths | ||
|
||
object Scala3CompilerUtils { | ||
def runCompiler( | ||
code: String, | ||
extraClasspath: List[String] = List.empty, | ||
): List[Diagnostic] = { | ||
val reporter = new TestReporter() | ||
|
||
implicit val context: FreshContext = (new ContextBase).initialCtx.fresh.setReporter(reporter) | ||
|
||
val fullClassPath = context.settings.classpath.value :: extraClasspath ::: builtinClasspaths.filterNot(_.isEmpty) | ||
|
||
context.setSetting(context.settings.classpath, fullClassPath.mkString(":")) | ||
|
||
val compiler = new Compiler() | ||
val run = compiler.newRun | ||
run.compileSources(SourceFile.virtual("scala_compiler_util_run_code.scala", code, maybeIncomplete = false) :: Nil) | ||
|
||
reporter.storedInfos | ||
} | ||
|
||
private lazy val builtinClasspaths: List[String] = | ||
pathOf("scala.library.location") :: pathOf("scala.library2.location") :: Nil | ||
|
||
private def pathOf(jvmFlag: String) = { | ||
val jar = System.getProperty(jvmFlag) | ||
val libPath = Paths.get(baseDir, jar).toAbsolutePath | ||
libPath.toString | ||
} | ||
|
||
private lazy val baseDir = System.getProperty("user.dir") | ||
} | ||
|
||
class TestReporter extends StoreReporter(null) { | ||
def storedInfos: List[Diagnostic] = if (infos != null) infos.toList else List() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a comment why we need both standard libraries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't actually know why Scala 2 library is required, I tried various things until I got the code to compile... One hint was this line .
Without it compilation fails:
class dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to class dotty.tools.dotc.core.Symbols$ClassSymbol (dotty.tools.dotc.core.Symbols$NoSymbol$ and dotty.tools.dotc.core.Symbols$ClassSymbol are in unnamed module of loader 'app')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment.