Skip to content

Commit 44e9993

Browse files
authored
Merge pull request #166 from olafurpg/pkg-info
Enable cross-repo navigation.
2 parents 73c194c + e9df271 commit 44e9993

File tree

45 files changed

+1304
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1304
-127
lines changed

build.sbt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ lazy val V =
1515
def scala213 = "2.13.4"
1616
def scala212 = "2.12.12"
1717
def scalameta = "4.4.8"
18+
def requests = "0.6.5"
1819
}
1920

2021
inThisBuild(
@@ -102,7 +103,7 @@ lazy val plugin = project
102103
fatjarPackageSettings,
103104
javaOnlySettings,
104105
moduleName := "semanticdb-javac",
105-
javaToolchainVersion := "1.8",
106+
javaToolchainVersion := "8",
106107
assemblyShadeRules.in(assembly) :=
107108
Seq(
108109
ShadeRule
@@ -120,7 +121,7 @@ lazy val lsif = project
120121
.in(file("lsif-semanticdb"))
121122
.settings(
122123
moduleName := "lsif-semanticdb",
123-
javaToolchainVersion := "1.8",
124+
javaToolchainVersion := "8",
124125
javaOnlySettings,
125126
libraryDependencies +=
126127
"com.google.protobuf" % "protobuf-java-util" % V.protobuf,
@@ -146,6 +147,9 @@ lazy val cli = project
146147
buildInfoPackage := "com.sourcegraph.lsif_java",
147148
libraryDependencies ++=
148149
List(
150+
"io.get-coursier" %% "coursier" % V.coursier,
151+
"org.scala-lang.modules" %% "scala-xml" % "2.0.0-RC1",
152+
"com.lihaoyi" %% "requests" % V.requests,
149153
"org.scalameta" %% "moped" % V.moped,
150154
"org.scalameta" %% "ascii-graphs" % "0.1.2"
151155
),
@@ -299,7 +303,6 @@ lazy val bench = project
299303
.in(file("tests/benchmarks"))
300304
.settings(
301305
moduleName := "lsif-java-bench",
302-
javaToolchainVersion := "1.8",
303306
fork.in(run) := true,
304307
skip.in(publish) := true
305308
)
41.6 KB
Binary file not shown.

lsif-java/src/main/scala/com/sourcegraph/io/DeleteVisitor.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import java.nio.file.attribute.BasicFileAttributes
99

1010
class DeleteVisitor(deleteFile: Path => Boolean = _ => true)
1111
extends SimpleFileVisitor[Path] {
12+
override def preVisitDirectory(
13+
dir: Path,
14+
attrs: BasicFileAttributes
15+
): FileVisitResult = {
16+
if (!deleteFile(dir))
17+
FileVisitResult.SKIP_SUBTREE
18+
else
19+
super.preVisitDirectory(dir, attrs)
20+
}
1221
override def visitFile(
1322
file: Path,
1423
attrs: BasicFileAttributes

tests/unit/src/main/scala/tests/Dependencies.scala renamed to lsif-java/src/main/scala/com/sourcegraph/lsif_java/Dependencies.scala

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package tests
1+
package com.sourcegraph.lsif_java
22

33
import java.io.File
4-
import java.nio.file.FileSystems
4+
import java.nio.file.Path
55

66
import scala.concurrent.duration.Duration
77
import scala.xml.XML
88

9-
import scala.meta.io.AbsolutePath
10-
9+
import com.sourcegraph.lsif_java.BuildInfo
1110
import coursier.Fetch
11+
import coursier.Repositories
1212
import coursier.Resolve
1313
import coursier.cache.Cache
1414
import coursier.cache.CachePolicy
@@ -18,9 +18,12 @@ import coursier.parse.DependencyParser
1818
import coursier.util.Task
1919

2020
case class Dependencies(
21-
sources: Seq[AbsolutePath],
22-
classpath: Seq[AbsolutePath]
21+
dependencies: List[Dependency],
22+
sourcesResult: Fetch.Result,
23+
classpathResult: Fetch.Result
2324
) {
25+
val sources: Seq[Path] = sourcesResult.files.map(_.toPath())
26+
val classpath: Seq[Path] = classpathResult.files.map(_.toPath())
2427
def classpathSyntax: String = classpath.mkString(File.pathSeparator)
2528
}
2629

@@ -30,34 +33,46 @@ object Dependencies {
3033
.withCachePolicies(List(CachePolicy.LocalOnly, CachePolicy.Update))
3134
.withTtl(Duration.Inf)
3235
.withChecksums(Nil)
33-
private val jarPattern = FileSystems.getDefault.getPathMatcher("glob:**.jar")
36+
private val defaultExtraRepositories = List[Repository](
37+
Repositories.google,
38+
Repositories.clojars,
39+
Repositories.jitpack,
40+
Repositories.centralGcs
41+
)
3442

3543
def resolveDependencies(
3644
dependencies: List[String],
37-
repos: List[Repository]
45+
transitive: Boolean = true
3846
): Dependencies = {
3947
val deps = dependencies.map(parseDependency)
40-
val provided = deps.flatMap(d => resolveProvidedDeps(d, repos))
48+
val provided = deps.flatMap(d => resolveProvidedDeps(d))
49+
def nonTransitiveDeps = deps.map(_.withTransitive(false))
4150
val fetch = Fetch[Task](Cache.default)
4251
.addDependencies(deps: _*)
4352
.addDependencies(provided: _*)
44-
.addRepositories(repos: _*)
53+
.addRepositories(defaultExtraRepositories: _*)
4554

46-
val classpath = fetch.run()
47-
val sources = fetch.withClassifiers(Set(Classifier.sources)).run()
55+
val classpath = fetch.runResult()
56+
val sources = fetch
57+
.withDependencies(
58+
if (transitive)
59+
fetch.dependencies
60+
else
61+
nonTransitiveDeps
62+
)
63+
.withClassifiers(Set(Classifier.sources))
64+
.runResult()
4865
Dependencies(
49-
sources = sources.map(AbsolutePath(_)),
50-
classpath = classpath.map(AbsolutePath(_))
66+
dependencies = deps,
67+
sourcesResult = sources,
68+
classpathResult = classpath
5169
)
5270
}
5371

54-
private def resolveProvidedDeps(
55-
dep: Dependency,
56-
repos: List[Repository]
57-
): Seq[Dependency] = {
72+
def resolveProvidedDeps(dep: Dependency): Seq[Dependency] = {
5873
val artifacts = Resolve[Task](Cache.default)
5974
.addDependencies(dep)
60-
.addRepositories(repos: _*)
75+
.addRepositories(defaultExtraRepositories: _*)
6176
.run()
6277
.artifacts()
6378
for {
@@ -83,10 +98,13 @@ object Dependencies {
8398
)
8499
}.toList
85100

86-
private def parseDependency(lib: String): Dependency = {
87-
val dep = DependencyParser
101+
def parseDependencyEither(lib: String): Either[String, Dependency] = {
102+
DependencyParser
88103
.dependency(lib, defaultScalaVersion = BuildInfo.scalaVersion)
89-
dep match {
104+
}
105+
106+
private def parseDependency(lib: String): Dependency = {
107+
parseDependencyEither(lib) match {
90108
case Left(error) =>
91109
throw new IllegalArgumentException(error)
92110
case Right(value) =>

lsif-java/src/main/scala/com/sourcegraph/lsif_java/Embedded.scala

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
package com.sourcegraph.lsif_java
2+
23
import java.nio.charset.StandardCharsets
34
import java.nio.file.Files
45
import java.nio.file.Path
56
import java.nio.file.StandardCopyOption
67

8+
import moped.reporters.Reporter
9+
import os.CommandResult
10+
711
object Embedded {
812

913
def semanticdbJar(tmpDir: Path): Path =
1014
copyFile(tmpDir, "semanticdb-plugin.jar")
1115

1216
def agentJar(tmpDir: Path): Path = copyFile(tmpDir, "semanticdb-agent.jar")
17+
def coursier(tmpDir: Path): Path = {
18+
val result = copyFile(tmpDir, "lsif-java/coursier")
19+
result.toFile().setExecutable(true)
20+
result
21+
}
22+
23+
private def javacErrorpath(tmp: Path) = tmp.resolve("errorpath.txt")
1324

1425
def customJavac(sourceroot: Path, targetroot: Path, tmp: Path): Path = {
1526
val javac = tmp.resolve("javac")
1627
val pluginpath = Embedded.semanticdbJar(tmp)
17-
val oldArguments = tmp.resolve("javac_oldarguments")
18-
val newArguments = tmp.resolve("javac_newarguments")
19-
val launcherArgs = tmp.resolve("javac_launcherarguments")
28+
val errorpath = javacErrorpath(tmp)
29+
val javacopts = targetroot.resolve("javacopts.txt")
30+
Files.createDirectories(targetroot)
31+
val newJavacopts = tmp.resolve("javac_newarguments")
2032
val injectSemanticdbArguments = List[String](
2133
"java",
34+
s"-Dsemanticdb.errorpath=$errorpath",
2235
s"-Dsemanticdb.pluginpath=$pluginpath",
2336
s"-Dsemanticdb.sourceroot=$sourceroot",
2437
s"-Dsemanticdb.targetroot=$targetroot",
25-
s"-Dsemanticdb.output=$newArguments",
26-
s"-Dsemanticdb.old-output=$oldArguments",
38+
s"-Dsemanticdb.output=$$NEW_JAVAC_OPTS",
39+
s"-Dsemanticdb.old-output=$javacopts",
2740
s"-classpath $pluginpath",
2841
"com.sourcegraph.semanticdb_javac.InjectSemanticdbOptions",
2942
""""$@""""
@@ -32,27 +45,53 @@ object Embedded {
3245
s"""#!/usr/bin/env bash
3346
|set -eu
3447
|LAUNCHER_ARGS=()
35-
|echo $$@ >> $launcherArgs
48+
|NEW_JAVAC_OPTS="$newJavacopts-$$RANDOM"
3649
|for arg in "$$@"; do
3750
| if [[ $$arg == -J* ]]; then
3851
| LAUNCHER_ARGS+=("$$arg")
3952
| fi
4053
|done
4154
|$injectSemanticdbArguments
4255
|if [ $${#LAUNCHER_ARGS[@]} -eq 0 ]; then
43-
| javac "@$newArguments"
56+
| javac "@$$NEW_JAVAC_OPTS"
4457
|else
45-
| javac "@$newArguments" "$${LAUNCHER_ARGS[@]}"
58+
| javac "@$$NEW_JAVAC_OPTS" "$${LAUNCHER_ARGS[@]}"
4659
|fi
4760
|""".stripMargin
4861
Files.write(javac, script.getBytes(StandardCharsets.UTF_8))
4962
javac.toFile.setExecutable(true)
5063
javac
5164
}
5265

66+
/**
67+
* The custom javac wrapper reports errors to a specific file if unexpected
68+
* errors happen. The javac wrapper gets invoked by builds tools like
69+
* Gradle/Maven, which hide the actual errors from the script because they
70+
* assume the standard output is from javac. This file is used a side-channel
71+
* to avoid relying on the error reporting from Gradle/Maven.
72+
*/
73+
def reportUnexpectedJavacErrors(
74+
reporter: Reporter,
75+
tmp: Path
76+
): Option[CommandResult] = {
77+
val errorpath = javacErrorpath(tmp)
78+
if (Files.isRegularFile(errorpath)) {
79+
reporter.error("unexpected javac compile errors")
80+
Files
81+
.readAllLines(errorpath)
82+
.forEach { line =>
83+
reporter.error(line)
84+
}
85+
Some(CommandResult(1, Nil))
86+
} else {
87+
None
88+
}
89+
}
90+
5391
private def copyFile(tmpDir: Path, filename: String): Path = {
5492
val in = this.getClass.getResourceAsStream(s"/$filename")
5593
val out = tmpDir.resolve(filename)
94+
Files.createDirectories(out.getParent())
5695
try Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING)
5796
finally in.close()
5897
out

lsif-java/src/main/scala/com/sourcegraph/lsif_java/buildtools/BuildTool.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ abstract class BuildTool(val name: String, index: IndexCommand) {
1212

1313
protected def defaultTargetroot: Path
1414

15+
def isHidden: Boolean = false
16+
17+
def indexJdk(): Boolean = true
18+
1519
final def targetroot: Path =
1620
AbsolutePath
1721
.of(index.targetroot.getOrElse(defaultTargetroot), index.workingDirectory)
@@ -24,5 +28,11 @@ abstract class BuildTool(val name: String, index: IndexCommand) {
2428

2529
object BuildTool {
2630
def all(index: IndexCommand): List[BuildTool] =
27-
List(new GradleBuildTool(index), new MavenBuildTool(index))
31+
List(
32+
new GradleBuildTool(index),
33+
new MavenBuildTool(index),
34+
new LsifBuildTool(index)
35+
)
36+
def allNames: String =
37+
all(IndexCommand()).filterNot(_.isHidden).map(_.name).mkString(", ")
2838
}

0 commit comments

Comments
 (0)