Skip to content

Commit c4cb410

Browse files
authored
Merge pull request github#11472 from erik-krogh/exit-code
JS: make the JS autobuilder consistent with Ruby when no JS code was detected
2 parents 1c7cae4 + 6289ae3 commit c4cb410

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.semmle.util.exception.UserError;
5353
import com.semmle.util.extraction.ExtractorOutputConfig;
5454
import com.semmle.util.files.FileUtil;
55+
import com.semmle.util.files.FileUtil8;
5556
import com.semmle.util.io.WholeIO;
5657
import com.semmle.util.io.csv.CSVReader;
5758
import com.semmle.util.language.LegacyLanguage;
@@ -433,23 +434,41 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
433434
return true;
434435
}
435436

437+
/**
438+
* Returns whether the autobuilder has seen code.
439+
* This is overridden in tests.
440+
*/
441+
protected boolean hasSeenCode() {
442+
return seenCode;
443+
}
444+
436445
/** Perform extraction. */
437446
public int run() throws IOException {
438447
startThreadPool();
439448
try {
440-
extractSource();
441-
extractExterns();
449+
CompletableFuture<?> sourceFuture = extractSource();
450+
sourceFuture.join(); // wait for source extraction to complete
451+
if (hasSeenCode()) { // don't bother with the externs if no code was seen
452+
extractExterns();
453+
}
442454
extractXml();
443455
} finally {
444456
shutdownThreadPool();
445457
}
446-
if (!seenCode) {
458+
if (!hasSeenCode()) {
447459
if (seenFiles) {
448460
warn("Only found JavaScript or TypeScript files that were empty or contained syntax errors.");
449461
} else {
450462
warn("No JavaScript or TypeScript code found.");
451463
}
452-
return -1;
464+
// ensuring that the finalize steps detects that no code was seen.
465+
Path srcFolder = Paths.get(EnvironmentVariables.getWipDatabase(), "src");
466+
// check that the srcFolder is empty
467+
if (Files.list(srcFolder).count() == 0) {
468+
// Non-recursive delete because "src/" should be empty.
469+
FileUtil8.delete(srcFolder);
470+
}
471+
return 0;
453472
}
454473
return 0;
455474
}
@@ -571,7 +590,7 @@ public FileType fileType(Path f) {
571590
}
572591

573592
/** Extract all supported candidate files that pass the filters. */
574-
private void extractSource() throws IOException {
593+
private CompletableFuture<?> extractSource() throws IOException {
575594
// default extractor
576595
FileExtractor defaultExtractor =
577596
new FileExtractor(mkExtractorConfig(), outputConfig, trapCache);
@@ -618,7 +637,7 @@ private void extractSource() throws IOException {
618637
boolean hasTypeScriptFiles = extractedFiles.size() > 0;
619638

620639
// extract remaining files
621-
extractFiles(
640+
return extractFiles(
622641
filesToExtract, extractedFiles, extractors,
623642
f -> !(hasTypeScriptFiles && isFileDerivedFromTypeScriptFile(f, extractedFiles)));
624643
}

javascript/extractor/src/com/semmle/js/extractor/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Main {
4141
* A version identifier that should be updated every time the extractor changes in such a way that
4242
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
4343
*/
44-
public static final String EXTRACTOR_VERSION = "2022-11-16";
44+
public static final String EXTRACTOR_VERSION = "2022-11-29";
4545

4646
public static final Pattern NEWLINE = Pattern.compile("\n");
4747

javascript/extractor/src/com/semmle/js/extractor/test/AutoBuildTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ protected CompletableFuture<?> extract(FileExtractor extractor, Path file, boole
119119
return CompletableFuture.completedFuture(null);
120120
}
121121

122+
@Override
123+
protected boolean hasSeenCode() {
124+
return true;
125+
}
126+
122127
@Override
123128
public void verifyTypeScriptInstallation(ExtractorState state) {}
124129

0 commit comments

Comments
 (0)