Skip to content

Commit

Permalink
feat(java extractor): use default value if corpus is ambiguous (#5305)
Browse files Browse the repository at this point in the history
Before this change, java CUs don't receive a corpus if the required_inputs are not all in the same corpus. With this change, instead of assigning the empty corpus, the CU is assigned a corpus from the KYTHE_CORPUS environment variable.
  • Loading branch information
justbuchanan authored Jun 9, 2022
1 parent f6affc5 commit 40808f9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,13 @@ private CompilationUnit buildCompilationUnit(
unit.addSourceFile(sourceFile);
sourceFileCorpora.add(inputCorpus.getOrDefault(sourceFile, ""));
}
if (sourceFileCorpora.size() == 1) {
// Attribute the source files' corpus to the CompilationUnit if it is unambiguous.
unit.getVNameBuilder().setCorpus(Iterables.getOnlyElement(sourceFileCorpora));
}
// Attribute the source files' corpus to the CompilationUnit if it is unambiguous. Otherwise use
// the default corpus.
String cuCorpus =
sourceFileCorpora.size() == 1
? Iterables.getOnlyElement(sourceFileCorpora)
: fileVNames.getDefaultCorpus();
unit.getVNameBuilder().setCorpus(cuCorpus);
unit.setOutputKey(outputPath);
unit.setWorkingDirectory(stableRoot(rootDirectory, options, requiredInputs));
unit.addDetails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public VNameRewriteRules toProto() {
.build();
}

public String getDefaultCorpus() {
return defaultCorpus.get();
}

/** Base {@link VName} to use for files matching {@code pattern}. */
private static class BaseFileVName {
public final Pattern pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public void testJavaExtractorFileVNames() throws Exception {
java.extract(TARGET1, sources, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, "output");

CompilationUnit unit = description.getCompilationUnit();
assertThat(unit.getVName().getCorpus()).isEmpty(); // source files are from different corpora
// source files are from different corpora, so unit is assigned default corpus of "kythe"
assertThat(unit.getVName().getCorpus()).isEqualTo("kythe");
assertThat(unit.getWorkingDirectory()).isEqualTo("/root");
assertThat(unit).isNotNull();
assertThat(unit.getVName().getSignature()).isEqualTo(TARGET1);
Expand Down

0 comments on commit 40808f9

Please sign in to comment.