Skip to content

Commit 32ff67b

Browse files
authored
Merge pull request #227 from olafurpg/jdk-first
Improve handling of moniker import/exports and the JDK
2 parents ea78804 + 543fb44 commit 32ff67b

File tree

8 files changed

+49
-19
lines changed

8 files changed

+49
-19
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class BuildTool(val name: String, index: IndexCommand) {
1313
protected def defaultTargetroot: Path
1414

1515
def isHidden: Boolean = false
16+
def buildKind: String = ""
1617

1718
final def sourceroot: Path = index.workingDirectory
1819
final def targetroot: Path =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class LsifBuildTool(index: IndexCommand) extends BuildTool("LSIF", index) {
5454
index.workingDirectory.resolve(LsifBuildTool.ConfigFileName)
5555
def usedInCurrentDirectory(): Boolean = Files.isRegularFile(configFile)
5656
override def isHidden: Boolean = true
57+
override def buildKind: String =
58+
parsedConfig.fold(_.kind, _ => super.buildKind)
5759
def generateSemanticdb(): CommandResult = {
5860
parsedConfig match {
5961
case ValueResult(value) =>

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/IndexCommand.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ case class IndexCommand(
160160
output = finalOutput,
161161
targetroot = List(tool.targetroot),
162162
packagehub = packagehub,
163+
buildKind = tool.buildKind,
163164
app = app
164165
).run()
165166
}

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/IndexSemanticdbCommand.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ final case class IndexSemanticdbCommand(
3838
packagehub: Option[String] = None,
3939
@Description("Directories that contain SemanticDB files.")
4040
@PositionalArguments() targetroot: List[Path] = Nil,
41+
@Description(
42+
"The kind of this build, one of: empty string, jdk, maven"
43+
) buildKind: String = "",
4144
@Inline() app: Application = Application.default
4245
) extends Command {
4346
def sourceroot: Path = AbsolutePath.of(app.env.workingDirectory)
@@ -72,7 +75,8 @@ final case class IndexSemanticdbCommand(
7275
"java",
7376
format,
7477
parallel,
75-
packages.map(_.toPackageInformation).asJava
78+
packages.map(_.toPackageInformation).asJava,
79+
buildKind
7680
)
7781
LsifSemanticdb.run(options)
7882
postPackages(packages)

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private Integer processDocument(
9797
.collect(Collectors.toSet());
9898
doc.id = documentId;
9999
ResultSets results =
100-
new ResultSets(writer, globals, isExportedSymbol, localDefinitions, packages);
100+
new ResultSets(writer, globals, isExportedSymbol, localDefinitions, packages, options);
101101
Set<Integer> rangeIds = new LinkedHashSet<>();
102102

103103
for (SymbolOccurrence occ : doc.sortedSymbolOccurrences()) {

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/LsifSemanticdbOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class LsifSemanticdbOptions {
1616
public final LsifOutputFormat format;
1717
public final boolean parallel;
1818
public final List<MavenPackage> packages;
19+
public final String buildKind;
1920

2021
public LsifSemanticdbOptions(
2122
List<Path> targetroots,
@@ -26,7 +27,8 @@ public LsifSemanticdbOptions(
2627
String language,
2728
LsifOutputFormat format,
2829
boolean parallel,
29-
List<MavenPackage> packages) {
30+
List<MavenPackage> packages,
31+
String buildKind) {
3032
this.targetroots = targetroots;
3133
this.output = output;
3234
this.sourceroot = sourceroot;
@@ -36,5 +38,6 @@ public LsifSemanticdbOptions(
3638
this.format = format;
3739
this.parallel = parallel;
3840
this.packages = packages;
41+
this.buildKind = buildKind;
3942
}
4043
}

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/PackageTable.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,26 @@ public class PackageTable implements Function<Package, Integer> {
3636
public PackageTable(LsifSemanticdbOptions options, LsifWriter writer) throws IOException {
3737
this.writer = writer;
3838
this.javaVersion = new JavaVersion();
39+
// NOTE: it's important that we index the JDK before maven packages. Some maven packages
40+
// redefine classes from the JDK and we want those maven packages to take precedence over
41+
// the JDK. The motivation to prioritize maven packages over the JDK is that we only want
42+
// to exports monikers against the JDK when indexing the JDK repo.
43+
indexJdk();
3944
for (MavenPackage pkg : options.packages) {
4045
indexPackage(pkg);
4146
}
42-
indexJdk();
4347
}
4448

45-
public void writeImportedSymbol(String symbol, int monikerId) {
46-
packageForSymbol(symbol)
47-
.ifPresent(
48-
pkg -> {
49-
int pkgId = lsif.computeIfAbsent(pkg, this);
50-
writer.emitPackageInformationEdge(monikerId, pkgId);
51-
});
49+
public void writeMonikerPackage(int monikerId, Package pkg) {
50+
int pkgId = lsif.computeIfAbsent(pkg, this);
51+
writer.emitPackageInformationEdge(monikerId, pkgId);
5252
}
5353

54-
private Optional<Package> packageForClassfile(String classfile) {
55-
Package result = byClassfile.get(classfile);
56-
if (result != null) return Optional.of(result);
57-
if (!javaVersion.isJava8 && isJrtClassfile(classfile)) return Optional.of(javaVersion.pkg);
58-
return Optional.empty();
54+
public void writeImportedSymbol(String symbol, int monikerId) {
55+
packageForSymbol(symbol).ifPresent(pkg -> writeMonikerPackage(monikerId, pkg));
5956
}
6057

61-
private Optional<Package> packageForSymbol(String symbol) {
58+
public Optional<Package> packageForSymbol(String symbol) {
6259
return SymbolDescriptor.toplevel(symbol)
6360
.flatMap(
6461
toplevel -> {
@@ -67,6 +64,13 @@ private Optional<Package> packageForSymbol(String symbol) {
6764
});
6865
}
6966

67+
private Optional<Package> packageForClassfile(String classfile) {
68+
Package result = byClassfile.get(classfile);
69+
if (result != null) return Optional.of(result);
70+
if (!javaVersion.isJava8 && isJrtClassfile(classfile)) return Optional.of(javaVersion.pkg);
71+
return Optional.empty();
72+
}
73+
7074
private void indexPackage(MavenPackage pkg) throws IOException {
7175
if (!JAR_PATTERN.matches(pkg.jar)) {
7276
return;

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/ResultSets.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sourcegraph.semanticdb_javac.SemanticdbSymbols;
44
import java.util.HashMap;
55
import java.util.Map;
6+
import java.util.Optional;
67
import java.util.Set;
78
import java.util.function.Function;
89

@@ -15,18 +16,21 @@ public class ResultSets implements Function<String, ResultIds> {
1516
private final Set<String> exportedSymbols;
1617
private final Set<String> localDefinitions;
1718
private final PackageTable packages;
19+
private final boolean isJdkRepo;
1820

1921
public ResultSets(
2022
LsifWriter writer,
2123
Map<String, ResultIds> globals,
2224
Set<String> exportedSymbols,
2325
Set<String> localDefinitions,
24-
PackageTable packages) {
26+
PackageTable packages,
27+
LsifSemanticdbOptions options) {
2528
this.writer = writer;
2629
this.globals = globals;
2730
this.exportedSymbols = exportedSymbols;
2831
this.localDefinitions = localDefinitions;
2932
this.packages = packages;
33+
this.isJdkRepo = options.buildKind.equals("jdk");
3034
locals = new HashMap<>();
3135
}
3236

@@ -43,9 +47,20 @@ public ResultIds apply(String symbol) {
4347
int resultSet = writer.emitResultSet();
4448

4549
// Moniker
50+
Optional<Package> pkg = packages.packageForSymbol(symbol);
51+
if (pkg.isPresent() && pkg.get() instanceof JdkPackage && !isJdkRepo) {
52+
// Never export monikers for the JDK repo unless we're indexing the JDK repo.
53+
// Some Maven packages contain sources that redefine symbols like `java/lang/String#`
54+
// even if the the jar files don't contain `java/lang/String.class`. For example,
55+
// see the package com.google.gwt:gwt-user:2.9.0.
56+
// Related issue: https://github.com/sourcegraph/sourcegraph/issues/21058
57+
isExportedSymbol = false;
58+
}
4659
int monikerId = writer.emitMonikerVertex(symbol, hasDefinitionResult);
4760
writer.emitMonikerEdge(resultSet, monikerId);
48-
packages.writeImportedSymbol(symbol, monikerId);
61+
if (pkg.isPresent()) {
62+
packages.writeMonikerPackage(monikerId, pkg.get());
63+
}
4964

5065
int definitionId = hasDefinitionResult ? writer.emitDefinitionResult(resultSet) : -1;
5166

0 commit comments

Comments
 (0)