Skip to content

Commit d197e8a

Browse files
paldepindCopilot
andcommitted
JavaScript: Remove TypeScript parser memory limits
Remove extractor-imposed Node heap limits and obsolete TypeScript RAM configuration. Stop interpreting CODEQL_RAM in the JavaScript autobuild launchers, and retain --typescript-ram as an ignored compatibility option. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7f45b794-38b9-4abc-b652-03279c8dba77
1 parent 81a1deb commit d197e8a

7 files changed

Lines changed: 119 additions & 77 deletions

File tree

‎javascript/extractor/src/com/semmle/js/extractor/ExtractorConfig.java‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ public Set<String> getPredefinedGlobals() {
224224
/** Should textual information be extracted into the lines/4 relation? */
225225
private boolean extractLines;
226226

227-
/** Override amount of RAM to allocate to the TypeScript compiler. */
228-
private int typescriptRam;
229-
230227
/** The default character encoding to use for parsing source files. */
231228
private String defaultEncoding;
232229

@@ -267,7 +264,6 @@ public ExtractorConfig(ExtractorConfig that) {
267264
this.sourceType = that.sourceType;
268265
this.htmlHandling = that.htmlHandling;
269266
this.extractLines = that.extractLines;
270-
this.typescriptRam = that.typescriptRam;
271267
this.defaultEncoding = that.defaultEncoding;
272268
this.virtualSourceRoot = that.virtualSourceRoot;
273269
}
@@ -426,16 +422,6 @@ public ExtractorConfig withExtractLines(boolean extractLines) {
426422
return res;
427423
}
428424

429-
public int getTypeScriptRam() {
430-
return typescriptRam;
431-
}
432-
433-
public ExtractorConfig withTypeScriptRam(int ram) {
434-
ExtractorConfig res = new ExtractorConfig(this);
435-
res.typescriptRam = ram;
436-
return res;
437-
}
438-
439425
public String getDefaultEncoding() {
440426
return defaultEncoding;
441427
}

‎javascript/extractor/src/com/semmle/js/extractor/Main.java‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.semmle.js.parser.ParsedProject;
2121
import com.semmle.ts.extractor.TypeScriptParser;
2222
import com.semmle.util.data.StringUtil;
23-
import com.semmle.util.data.UnitParser;
2423
import com.semmle.util.exception.ResourceError;
2524
import com.semmle.util.exception.UserError;
2625
import com.semmle.util.extraction.ExtractorOutputConfig;
@@ -60,9 +59,9 @@ public class Main {
6059
private static final String P_SOURCE_TYPE = "--source-type";
6160
private static final String P_TYPESCRIPT = "--typescript";
6261
private static final String P_TYPESCRIPT_FULL = "--typescript-full";
63-
private static final String P_TYPESCRIPT_RAM = "--typescript-ram";
6462

6563
// symbolic constants for deprecated command line parameter names
64+
private static final String P_TYPESCRIPT_RAM = "--typescript-ram";
6665
private static final String P_EXCLUDE_PATH = "--exclude-path";
6766
private static final String P_TOLERATE_PARSE_ERRORS = "--tolerate-parse-errors";
6867
private static final String P_NODEJS = "--nodejs";
@@ -130,7 +129,6 @@ public void run(String[] args) {
130129
}
131130

132131
TypeScriptParser tsParser = extractorState.getTypeScriptParser();
133-
tsParser.setTypescriptRam(extractorConfig.getTypeScriptRam());
134132
if (containsTypeScriptFiles()) {
135133
tsParser.verifyInstallation(!ap.has(P_QUIET));
136134
}
@@ -373,10 +371,7 @@ private ArgsParser addArgs(ArgsParser argsParser) {
373371
argsParser.addFlag(P_TYPESCRIPT, 0, "Enable basic TypesScript support.");
374372
argsParser.addFlag(
375373
P_TYPESCRIPT_FULL, 0, "Enable full TypeScript support with static type information.");
376-
argsParser.addFlag(
377-
P_TYPESCRIPT_RAM,
378-
1,
379-
"Amount of memory allocated to the TypeScript compiler process. The default is 1G.");
374+
argsParser.addToleratedFlag(P_TYPESCRIPT_RAM, 1);
380375
argsParser.addToleratedFlag(P_DEBUG_EXCLUSIONS, 0);
381376
argsParser.addTrailingParam("files", "Files and directories to extract.");
382377
return argsParser;
@@ -415,11 +410,7 @@ private ExtractorConfig parseJSOptions(ArgsParser ap) {
415410
ap.has(P_EXPERIMENTAL) ? HtmlPopulator.Config.ALL : HtmlPopulator.Config.ELEMENTS))
416411
.withFileType(getFileType(ap))
417412
.withSourceType(ap.getEnum(P_SOURCE_TYPE, SourceType.class, SourceType.AUTO))
418-
.withExtractLines(ap.has(P_EXTRACT_PROGRAM_TEXT))
419-
.withTypeScriptRam(
420-
ap.has(P_TYPESCRIPT_RAM)
421-
? UnitParser.parseOpt(ap.getString(P_TYPESCRIPT_RAM), UnitParser.MEGABYTES)
422-
: 0);
413+
.withExtractLines(ap.has(P_EXTRACT_PROGRAM_TEXT));
423414
if (ap.has(P_DEFAULT_ENCODING)) cfg = cfg.withDefaultEncoding(ap.getString(P_DEFAULT_ENCODING));
424415

425416
// Make a usable virtual source root mapping.

‎javascript/extractor/src/com/semmle/ts/extractor/TypeScriptParser.java‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.semmle.js.parser.ParsedProject;
3737
import com.semmle.js.parser.JSParser.Result;
3838
import com.semmle.util.data.StringUtil;
39-
import com.semmle.util.data.UnitParser;
4039
import com.semmle.util.exception.CatastrophicError;
4140
import com.semmle.util.exception.Exceptions;
4241
import com.semmle.util.exception.InterruptedError;
@@ -96,24 +95,6 @@ public class TypeScriptParser {
9695
*/
9796
public static final String TYPESCRIPT_RETRIES_VAR = "SEMMLE_TYPESCRIPT_RETRIES";
9897

99-
/**
100-
* An environment variable (without the <code>SEMMLE_</code> or <code>LGTM_</code> prefix), that can be
101-
* set to indicate the maximum heap space usable by the Node.js process, in addition to its
102-
* "reserve memory".
103-
*
104-
* <p>Defaults to 2000 MB (for a total heap space of 2400 MB by default).
105-
*/
106-
public static final String TYPESCRIPT_RAM_SUFFIX = "TYPESCRIPT_RAM";
107-
108-
/**
109-
* An environment variable (without the <code>SEMMLE_</code> or <code>LGTM_</code> prefix), that can be
110-
* set to indicate the additional heap space added to <code>TYPESCRIPT_RAM</code> when setting the
111-
* Node.js heap limit.
112-
*
113-
* <p>Defaults to 400 MB (for a total heap space of 2400 MB by default).
114-
*/
115-
public static final String TYPESCRIPT_RAM_RESERVE_SUFFIX = "TYPESCRIPT_RAM_RESERVE";
116-
11798
/**
11899
* An environment variable with additional VM arguments to pass to the Node process.
119100
*
@@ -154,17 +135,9 @@ public class TypeScriptParser {
154135
*/
155136
private List<String> nodeJsRuntimeExtraArgs = Collections.emptyList();
156137

157-
/** If non-zero, we use this instead of relying on the corresponding environment variable. */
158-
private int typescriptRam = 0;
159-
160138
/** Metadata requested immediately after starting the TypeScript parser. */
161139
private TypeScriptParserMetadata metadata;
162140

163-
/** Sets the amount of RAM to allocate to the TypeScript compiler.s */
164-
public void setTypescriptRam(int megabytes) {
165-
this.typescriptRam = megabytes;
166-
}
167-
168141
/**
169142
* Verifies that Node.js and TypeScript are installed and throws an exception otherwise.
170143
*
@@ -274,14 +247,6 @@ private List<String> getNodeJsRuntimeInvocation(String... args) {
274247
private void setupParserWrapper() {
275248
verifyNodeInstallation();
276249

277-
int mainMemoryMb =
278-
typescriptRam != 0
279-
? typescriptRam
280-
: EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_SUFFIX, 2000);
281-
int reserveMemoryMb = EnvironmentVariables.getMegabyteCountFromPrefixedEnv(TYPESCRIPT_RAM_RESERVE_SUFFIX, 400);
282-
283-
System.out.println("Memory for TypeScript process: " + mainMemoryMb + " MB, and " + reserveMemoryMb + " MB reserve");
284-
285250
File parserWrapper = getParserWrapper();
286251

287252
String debugFlagString = Env.systemEnv().getNonEmpty(TYPESCRIPT_NODE_FLAGS);
@@ -297,7 +262,6 @@ private void setupParserWrapper() {
297262
}
298263

299264
List<String> cmd = getNodeJsRuntimeInvocation();
300-
cmd.add("--max_old_space_size=" + (mainMemoryMb + reserveMemoryMb));
301265
cmd.addAll(debugFlags);
302266
cmd.add(parserWrapper.getAbsolutePath());
303267

‎javascript/extractor/test/com/semmle/js/extractor/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
DecoratorTests.class,
2727
ExportExtensionsTests.class,
2828
AutoBuildTests.class,
29+
TypeScriptParserTests.class,
2930
RobustnessTests.class,
3031
NumericSeparatorTests.class
3132
})
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.semmle.js.extractor.test;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonParser;
5+
import com.semmle.js.extractor.Main;
6+
import com.semmle.ts.extractor.TypeScriptParser;
7+
import com.semmle.util.extraction.ExtractorOutputConfig;
8+
import com.semmle.util.process.Env;
9+
import com.semmle.util.srcarchive.DummySourceArchive;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.ArrayList;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import org.junit.After;
17+
import org.junit.Assert;
18+
import org.junit.Before;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.rules.TemporaryFolder;
22+
23+
public class TypeScriptParserTests {
24+
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
25+
26+
@Before
27+
public void setup() {
28+
Env.systemEnv()
29+
.pushEnvironmentContextExcluding(
30+
List.of(
31+
TypeScriptParser.TYPESCRIPT_NODE_RUNTIME_EXTRA_ARGS_VAR,
32+
TypeScriptParser.TYPESCRIPT_NODE_FLAGS,
33+
"SEMMLE_TYPESCRIPT_RAM",
34+
"SEMMLE_TYPESCRIPT_RAM_RESERVE",
35+
"LGTM_TYPESCRIPT_RAM",
36+
"LGTM_TYPESCRIPT_RAM_RESERVE",
37+
"ODASA_DB"));
38+
}
39+
40+
@After
41+
public void teardown() {
42+
Env.systemEnv().popEnvironmentContext();
43+
}
44+
45+
private List<String> startParser(Map<String, String> environment) throws Exception {
46+
Path wrapper = temporaryFolder.newFolder().toPath().resolve("wrapper.cjs");
47+
Files.writeString(
48+
wrapper,
49+
"""
50+
const fs = require('fs');
51+
const readline = require('readline');
52+
fs.writeFileSync(__filename + '.args', JSON.stringify(process.execArgv));
53+
readline.createInterface({ input: process.stdin }).on('line', line => {
54+
const request = JSON.parse(line);
55+
const response = request.command === 'get-metadata'
56+
? { type: 'metadata', nodeFlags: {}, syntaxKinds: {} }
57+
: { type: 'ok' };
58+
console.log(JSON.stringify(response));
59+
});
60+
""");
61+
Map<String, String> env = new LinkedHashMap<>(environment);
62+
env.put(TypeScriptParser.PARSER_WRAPPER_PATH_ENV_VAR, wrapper.toString());
63+
Env.systemEnv().pushEnvironmentContext(env);
64+
TypeScriptParser parser = new TypeScriptParser();
65+
try {
66+
parser.prepareFiles(List.of());
67+
List<String> arguments = new ArrayList<>();
68+
for (JsonElement argument :
69+
new JsonParser().parse(Files.readString(Path.of(wrapper + ".args"))).getAsJsonArray()) {
70+
arguments.add(argument.getAsString());
71+
}
72+
return arguments;
73+
} finally {
74+
parser.killProcess();
75+
Env.systemEnv().popEnvironmentContext();
76+
}
77+
}
78+
79+
@Test(timeout = 30000)
80+
public void noAutomaticHeapLimit() throws Exception {
81+
Assert.assertEquals(List.of(), startParser(Map.of()));
82+
}
83+
84+
@Test(timeout = 30000)
85+
public void ignoresLegacyMemoryEnvironment() throws Exception {
86+
for (String prefix : List.of("SEMMLE_", "LGTM_")) {
87+
Assert.assertEquals(
88+
List.of(),
89+
startParser(
90+
Map.of(prefix + "TYPESCRIPT_RAM", "128", prefix + "TYPESCRIPT_RAM_RESERVE", "32")));
91+
for (String suffix : List.of("TYPESCRIPT_RAM", "TYPESCRIPT_RAM_RESERVE")) {
92+
Assert.assertEquals(List.of(), startParser(Map.of(prefix + suffix, "not-a-memory-size")));
93+
}
94+
}
95+
}
96+
97+
@Test(timeout = 30000)
98+
public void preservesExplicitNodeArguments() throws Exception {
99+
Assert.assertEquals(
100+
List.of("--no-warnings", "--max-old-space-size=128"),
101+
startParser(
102+
Map.of(
103+
TypeScriptParser.TYPESCRIPT_NODE_RUNTIME_EXTRA_ARGS_VAR,
104+
"--no-warnings --max-old-space-size=128")));
105+
}
106+
107+
@Test
108+
public void toleratesLegacyMemoryOption() throws Exception {
109+
Path emptyDirectory = temporaryFolder.newFolder().toPath();
110+
for (String value : List.of("128", "not-a-memory-size")) {
111+
new Main(new ExtractorOutputConfig(null, new DummySourceArchive()))
112+
.run(new String[] {"--quiet", "--typescript-ram", value, emptyDirectory.toString()});
113+
}
114+
}
115+
}

‎javascript/resources/tools/autobuild.cmd‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ SETLOCAL EnableDelayedExpansion
33

44
set jvm_args=-Xss16m
55

6-
rem If CODEQL_RAM is set, use half for Java and half for TS.
7-
if NOT [%CODEQL_RAM%] == [] (
8-
set /a "half_ram=CODEQL_RAM/2"
9-
set LGTM_TYPESCRIPT_RAM=!half_ram!
10-
set jvm_args=!jvm_args! -Xmx!half_ram!m
11-
)
12-
136
rem If CODEQL_THREADS is set, propagate via LGTM_THREADS.
147
if NOT [%CODEQL_THREADS%] == [] (
158
set LGTM_THREADS=%CODEQL_THREADS%

‎javascript/resources/tools/autobuild.sh‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ set -eu
44

55
jvm_args=-Xss16m
66

7-
# If CODEQL_RAM is set, use half for Java and half for TS.
8-
if [ -n "${CODEQL_RAM:-}" ] ; then
9-
half_ram="$(( CODEQL_RAM / 2 ))"
10-
LGTM_TYPESCRIPT_RAM="$half_ram"
11-
export LGTM_TYPESCRIPT_RAM
12-
jvm_args="$jvm_args -Xmx${half_ram}m"
13-
fi
14-
157
# If CODEQL_THREADS is set, propagate via LGTM_THREADS.
168
if [ -n "${CODEQL_THREADS:-}" ] ; then
179
LGTM_THREADS="$CODEQL_THREADS"

0 commit comments

Comments
 (0)