Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Eisenberg f1d500967a Add messy debug statements 2021-05-11 13:13:33 -07:00
Andrew Eisenberg a19f1a7b3c Avoid counting lines of code in parallel with running queries
This can lead to OOME in larger repositories. Unfortunately, running
the LoC serially will cause the action to take longer, but I don't think
there is any other way around this.
2021-05-11 10:57:21 -07:00
4 changed files with 83 additions and 46 deletions
+33 -14
View File
@@ -78,23 +78,18 @@ async function finalizeDatabaseCreation(config, threadsFlag, logger) {
// Runs queries and creates sarif files in the given folder // Runs queries and creates sarif files in the given folder
async function runQueries(sarifFolder, memoryFlag, addSnippetsFlag, threadsFlag, automationDetailsId, config, logger) { async function runQueries(sarifFolder, memoryFlag, addSnippetsFlag, threadsFlag, automationDetailsId, config, logger) {
const statusReport = {}; const statusReport = {};
// count the number of lines in the background
const locPromise = count_loc_1.countLoc(path.resolve(),
// config.paths specifies external directories. the current
// directory is included in the analysis by default. Replicate
// that here.
config.paths, config.pathsIgnore, config.languages, logger);
for (const language of config.languages) { for (const language of config.languages) {
logger.startGroup(`Analyzing ${language}`); logger.startGroup(`Analyzing ${language}`);
const queries = config.queries[language]; const queries = config.queries[language];
if (queries.builtin.length === 0 && queries.custom.length === 0) { if (queries.builtin.length === 0 && queries.custom.length === 0) {
throw new Error(`Unable to analyse ${language} as no queries were selected for this language`); throw new Error(`Unable to analyse ${language} as no queries were selected for this language`);
} }
const allSarifFiles = [];
try { try {
if (queries["builtin"].length > 0) { if (queries["builtin"].length > 0) {
const startTimeBuliltIn = new Date().getTime(); const startTimeBuliltIn = new Date().getTime();
const sarifFile = await runQueryGroup(language, "builtin", queries["builtin"], sarifFolder, undefined); const sarifFile = await runQueryGroup(language, "builtin", queries["builtin"], sarifFolder, undefined);
await injectLinesOfCode(sarifFile, language, locPromise); allSarifFiles.push(sarifFile);
statusReport[`analyze_builtin_queries_${language}_duration_ms`] = statusReport[`analyze_builtin_queries_${language}_duration_ms`] =
new Date().getTime() - startTimeBuliltIn; new Date().getTime() - startTimeBuliltIn;
} }
@@ -105,12 +100,28 @@ async function runQueries(sarifFolder, memoryFlag, addSnippetsFlag, threadsFlag,
if (queries["custom"][i].queries.length > 0) { if (queries["custom"][i].queries.length > 0) {
const sarifFile = await runQueryGroup(language, `custom-${i}`, queries["custom"][i].queries, temporarySarifDir, queries["custom"][i].searchPath); const sarifFile = await runQueryGroup(language, `custom-${i}`, queries["custom"][i].queries, temporarySarifDir, queries["custom"][i].searchPath);
temporarySarifFiles.push(sarifFile); temporarySarifFiles.push(sarifFile);
allSarifFiles.push(sarifFile);
}
}
logger.info("111111111");
logger.info("About to start LoC");
logger.info("111111111");
if (allSarifFiles.length > 0) {
const linesOfCode = await count_loc_1.countLoc(path.resolve(),
// config.paths specifies external directories. the current
// directory is included in the analysis by default. Replicate
// that here.
config.paths, config.pathsIgnore, config.languages, logger);
logger.info("22222222");
logger.info("Finished LoC");
logger.info("22222222");
for (const sarifFile of allSarifFiles) {
injectLinesOfCode(sarifFile, language, linesOfCode);
} }
} }
if (temporarySarifFiles.length > 0) { if (temporarySarifFiles.length > 0) {
const sarifFile = path.join(sarifFolder, `${language}-custom.sarif`); const sarifFile = path.join(sarifFolder, `${language}-custom.sarif`);
fs.writeFileSync(sarifFile, upload_lib_1.combineSarifFiles(temporarySarifFiles)); fs.writeFileSync(sarifFile, upload_lib_1.combineSarifFiles(temporarySarifFiles));
await injectLinesOfCode(sarifFile, language, locPromise);
statusReport[`analyze_custom_queries_${language}_duration_ms`] = statusReport[`analyze_custom_queries_${language}_duration_ms`] =
new Date().getTime() - startTimeCustom; new Date().getTime() - startTimeCustom;
} }
@@ -133,8 +144,17 @@ async function runQueries(sarifFolder, memoryFlag, addSnippetsFlag, threadsFlag,
fs.writeFileSync(querySuitePath, querySuiteContents); fs.writeFileSync(querySuitePath, querySuiteContents);
logger.debug(`Query suite file for ${language}...\n${querySuiteContents}`); logger.debug(`Query suite file for ${language}...\n${querySuiteContents}`);
const sarifFile = path.join(destinationFolder, `${language}-${type}.sarif`); const sarifFile = path.join(destinationFolder, `${language}-${type}.sarif`);
const codeql = codeql_1.getCodeQL(config.codeQLCmd); // const codeql = getCodeQL(config.codeQLCmd);
await codeql.databaseAnalyze(databasePath, sarifFile, searchPath, querySuitePath, memoryFlag, addSnippetsFlag, threadsFlag, automationDetailsId); // await codeql.databaseAnalyze(
// databasePath,
// sarifFile,
// searchPath,
// querySuitePath,
// memoryFlag,
// addSnippetsFlag,
// threadsFlag,
// automationDetailsId
// );
logger.debug(`SARIF results for database ${language} created at "${sarifFile}"`); logger.debug(`SARIF results for database ${language} created at "${sarifFile}"`);
logger.endGroup(); logger.endGroup();
return sarifFile; return sarifFile;
@@ -152,9 +172,8 @@ async function runAnalyze(outputDir, memoryFlag, addSnippetsFlag, threadsFlag, a
return { ...queriesStats }; return { ...queriesStats };
} }
exports.runAnalyze = runAnalyze; exports.runAnalyze = runAnalyze;
async function injectLinesOfCode(sarifFile, language, locPromise) { function injectLinesOfCode(sarifFile, language, linesOfCode) {
const lineCounts = await locPromise; if (language in linesOfCode) {
if (language in lineCounts) {
const sarif = JSON.parse(fs.readFileSync(sarifFile, "utf8")); const sarif = JSON.parse(fs.readFileSync(sarifFile, "utf8"));
if (Array.isArray(sarif.runs)) { if (Array.isArray(sarif.runs)) {
for (const run of sarif.runs) { for (const run of sarif.runs) {
@@ -166,7 +185,7 @@ async function injectLinesOfCode(sarifFile, language, locPromise) {
(r) => { var _a; return r.ruleId === ruleId || ((_a = r.rule) === null || _a === void 0 ? void 0 : _a.id) === ruleId; }); (r) => { var _a; return r.ruleId === ruleId || ((_a = r.rule) === null || _a === void 0 ? void 0 : _a.id) === ruleId; });
// only add the baseline value if the rule already exists // only add the baseline value if the rule already exists
if (rule) { if (rule) {
rule.baseline = lineCounts[language]; rule.baseline = linesOfCode[language];
} }
} }
} }
+1 -1
View File
File diff suppressed because one or more lines are too long
+4 -1
View File
@@ -154,10 +154,13 @@ class LocDir {
ignore: this.exclude, ignore: this.exclude,
nodir: true nodir: true
}); });
console.log(`Excluding: ${this.exclude}`);
const files = []; const files = [];
const info = { ...defaultInfo }; const info = { ...defaultInfo };
let languages = {}; let languages = {};
await Promise.all(paths.map(async (pathItem) => { await Promise.all(paths.map(async (pathItem) => {
console.log(`Processing ${pathItem}`);
const fullPath = slash2_1.default(path_1.default.join(this.cwd, pathItem)); const fullPath = slash2_1.default(path_1.default.join(this.cwd, pathItem));
if (!pathItem || if (!pathItem ||
this.ignoreLanguage(pathItem) || this.ignoreLanguage(pathItem) ||
@@ -205,4 +208,4 @@ function ensureArray(arr, dfault) {
? arr ? arr
: [arr]; : [arr];
} }
//# sourceMappingURL=directory.js.map //# sourceMappingURL=directory.js.map
+45 -30
View File
@@ -146,18 +146,6 @@ export async function runQueries(
): Promise<QueriesStatusReport> { ): Promise<QueriesStatusReport> {
const statusReport: QueriesStatusReport = {}; const statusReport: QueriesStatusReport = {};
// count the number of lines in the background
const locPromise = countLoc(
path.resolve(),
// config.paths specifies external directories. the current
// directory is included in the analysis by default. Replicate
// that here.
config.paths,
config.pathsIgnore,
config.languages,
logger
);
for (const language of config.languages) { for (const language of config.languages) {
logger.startGroup(`Analyzing ${language}`); logger.startGroup(`Analyzing ${language}`);
@@ -168,6 +156,7 @@ export async function runQueries(
); );
} }
const allSarifFiles: string[] = [];
try { try {
if (queries["builtin"].length > 0) { if (queries["builtin"].length > 0) {
const startTimeBuliltIn = new Date().getTime(); const startTimeBuliltIn = new Date().getTime();
@@ -178,7 +167,8 @@ export async function runQueries(
sarifFolder, sarifFolder,
undefined undefined
); );
await injectLinesOfCode(sarifFile, language, locPromise);
allSarifFiles.push(sarifFile);
statusReport[`analyze_builtin_queries_${language}_duration_ms`] = statusReport[`analyze_builtin_queries_${language}_duration_ms`] =
new Date().getTime() - startTimeBuliltIn; new Date().getTime() - startTimeBuliltIn;
@@ -196,12 +186,38 @@ export async function runQueries(
queries["custom"][i].searchPath queries["custom"][i].searchPath
); );
temporarySarifFiles.push(sarifFile); temporarySarifFiles.push(sarifFile);
allSarifFiles.push(sarifFile);
} }
} }
logger.info("111111111");
logger.info("About to start LoC");
logger.info("111111111");
if (allSarifFiles.length > 0) {
const linesOfCode = await countLoc(
path.resolve(),
// config.paths specifies external directories. the current
// directory is included in the analysis by default. Replicate
// that here.
config.paths,
config.pathsIgnore,
config.languages,
logger
);
logger.info("22222222");
logger.info("Finished LoC");
logger.info("22222222");
for (const sarifFile of allSarifFiles) {
injectLinesOfCode(sarifFile, language, linesOfCode);
}
}
if (temporarySarifFiles.length > 0) { if (temporarySarifFiles.length > 0) {
const sarifFile = path.join(sarifFolder, `${language}-custom.sarif`); const sarifFile = path.join(sarifFolder, `${language}-custom.sarif`);
fs.writeFileSync(sarifFile, combineSarifFiles(temporarySarifFiles)); fs.writeFileSync(sarifFile, combineSarifFiles(temporarySarifFiles));
await injectLinesOfCode(sarifFile, language, locPromise);
statusReport[`analyze_custom_queries_${language}_duration_ms`] = statusReport[`analyze_custom_queries_${language}_duration_ms`] =
new Date().getTime() - startTimeCustom; new Date().getTime() - startTimeCustom;
@@ -237,17 +253,17 @@ export async function runQueries(
const sarifFile = path.join(destinationFolder, `${language}-${type}.sarif`); const sarifFile = path.join(destinationFolder, `${language}-${type}.sarif`);
const codeql = getCodeQL(config.codeQLCmd); // const codeql = getCodeQL(config.codeQLCmd);
await codeql.databaseAnalyze( // await codeql.databaseAnalyze(
databasePath, // databasePath,
sarifFile, // sarifFile,
searchPath, // searchPath,
querySuitePath, // querySuitePath,
memoryFlag, // memoryFlag,
addSnippetsFlag, // addSnippetsFlag,
threadsFlag, // threadsFlag,
automationDetailsId // automationDetailsId
); // );
logger.debug( logger.debug(
`SARIF results for database ${language} created at "${sarifFile}"` `SARIF results for database ${language} created at "${sarifFile}"`
@@ -289,13 +305,12 @@ export async function runAnalyze(
return { ...queriesStats }; return { ...queriesStats };
} }
async function injectLinesOfCode( function injectLinesOfCode(
sarifFile: string, sarifFile: string,
language: string, language: string,
locPromise: Promise<Partial<Record<IdPrefixes, number>>> linesOfCode: Partial<Record<IdPrefixes, number>>
) { ) {
const lineCounts = await locPromise; if (language in linesOfCode) {
if (language in lineCounts) {
const sarif = JSON.parse(fs.readFileSync(sarifFile, "utf8")); const sarif = JSON.parse(fs.readFileSync(sarifFile, "utf8"));
if (Array.isArray(sarif.runs)) { if (Array.isArray(sarif.runs)) {
for (const run of sarif.runs) { for (const run of sarif.runs) {
@@ -308,7 +323,7 @@ async function injectLinesOfCode(
); );
// only add the baseline value if the rule already exists // only add the baseline value if the rule already exists
if (rule) { if (rule) {
rule.baseline = lineCounts[language]; rule.baseline = linesOfCode[language];
} }
} }
} }