Skip to content

Commit 43accc5

Browse files
committed
JavaScript: Ignore outDirs that would exclude everything
In #19680 we added support for automatically ignoring files in the `outDir` directory as specified in the TSconfig compiler options (as these files were likely duplicates of `.ts` file we were already scanning). However, in some cases people put `outDir: "."` or even `outDir: ".."` in their configuration, which had the side effect of excluding _all_ files, leading to a failed extraction. With the changes in this PR, we now ignore any `outDir`s that are not properly contained within the source root of the code being scanned. This should prevent the files from being extracted, while still allowing us to not double-scan files in, say, a `.github` directory, as seen in some Actions workflows.
1 parent 649c883 commit 43accc5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,11 @@ private CompletableFuture<?> extractSource() throws IOException {
754754
continue;
755755
}
756756
Path odir = cfg.getParent().resolve(root.getCompilerOptions().getOutDir()).toAbsolutePath().normalize();
757-
outDirs.add(odir);
757+
// Only exclude outDirs that are proper subdirectories of the source root
758+
// This prevents excluding all code when outDir points outside the source root or to the source root itself
759+
if (tryRelativize(LGTM_SRC, odir) != null && !odir.equals(LGTM_SRC)) {
760+
outDirs.add(odir);
761+
}
758762
}
759763
} catch (Exception e) {
760764
// ignore malformed tsconfig or missing fields

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,36 @@ public void skipFilesInTsconfigOutDir() throws IOException {
235235
runTest();
236236
}
237237

238+
@Test
239+
public void skipFilesInTsconfigOutDirPointingToParent() throws IOException {
240+
// Test that outDir pointing to parent directory (outside source root) is ignored
241+
addFile(true, LGTM_SRC, "tsconfig.json");
242+
Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json");
243+
Files.write(config,
244+
"{\"compilerOptions\":{\"outDir\":\"..\"}}".getBytes(StandardCharsets.UTF_8));
245+
246+
// All files should be extracted since outDir pointing outside source root should be ignored
247+
addFile(true, LGTM_SRC, "src", "app.ts");
248+
addFile(true, LGTM_SRC, "main.js");
249+
250+
runTest();
251+
}
252+
253+
@Test
254+
public void skipFilesInTsconfigOutDirPointingToSourceRoot() throws IOException {
255+
// Test that outDir pointing to source root itself is ignored
256+
addFile(true, LGTM_SRC, "tsconfig.json");
257+
Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json");
258+
Files.write(config,
259+
"{\"compilerOptions\":{\"outDir\":\".\"}}".getBytes(StandardCharsets.UTF_8));
260+
261+
// All files should be extracted since outDir pointing to source root should be ignored
262+
addFile(true, LGTM_SRC, "src", "app.ts");
263+
addFile(true, LGTM_SRC, "main.js");
264+
265+
runTest();
266+
}
267+
238268
@Test
239269
public void includeFile() throws IOException {
240270
envVars.put("LGTM_INDEX_INCLUDE", "tst.js");

0 commit comments

Comments
 (0)