Skip to content

Commit 737ec70

Browse files
authored
Merge pull request #460 from xiemaisi/js/in-dist-trap-cache
JavaScript: Teach `AutoBuild` to use in-dist externs trap cache.
2 parents 0d7c5ea + 851e71c commit 737ec70

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,8 @@ private void setupIncludesAndExcludes() {
285285
excludes.add(toRealPath(folderPath));
286286
} catch (InvalidPathException | URISyntaxException | ResourceError e) {
287287
Exceptions.ignore(e, "Ignore path and print warning message instead");
288-
System.err.println("Ignoring '" + fields[0] + "' classification for " +
288+
warn("Ignoring '" + fields[0] + "' classification for " +
289289
folder + ", which is not a valid path.");
290-
System.err.flush();
291290
}
292291
}
293292
}
@@ -354,8 +353,7 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
354353
patterns.add(realPath);
355354
} catch (ResourceError e) {
356355
Exceptions.ignore(e, "Ignore exception and print warning instead.");
357-
System.err.println("Skipping path " + path + ", which does not exist.");
358-
System.err.flush();
356+
warn("Skipping path " + path + ", which does not exist.");
359357
}
360358
return true;
361359
}
@@ -374,6 +372,35 @@ public void run() throws IOException {
374372
*/
375373
private void extractExterns() throws IOException {
376374
ExtractorConfig config = new ExtractorConfig(false).withExterns(true);
375+
376+
// use explicitly specified trap cache, or otherwise $SEMMLE_DIST/.cache/trap-cache/javascript,
377+
// which we pre-populate when building the distribution
378+
ITrapCache trapCache = this.trapCache;
379+
if (trapCache instanceof DummyTrapCache) {
380+
Path trapCachePath = SEMMLE_DIST.resolve(".cache").resolve("trap-cache").resolve("javascript");
381+
if (Files.isDirectory(trapCachePath)) {
382+
trapCache = new DefaultTrapCache(trapCachePath.toString(), null, Main.EXTRACTOR_VERSION) {
383+
boolean warnedAboutCacheMiss = false;
384+
385+
@Override
386+
public File lookup(String source, ExtractorConfig config, FileType type) {
387+
File f = super.lookup(source, config, type);
388+
// only return `f` if it exists; this has the effect of making the cache read-only
389+
if (f.exists())
390+
return f;
391+
// warn on first failed lookup
392+
if (!warnedAboutCacheMiss) {
393+
warn("Trap cache lookup for externs failed.");
394+
warnedAboutCacheMiss = true;
395+
}
396+
return null;
397+
}
398+
};
399+
} else {
400+
warn("No externs trap cache found");
401+
}
402+
}
403+
377404
FileExtractor extractor = new FileExtractor(config, outputConfig, trapCache, extractorState);
378405
FileVisitor<? super Path> visitor = new SimpleFileVisitor<Path>() {
379406
@Override
@@ -539,8 +566,7 @@ private SourceType getSourceType() {
539566
protected void extract(FileExtractor extractor, Path file) throws IOException {
540567
File f = file.toFile();
541568
if (!f.exists()) {
542-
System.err.println("Skipping " + file + ", which does not exist.");
543-
System.err.flush();
569+
warn("Skipping " + file + ", which does not exist.");
544570
return;
545571
}
546572

@@ -549,6 +575,11 @@ protected void extract(FileExtractor extractor, Path file) throws IOException {
549575
logEndProcess();
550576
}
551577

578+
private void warn(String msg) {
579+
System.err.println(msg);
580+
System.err.flush();
581+
}
582+
552583
private void logBeginProcess(String message) {
553584
System.out.print(message + "...");
554585
System.out.flush();

0 commit comments

Comments
 (0)