Skip to content

Commit d33cd71

Browse files
authored
Merge pull request #20030 from github/tausbn/javascript-ignore-tsconfig-outdirs-that-exclude-everything
JavaScript: Ignore `outDir`s that would exclude everything
2 parents 85d1e06 + 30f7058 commit d33cd71

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,51 @@ 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+
268+
@Test
269+
public void skipFilesInTsconfigOutDirWithRelativePath() throws IOException {
270+
// Test that outDir with relative path "somedir/.." (resolves to root) is ignored
271+
addFile(true, LGTM_SRC, "tsconfig.json");
272+
Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json");
273+
Files.write(config,
274+
"{\"compilerOptions\":{\"outDir\":\"somedir/..\"}}".getBytes(StandardCharsets.UTF_8));
275+
276+
// All files should be extracted since outDir resolving to root should be ignored
277+
addFile(true, LGTM_SRC, "src", "app.ts");
278+
addFile(true, LGTM_SRC, "main.js");
279+
280+
runTest();
281+
}
282+
238283
@Test
239284
public void includeFile() throws IOException {
240285
envVars.put("LGTM_INDEX_INCLUDE", "tst.js");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* 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.

0 commit comments

Comments
 (0)