From 43accc50cd247cb60f18afcfff0727b649eea506 Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 11 Jul 2025 13:28:59 +0000 Subject: [PATCH 1/3] JavaScript: Ignore `outDir`s 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. --- .../com/semmle/js/extractor/AutoBuild.java | 6 +++- .../js/extractor/test/AutoBuildTests.java | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java index 230f6da5b3d8..08dac06c4d94 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java +++ b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java @@ -754,7 +754,11 @@ private CompletableFuture extractSource() throws IOException { continue; } Path odir = cfg.getParent().resolve(root.getCompilerOptions().getOutDir()).toAbsolutePath().normalize(); - outDirs.add(odir); + // Only exclude outDirs that are proper subdirectories of the source root + // This prevents excluding all code when outDir points outside the source root or to the source root itself + if (tryRelativize(LGTM_SRC, odir) != null && !odir.equals(LGTM_SRC)) { + outDirs.add(odir); + } } } catch (Exception e) { // ignore malformed tsconfig or missing fields diff --git a/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java b/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java index b972355d91d6..95f68aaf99ac 100644 --- a/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java +++ b/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java @@ -235,6 +235,36 @@ public void skipFilesInTsconfigOutDir() throws IOException { runTest(); } + @Test + public void skipFilesInTsconfigOutDirPointingToParent() throws IOException { + // Test that outDir pointing to parent directory (outside source root) is ignored + addFile(true, LGTM_SRC, "tsconfig.json"); + Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json"); + Files.write(config, + "{\"compilerOptions\":{\"outDir\":\"..\"}}".getBytes(StandardCharsets.UTF_8)); + + // All files should be extracted since outDir pointing outside source root should be ignored + addFile(true, LGTM_SRC, "src", "app.ts"); + addFile(true, LGTM_SRC, "main.js"); + + runTest(); + } + + @Test + public void skipFilesInTsconfigOutDirPointingToSourceRoot() throws IOException { + // Test that outDir pointing to source root itself is ignored + addFile(true, LGTM_SRC, "tsconfig.json"); + Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json"); + Files.write(config, + "{\"compilerOptions\":{\"outDir\":\".\"}}".getBytes(StandardCharsets.UTF_8)); + + // All files should be extracted since outDir pointing to source root should be ignored + addFile(true, LGTM_SRC, "src", "app.ts"); + addFile(true, LGTM_SRC, "main.js"); + + runTest(); + } + @Test public void includeFile() throws IOException { envVars.put("LGTM_INDEX_INCLUDE", "tst.js"); From 2f822cb0cdf95aa5070ac4993f02a6c9ebe32c3c Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 11 Jul 2025 13:32:35 +0000 Subject: [PATCH 2/3] JavaScript: Add change note --- ...2025-07-11-ignore-outdirs-that-would-exclude-everything.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 javascript/ql/lib/change-notes/2025-07-11-ignore-outdirs-that-would-exclude-everything.md diff --git a/javascript/ql/lib/change-notes/2025-07-11-ignore-outdirs-that-would-exclude-everything.md b/javascript/ql/lib/change-notes/2025-07-11-ignore-outdirs-that-would-exclude-everything.md new file mode 100644 index 000000000000..aeffaebb477d --- /dev/null +++ b/javascript/ql/lib/change-notes/2025-07-11-ignore-outdirs-that-would-exclude-everything.md @@ -0,0 +1,4 @@ +--- +category: fix +--- +* The JavaScript extractor no longer ignores source files specified in the `tsconfig.json` compiler options `outDir` if doing so would result in excluding all source code. From 30f705822d12eebb8d50bf01c7aaa61151abed1e Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 11 Jul 2025 14:58:03 +0000 Subject: [PATCH 3/3] JavaScript: Add test where `outDir` resolves to an unwanted path --- .../semmle/js/extractor/test/AutoBuildTests.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java b/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java index 95f68aaf99ac..e6b168ac340c 100644 --- a/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java +++ b/javascript/extractor/test/com/semmle/js/extractor/test/AutoBuildTests.java @@ -265,6 +265,21 @@ public void skipFilesInTsconfigOutDirPointingToSourceRoot() throws IOException { runTest(); } + @Test + public void skipFilesInTsconfigOutDirWithRelativePath() throws IOException { + // Test that outDir with relative path "somedir/.." (resolves to root) is ignored + addFile(true, LGTM_SRC, "tsconfig.json"); + Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json"); + Files.write(config, + "{\"compilerOptions\":{\"outDir\":\"somedir/..\"}}".getBytes(StandardCharsets.UTF_8)); + + // All files should be extracted since outDir resolving to root should be ignored + addFile(true, LGTM_SRC, "src", "app.ts"); + addFile(true, LGTM_SRC, "main.js"); + + runTest(); + } + @Test public void includeFile() throws IOException { envVars.put("LGTM_INDEX_INCLUDE", "tst.js");