Skip to content

Commit ca79b08

Browse files
committed
TS: Add debugging flag and document how to run the debugger
1 parent 24f407c commit ca79b08

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
TypeScript parser wrapper
2+
-------------------------
3+
4+
The TypeScript "parser wrapper" is a Node.js program launched from the JavaScript
5+
extractor in order to extract TypeScript code.
6+
7+
The two processes communicate via a command/response protocol via stdin/stdout.
8+
9+
Debugging
10+
---------
11+
12+
To debug the parser script:
13+
14+
1. Launch an extraction process with the environment variable `SEMMLE_TYPESCRIPT_ATTACH_DEBUGGER`
15+
set to `break`.
16+
17+
2. Open VSCode and choose "Debug: Attach to Node process" from the command palette, and
18+
choose the process that looks like the parser-wrapper.
19+
20+
If you don't wish to pause on entry, instead set `SEMMLE_TYPESCRIPT_ATTACH_DEBUGGER` to
21+
any non-empty value.

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ public class TypeScriptParser {
9898
*/
9999
public static final String TYPESCRIPT_RAM_RESERVE_SUFFIX = "TYPESCRIPT_RAM_RESERVE";
100100

101+
/**
102+
* An environment variable which, if set, allows a debugger to be attached to the Node.js
103+
* process. Remote debugging will not be enabled.
104+
* <p>
105+
* If set to <code>break</code> the Node.js process will pause on entry waiting for the
106+
* debugger to attach (<code>--inspect-brk</code>). If set to any other non-empty value,
107+
* it will just enable debugging (<code>--inspect</code>).
108+
*/
109+
public static final String TYPESCRIPT_ATTACH_DEBUGGER = "SEMMLE_TYPESCRIPT_ATTACH_DEBUGGER";
110+
101111
/** The Node.js parser wrapper process, if it has been started already. */
102112
private Process parserWrapperProcess;
103113

@@ -203,7 +213,9 @@ private List<String> getNodeJsRuntimeInvocation(String ...args) {
203213
result.add(nodeJsRuntime);
204214
result.addAll(nodeJsRuntimeExtraArgs);
205215
for(String arg : args) {
206-
result.add(arg);
216+
if (arg.length() > 0) {
217+
result.add(arg);
218+
}
207219
}
208220
return result;
209221
}
@@ -236,9 +248,20 @@ private void setupParserWrapper() {
236248
int reserveMemoryMb = getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
237249

238250
File parserWrapper = getParserWrapper();
239-
251+
252+
String debuggerFlag = Env.systemEnv().get(TYPESCRIPT_ATTACH_DEBUGGER);
253+
String inspectArg = "";
254+
if (debuggerFlag != null && debuggerFlag.length() > 0) {
255+
if (debuggerFlag.equalsIgnoreCase("break")) {
256+
inspectArg = "--inspect-brk";
257+
} else {
258+
inspectArg = "--inspect";
259+
}
260+
}
261+
240262
List<String> cmd = getNodeJsRuntimeInvocation(
241263
"--max_old_space_size=" + (mainMemoryMb + reserveMemoryMb),
264+
inspectArg,
242265
parserWrapper.getAbsolutePath()
243266
);
244267
ProcessBuilder pb = new ProcessBuilder(cmd);

0 commit comments

Comments
 (0)