Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 28 additions & 45 deletions lib/entry-points.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 33 additions & 48 deletions src/codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,16 +514,13 @@ async function getCodeQLForCmd(
async getVersion() {
let result = util.getCachedCodeQlVersion(cmd);
if (result === undefined) {
const output = await runCli(cmd, ["version", "--format=json"], {
noStreamStdout: true,
});
try {
result = JSON.parse(output) as VersionInfo;
} catch {
throw Error(
`Invalid JSON output from \`version --format=json\`: ${output}`,
);
}
result = await runCliJson<VersionInfo>(
cmd,
["version", "--format=json"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could the --format=json or --format=betterjson argument be added automatically by runCliJson?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That would be nice! Unfortunately, there doesn't seem to be consistency between all of the calls of runCliJson:

  • getVersion, resolveDatabase use --format=json
  • resolveLanguages uses --format=betterjson
  • resolveBuildEnvironment uses nothing??
  • resolveQueriesStartingPacks uses --format=startingpacks

{
noStreamStdout: true,
},
);
util.cacheCodeQlVersion(cmd, result);
}
return result;
Expand Down Expand Up @@ -731,26 +728,20 @@ async function getCodeQLForCmd(
filterToLanguagesWithQueries: boolean;
} = { filterToLanguagesWithQueries: false },
) {
const codeqlArgs = [
return runCliJson<ResolveLanguagesOutput>(cmd, [
"resolve",
"languages",
"--format=betterjson",
"--extractor-options-verbosity=4",
"--extractor-include-aliases",
// TODO: Unconditionally include `--filter-to-languages-with-queries`
// once CODEQL_MINIMUM_VERSION is at least v2.23.0
// — the first version to support this flag.
Comment on lines +737 to +739

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Elaborating on my comment on the other PR, what does this comment have to do with the changes here? Just a drive-by improvement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what does this comment have to do with the changes here? Just a drive-by improvement?

Nothing, really—a "drive-by improvement" is a nice way to word it. But, it seemed more relevant here than in #3950. I'm fine to delete it, but I think it adds some value.

...(filterToLanguagesWithQueries
? ["--filter-to-languages-with-queries"]
: []),
...getExtraOptionsFromEnv(["resolve", "languages"]),
];
const output = await runCli(cmd, codeqlArgs);

try {
return JSON.parse(output) as ResolveLanguagesOutput;
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve languages with --format=betterjson: ${e}`,
);
}
]);
},
async resolveBuildEnvironment(
workingDir: string | undefined,
Expand All @@ -766,15 +757,7 @@ async function getCodeQLForCmd(
if (workingDir !== undefined) {
codeqlArgs.push("--working-dir", workingDir);
}
const output = await runCli(cmd, codeqlArgs);

try {
return JSON.parse(output) as ResolveBuildEnvironmentOutput;
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve build-environment: ${e} in\n${output}`,
);
}
return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs);
},
async databaseRunQueries(
databasePath: string,
Expand Down Expand Up @@ -976,15 +959,9 @@ async function getCodeQLForCmd(
...getExtraOptionsFromEnv(["resolve", "queries"]),
...queries,
];
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });

try {
return JSON.parse(output) as string[];
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve queries --format=startingpacks: ${e}`,
);
}
return await runCliJson<string[]>(cmd, codeqlArgs, {
noStreamStdout: true,
});
},
async resolveDatabase(
databasePath: string,
Expand All @@ -996,15 +973,9 @@ async function getCodeQLForCmd(
"--format=json",
...getExtraOptionsFromEnv(["resolve", "database"]),
];
const output = await runCli(cmd, codeqlArgs, { noStreamStdout: true });

try {
return JSON.parse(output) as ResolveDatabaseOutput;
} catch (e) {
throw new Error(
`Unexpected output from codeql resolve database --format=json: ${e}`,
);
}
return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, {
noStreamStdout: true,
});
},
async mergeResults(
sarifFiles: string[],
Expand Down Expand Up @@ -1160,6 +1131,20 @@ async function runCli(
}
}

async function runCliJson<T>(
Comment thread
mario-campos marked this conversation as resolved.
cmd: string,
args: string[] = [],
opts: { stdin?: string; noStreamStdout?: boolean } = {},
): Promise<T> {
Comment thread
mario-campos marked this conversation as resolved.
const output = await runCli(cmd, args, opts);
try {
return JSON.parse(output) as T;
} catch {
const command = [cmd, ...args].join(" ");
throw Error(`Invalid JSON output from \`${command}\`: ${output}`);
Comment thread
mario-campos marked this conversation as resolved.
Outdated
}
Comment thread
mario-campos marked this conversation as resolved.
}

/**
* Writes the code scanning configuration that is to be used by the CLI.
*
Expand Down
Loading