Skip to content

JS: Update DiagnosticLocation call to gracefully handle invalid locations #12895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -1237,19 +1238,29 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
List<ParseError> errors = loc == null ? Collections.emptyList() : loc.getParseErrors();
for (ParseError err : errors) {
String msg = "A parse error occurred: " + StringUtil.quoteWithBackticks(err.getMessage().trim())
+ ". 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.";
// file, relative to the source root
DiagnosticLocation.Builder builder = DiagnosticLocation.builder();
+ ".";

Optional<DiagnosticLocation> diagLoc = Optional.empty();
if (file.startsWith(LGTM_SRC)) {
builder = builder.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString());
}
DiagnosticLocation diagLoc = builder
diagLoc = DiagnosticLocation.builder()
.setFile(file.subpath(LGTM_SRC.getNameCount(), file.getNameCount()).toString()) // file, relative to the source root
.setStartLine(err.getPosition().getLine())
.setStartColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
.setEndLine(err.getPosition().getLine())
.setEndColumn(err.getPosition().getColumn() + 1) // convert from 0-based to 1-based
.build();
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc);
.build()
.getOk();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ql/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java:1252: error: cannot find symbol
            .getOk();
            ^
  symbol:   method getOk()
  location: class DiagnosticLocation

Copy link
Contributor Author

@henrymercer henrymercer Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to us changing the DiagnosticLocation API in the corresponding internal PR (backlinked above). The tests are passing there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this PR depends on another PR.

}
if (diagLoc.isPresent()) {
msg += " 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.";
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR, diagLoc.get());
} else {
msg += " The affected file is not located within the code being analyzed."
+ (Env.systemEnv().isActions() ? " Please see the workflow run logs for more information." : "");
writeDiagnostics(msg, JSDiagnosticKind.PARSE_ERROR);
}
}
logEndProcess(start, "Done extracting " + file);
} catch (OutOfMemoryError oom) {
Expand Down