Skip to content

Commit d53abd8

Browse files
Make suggested changes, build currently failing
1 parent c3eca5b commit d53abd8

9 files changed

Lines changed: 103 additions & 55 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
## [UNRELEASED]
44

5-
- Fix the _CodeQL: Open Referenced File_ command for Windows systems. [#979](https://github.com/github/vscode-codeql/pull/979)
65
- Fix the _CodeQL: Open Referenced File_ command for Windows systems. [#979](https://github.com/github/vscode-codeql/pull/979)
7-
- Fix a bug that causes VSCode to crash when handling large SARIF files (>4GB) [#1004](https://github.com/github/vscode-codeql/pull/1004)
6+
- Support large SARIF results files (>4GB) without crashing VS Code. [#1004](https://github.com/github/vscode-codeql/pull/1004)
87
- Fix a bug that shows 'Set current database' when hovering over the currently selected database in the databases view. [#976](https://github.com/github/vscode-codeql/pull/976)
98
- Fix a bug with importing large databases. Databases over 4GB can now be imported directly from LGTM or from a zip file. This functionality is only available when using CodeQL CLI version 2.6.0 or later. [#971](https://github.com/github/vscode-codeql/pull/971)
109
- Replace certain control codes (`U+0000` - `U+001F`) with their corresponding control labels (`U+2400` - `U+241F`) in the results view. [#963](https://github.com/github/vscode-codeql/pull/963)

extensions/ql-vscode/package-lock.json

Lines changed: 32 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@
969969
"react": "^16.8.6",
970970
"react-dom": "^16.8.6",
971971
"semver": "~7.3.2",
972+
"stream": "^0.0.2",
972973
"stream-chain": "~2.2.4",
973974
"stream-json": "~1.7.3",
974975
"tmp": "^0.1.0",

extensions/ql-vscode/src/cli.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import * as cpp from 'child-process-promise';
22
import * as child_process from 'child_process';
3-
import * as fs from 'fs-extra';
43
import * as path from 'path';
5-
import { parser } from 'stream-json';
6-
import { pick } from 'stream-json/filters/Pick';
7-
import Assembler = require('stream-json/Assembler');
8-
import { chain } from 'stream-chain';
94
import * as sarif from 'sarif';
105
import { SemVer } from 'semver';
116
import { Readable } from 'stream';
@@ -21,6 +16,7 @@ import { assertNever } from './pure/helpers-pure';
2116
import { QueryMetadata, SortDirection } from './pure/interface-types';
2217
import { Logger, ProgressReporter } from './logging';
2318
import { CompilationMessage } from './pure/messages';
19+
import { parseSarif } from './pure/sarif-utils';
2420
import { dbSchemeToLanguage } from './helpers';
2521

2622
/**
@@ -38,8 +34,6 @@ const CSV_FORMAT = 'csv';
3834
*/
3935
const LOGGING_FLAGS = ['-v', '--log-to-stderr'];
4036

41-
const DUMMY_TOOL : sarif.Tool = {driver: {name: ''}};
42-
4337
/**
4438
* The expected output of `codeql resolve library-path`.
4539
*/
@@ -582,46 +576,6 @@ export class CodeQLCliServer implements Disposable {
582576
}
583577
}
584578

585-
static async parseSarif(interpretedResultsPath: string) : Promise<sarif.Log> {
586-
try {
587-
// Parse the SARIF file into token streams, filtering out only the results array.
588-
const p = parser();
589-
const pipeline = chain([
590-
fs.createReadStream(interpretedResultsPath),
591-
p,
592-
pick({filter: 'runs.0.results'})
593-
]);
594-
595-
// Creates JavaScript objects from the token stream
596-
const asm = Assembler.connectTo(pipeline);
597-
598-
// Returns a constructed Log object with the results or an empty array if no results were found.
599-
// If the parser fails for any reason, it will reject the promise.
600-
return await new Promise((resolve, reject) => {
601-
pipeline.on('error', (error) => {
602-
reject(error);
603-
});
604-
605-
asm.on('done', (asm) => {
606-
607-
const log : sarif.Log = {
608-
version: '2.1.0',
609-
runs: [
610-
{
611-
tool: DUMMY_TOOL,
612-
results: asm.current ?? []
613-
}
614-
]
615-
};
616-
617-
resolve(log);
618-
});
619-
});
620-
} catch (err) {
621-
throw new Error(`Parsing output of interpretation failed: ${err.stderr || err}`);
622-
}
623-
}
624-
625579
/**
626580
* Gets the metadata for a query.
627581
* @param queryPath The path to the query.
@@ -728,7 +682,7 @@ export class CodeQLCliServer implements Disposable {
728682

729683
async interpretBqrs(metadata: QueryMetadata, resultsPath: string, interpretedResultsPath: string, sourceInfo?: SourceInfo): Promise<sarif.Log> {
730684
await this.runInterpretCommand(SARIF_FORMAT, metadata, resultsPath, interpretedResultsPath, sourceInfo);
731-
return await CodeQLCliServer.parseSarif(interpretedResultsPath);
685+
return await parseSarif(interpretedResultsPath);
732686
}
733687

734688
async generateResultsCsv(metadata: QueryMetadata, resultsPath: string, csvPath: string, sourceInfo?: SourceInfo): Promise<void> {

extensions/ql-vscode/src/pure/sarif-utils.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import * as Sarif from 'sarif';
2+
import * as fs from 'fs-extra';
3+
import { parser } from 'stream-json';
4+
import { pick } from 'stream-json/filters/Pick';
5+
import Assembler = require('stream-json/Assembler');
6+
import { chain } from 'stream-chain';
7+
28
import { ResolvableLocationValue } from './bqrs-cli-types';
39

10+
const DUMMY_TOOL : Sarif.Tool = {driver: {name: ''}};
11+
412
export interface SarifLink {
513
dest: number;
614
text: string;
@@ -156,6 +164,46 @@ export function parseSarifLocation(
156164
}
157165
}
158166

167+
export async function parseSarif(interpretedResultsPath: string) : Promise<Sarif.Log> {
168+
try {
169+
// Parse the SARIF file into token streams, filtering out only the results array.
170+
const p = parser();
171+
const pipeline = chain([
172+
fs.createReadStream(interpretedResultsPath),
173+
p,
174+
pick({filter: 'runs.0.results'})
175+
]);
176+
177+
// Creates JavaScript objects from the token stream
178+
const asm = Assembler.connectTo(pipeline);
179+
180+
// Returns a constructed Log object with the results or an empty array if no results were found.
181+
// If the parser fails for any reason, it will reject the promise.
182+
return await new Promise((resolve, reject) => {
183+
pipeline.on('error', (error) => {
184+
reject(error);
185+
});
186+
187+
asm.on('done', (asm) => {
188+
189+
const log : Sarif.Log = {
190+
version: '2.1.0',
191+
runs: [
192+
{
193+
tool: DUMMY_TOOL,
194+
results: asm.current ?? []
195+
}
196+
]
197+
};
198+
199+
resolve(log);
200+
});
201+
});
202+
} catch (err) {
203+
throw new Error(`Parsing output of interpretation failed: ${err.stderr || err}`);
204+
}
205+
}
206+
159207
export function isNoLocation(loc: ParsedSarifLocation): loc is NoLocation {
160208
return 'hint' in loc;
161209
}

extensions/ql-vscode/test/pure-tests/sarif-utils.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import 'mocha';
22
import { expect } from 'chai';
33
import * as Sarif from 'sarif';
4+
import * as path from 'path';
45

56
import {
67
getPathRelativeToSourceLocationPrefix,
78
parseSarifLocation,
89
parseSarifPlainTextMessage,
9-
unescapeSarifText
10+
unescapeSarifText,
11+
parseSarif
1012
} from '../../src/pure/sarif-utils';
1113

1214

1315
describe('parsing sarif', () => {
16+
const sarifDir = path.join(path.dirname(__dirname), 'sarif');
17+
1418
it('should be able to parse a simple message from the spec', async function() {
1519
const message = 'Tainted data was used. The data came from [here](3).';
1620
const results = parseSarifPlainTextMessage(message);
@@ -60,6 +64,20 @@ describe('parsing sarif', () => {
6064
.to.eq('file:/a/b/c/?x=test');
6165
});
6266

67+
it('should parse a valid SARIF file', async () => {
68+
const result = await parseSarif(path.join(sarifDir, 'validSarif.sarif'));
69+
expect(result.version).to.exist;
70+
expect(result.runs).to.exist;
71+
expect(result.runs[0].tool).to.exist;
72+
expect(result.runs[0].tool.driver).to.exist;
73+
expect(result.runs.length).to.be.at.least(1);
74+
});
75+
76+
it('should return an empty array if there are no results', async () => {
77+
const result = await parseSarif(path.join(sarifDir, 'emptyResultsSarif.sarif'));
78+
expect(result.runs[0].results).to.be.empty;
79+
});
80+
6381
describe('parseSarifLocation', () => {
6482
it('should parse a sarif location with "no location"', () => {
6583
expect(parseSarifLocation({}, '')).to.deep.equal({

extensions/ql-vscode/src/vscode-tests/no-workspace/data/sarif/emptyResultsSarif.sarif renamed to extensions/ql-vscode/test/sarif/emptyResultsSarif.sarif

File renamed without changes.

extensions/ql-vscode/src/vscode-tests/no-workspace/data/sarif/invalidSarif.sarif renamed to extensions/ql-vscode/test/sarif/invalidSarif.sarif

File renamed without changes.

extensions/ql-vscode/src/vscode-tests/no-workspace/data/sarif/validSarif.sarif renamed to extensions/ql-vscode/test/sarif/validSarif.sarif

File renamed without changes.

0 commit comments

Comments
 (0)