Skip to content

Commit 1ae116c

Browse files
authored
Merge pull request github#12895 from github/henrymercer/diagnostics-verify-one-based
JS: Update `DiagnosticLocation` call to gracefully handle invalid locations
2 parents 128d102 + d7474f9 commit 1ae116c

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Optional;
2930
import java.util.Set;
3031
import java.util.concurrent.CompletableFuture;
3132
import java.util.concurrent.ExecutorService;
@@ -1237,19 +1238,29 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
12371238
List<ParseError> errors = loc == null ? Collections.emptyList() : loc.getParseErrors();
12381239
for (ParseError err : errors) {
12391240
String msg = "A parse error occurred: " + StringUtil.quoteWithBackticks(err.getMessage().trim())
1240-
+ ". Check the syntax of the file. If the file is invalid, correct the error or [exclude](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning) the file from analysis.";
1241-
// file, relative to the source root
1242-
DiagnosticLocation.Builder builder = DiagnosticLocation.builder();
1241+
+ ".";
1242+
1243+
Optional<DiagnosticLocation> diagLoc = Optional.empty();
12431244
if (file.startsWith(LGTM_SRC)) {
1244-
builder = builder.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString());
1245-
}
1246-
DiagnosticLocation diagLoc = builder
1245+
diagLoc = DiagnosticLocation.builder()
1246+
.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString()) // file, relative to the source root
12471247
.setStartLine(err.getPosition().getLine())
12481248
.setStartColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
12491249
.setEndLine(err.getPosition().getLine())
12501250
.setEndColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
1251-
.build();
1252-
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc);
1251+
.build()
1252+
.getOk();
1253+
}
1254+
if (diagLoc.isPresent()) {
1255+
msg += " Check the syntax of the file. If the file is invalid, correct the error or "
1256+
+ "[exclude](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-"
1257+
+ "your-code-for-vulnerabilities-and-errors/customizing-code-scanning) the file from analysis.";
1258+
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc.get());
1259+
} else {
1260+
msg += " The affected file is not located within the code being analyzed."
1261+
+ (Env.systemEnv().isActions() ? " Please see the workflow run logs for more information." : "");
1262+
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR);
1263+
}
12531264
}
12541265
logEndProcess(start, "Done extracting " + file);
12551266
} catch (OutOfMemoryError oom) {

0 commit comments

Comments
 (0)