Skip to content

Commit e441e43

Browse files
authored
Merge pull request #2484 from asger-semmle/typescript-codeql-env-var
JS: Make extractor aware of CodeQL env vars
2 parents f162749 + f988e90 commit e441e43

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public class AutoBuild {
205205

206206
public AutoBuild() {
207207
this.LGTM_SRC = toRealPath(getPathFromEnvVar("LGTM_SRC"));
208-
this.SEMMLE_DIST = getPathFromEnvVar(Env.Var.SEMMLE_DIST.toString());
208+
this.SEMMLE_DIST = Paths.get(EnvironmentVariables.getExtractorRoot());
209209
this.outputConfig = new ExtractorOutputConfig(LegacyLanguage.JAVASCRIPT);
210210
this.trapCache = mkTrapCache();
211211
this.typeScriptMode =
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.semmle.js.extractor;
2+
3+
import com.semmle.util.exception.UserError;
4+
import com.semmle.util.process.Env;
5+
import com.semmle.util.process.Env.Var;
6+
7+
public class EnvironmentVariables {
8+
public static final String CODEQL_EXTRACTOR_JAVASCRIPT_ROOT_ENV_VAR =
9+
"CODEQL_EXTRACTOR_JAVASCRIPT_ROOT";
10+
11+
/**
12+
* Gets the extractor root based on the <code>CODEQL_EXTRACTOR_JAVASCRIPT_ROOT</code> or <code>
13+
* SEMMLE_DIST</code> or environment variable, or <code>null</code> if neither is set.
14+
*/
15+
public static String tryGetExtractorRoot() {
16+
String env = Env.systemEnv().get(CODEQL_EXTRACTOR_JAVASCRIPT_ROOT_ENV_VAR);
17+
if (env != null && !env.isEmpty()) return env;
18+
env = Env.systemEnv().get(Var.SEMMLE_DIST);
19+
if (env != null && !env.isEmpty()) return env;
20+
return null;
21+
}
22+
23+
/**
24+
* Gets the extractor root based on the <code>CODEQL_EXTRACTOR_JAVASCRIPT_ROOT</code> or <code>
25+
* SEMMLE_DIST</code> or environment variable, or throws a UserError if neither is set.
26+
*/
27+
public static String getExtractorRoot() {
28+
String env = tryGetExtractorRoot();
29+
if (env == null) {
30+
throw new UserError("SEMMLE_DIST or CODEQL_EXTRACTOR_JAVASCRIPT_ROOT must be set");
31+
}
32+
return env;
33+
}
34+
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package com.semmle.js.extractor;
22

3-
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.FileInputStream;
6-
import java.io.FileReader;
7-
import java.io.IOException;
8-
import java.nio.charset.Charset;
9-
import java.nio.charset.StandardCharsets;
10-
import java.util.LinkedHashSet;
11-
import java.util.Set;
12-
import java.util.regex.Pattern;
13-
143
import com.semmle.js.extractor.ExtractionMetrics.ExtractionPhase;
154
import com.semmle.js.extractor.trapcache.CachingTrapWriter;
165
import com.semmle.js.extractor.trapcache.ITrapCache;
@@ -21,6 +10,16 @@
2110
import com.semmle.util.io.WholeIO;
2211
import com.semmle.util.trap.TrapWriter;
2312
import com.semmle.util.trap.TrapWriter.Label;
13+
import java.io.BufferedReader;
14+
import java.io.File;
15+
import java.io.FileInputStream;
16+
import java.io.FileReader;
17+
import java.io.IOException;
18+
import java.nio.charset.Charset;
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.LinkedHashSet;
21+
import java.util.Set;
22+
import java.util.regex.Pattern;
2423

2524
/**
2625
* The file extractor extracts a single file and handles source archive population and TRAP caching;
@@ -156,8 +155,7 @@ private boolean hasBadFileHeader(File f, String lcExt, ExtractorConfig config) {
156155
byte[] bytes = new byte[fileHeaderSize];
157156
int length = fis.read(bytes);
158157

159-
if (length == -1)
160-
return false;
158+
if (length == -1) return false;
161159

162160
// Avoid invalid or unprintable UTF-8 files.
163161
if (config.getDefaultEncoding().equals("UTF-8") && hasUnprintableUtf8(bytes, length)) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.gson.JsonParseException;
88
import com.google.gson.JsonParser;
99
import com.google.gson.JsonPrimitive;
10+
import com.semmle.js.extractor.EnvironmentVariables;
1011
import com.semmle.js.extractor.ExtractionMetrics;
1112
import com.semmle.js.parser.JSParser.Result;
1213
import com.semmle.ts.extractor.TypeTable;
@@ -290,14 +291,12 @@ private File getParserWrapper() {
290291
File parserWrapper;
291292
LogbackUtils.getLogger(AbstractProcessBuilder.class).setLevel(Level.INFO);
292293
String explicitPath = Env.systemEnv().get(PARSER_WRAPPER_PATH_ENV_VAR);
293-
String semmleDistVar = Env.systemEnv().get(Env.Var.SEMMLE_DIST.name());
294294
if (explicitPath != null) {
295295
parserWrapper = new File(explicitPath);
296-
} else if (semmleDistVar != null && !semmleDistVar.isEmpty()) {
297-
parserWrapper = new File(semmleDistVar, "tools/typescript-parser-wrapper/main.js");
298296
} else {
299-
throw new CatastrophicError(
300-
"Could not find TypeScript parser: " + Env.Var.SEMMLE_DIST.name() + " is not set.");
297+
parserWrapper =
298+
new File(
299+
EnvironmentVariables.getExtractorRoot(), "tools/typescript-parser-wrapper/main.js");
301300
}
302301
if (!parserWrapper.isFile())
303302
throw new ResourceError(

0 commit comments

Comments
 (0)