Skip to content

Commit 3c1cbce

Browse files
committed
TS: Pass virtual source root explicitly to Node.js process
1 parent b88cc50 commit 3c1cbce

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

javascript/extractor/lib/typescript/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface ParseCommand {
4848
interface OpenProjectCommand {
4949
command: "open-project";
5050
tsConfig: string;
51+
virtualSourceRoot: string | null;
5152
packageEntryPoints: [string, string][];
5253
packageJsonFiles: [string, string][];
5354
}
@@ -261,7 +262,7 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
261262

262263
let packageEntryPoints = new Map(command.packageEntryPoints);
263264
let packageJsonFiles = new Map(command.packageJsonFiles);
264-
let virtualSourceRoot = new VirtualSourceRoot(process.cwd(), process.env["CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR"]);
265+
let virtualSourceRoot = new VirtualSourceRoot(process.cwd(), command.virtualSourceRoot);
265266

266267
/**
267268
* Rewrites path segments of form `node_modules/PACK/suffix` to be relative to
@@ -582,6 +583,7 @@ if (process.argv.length > 2) {
582583
tsConfig: argument,
583584
packageEntryPoints: [],
584585
packageJsonFiles: [],
586+
virtualSourceRoot: null,
585587
});
586588
for (let sf of state.project.program.getSourceFiles()) {
587589
if (pathlib.basename(sf.fileName) === "lib.d.ts") continue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ protected DependencyInstallationResult installDependencies(Set<Path> filesToExtr
784784
}
785785
}
786786

787-
return new DependencyInstallationResult(packageMainFile, packagesInRepo);
787+
return new DependencyInstallationResult(virtualSourceRoot, packageMainFile, packagesInRepo);
788788
}
789789

790790
/**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@
66

77
/** Contains the results of installing dependencies. */
88
public class DependencyInstallationResult {
9+
private Path virtualSourceRoot;
910
private Map<String, Path> packageEntryPoints;
1011
private Map<String, Path> packageJsonFiles;
1112

1213
public static final DependencyInstallationResult empty =
13-
new DependencyInstallationResult(Collections.emptyMap(), Collections.emptyMap());
14+
new DependencyInstallationResult(null, Collections.emptyMap(), Collections.emptyMap());
1415

1516
public DependencyInstallationResult(
17+
Path virtualSourceRoot,
1618
Map<String, Path> packageEntryPoints,
1719
Map<String, Path> packageJsonFiles) {
1820
this.packageEntryPoints = packageEntryPoints;
1921
this.packageJsonFiles = packageJsonFiles;
2022
}
2123

24+
/**
25+
* Returns the virtual source root or <code>null</code> if no virtual source root exists.
26+
*
27+
* The virtual source root is a directory hierarchy that mirrors the real source
28+
* root, where dependencies are installed.
29+
*/
30+
public Path getVirtualSourceRoot() {
31+
return virtualSourceRoot;
32+
}
33+
2234
/**
2335
* Returns the mapping from package names to the TypeScript file that should
2436
* act as its main entry point.

javascript/extractor/src/com/semmle/js/parser/TypeScriptParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.gson.JsonArray;
2222
import com.google.gson.JsonElement;
23+
import com.google.gson.JsonNull;
2324
import com.google.gson.JsonObject;
2425
import com.google.gson.JsonParseException;
2526
import com.google.gson.JsonParser;
@@ -435,6 +436,9 @@ public ParsedProject openProject(File tsConfigFile, DependencyInstallationResult
435436
request.add("tsConfig", new JsonPrimitive(tsConfigFile.getPath()));
436437
request.add("packageEntryPoints", mapToArray(deps.getPackageEntryPoints()));
437438
request.add("packageJsonFiles", mapToArray(deps.getPackageJsonFiles()));
439+
request.add("virtualSourceRoot", deps.getVirtualSourceRoot() == null
440+
? JsonNull.INSTANCE
441+
: new JsonPrimitive(deps.getVirtualSourceRoot().toString()));
438442
JsonObject response = talkToParserWrapper(request);
439443
try {
440444
checkResponseType(response, "project-opened");

0 commit comments

Comments
 (0)