Merge branch 'main' into update-bundle/codeql-bundle-v2.16.0

This commit is contained in:
Alexander Eyers-Taylor 2024-01-15 15:55:29 +00:00 committed by GitHub
commit 1fea7a57e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
113 changed files with 736 additions and 345 deletions

View file

@ -49,7 +49,11 @@ export async function uploadDebugArtifacts(
sanitizeArifactName(`${artifactName}${suffix}`),
toUpload.map((file) => path.normalize(file)),
path.normalize(rootDir),
{ continueOnError: true },
{
continueOnError: true,
// ensure we don't keep the debug artifacts around for too long since they can be large.
retentionDays: 7,
},
);
}

32
src/init.test.ts Normal file
View file

@ -0,0 +1,32 @@
import test from "ava";
import { Config } from "./config-utils";
import { printPathFiltersWarning } from "./init";
import { Language } from "./languages";
import { LoggedMessage, getRecordingLogger, setupTests } from "./testing-utils";
setupTests(test);
test("printPathFiltersWarning does not trigger when 'paths' and 'paths-ignore' are undefined", async (t) => {
const messages: LoggedMessage[] = [];
printPathFiltersWarning(
{
languages: [Language.cpp],
originalUserInput: {},
} as Partial<Config> as Config,
getRecordingLogger(messages),
);
t.is(messages.length, 0);
});
test("printPathFiltersWarning does not trigger when 'paths' and 'paths-ignore' are empty", async (t) => {
const messages: LoggedMessage[] = [];
printPathFiltersWarning(
{
languages: [Language.cpp],
originalUserInput: { paths: [], "paths-ignore": [] },
} as Partial<Config> as Config,
getRecordingLogger(messages),
);
t.is(messages.length, 0);
});

View file

@ -124,15 +124,18 @@ export async function runInit(
} catch (e) {
throw processError(e);
}
return await getCombinedTracerConfig(config);
return await getCombinedTracerConfig(await codeql.getVersion(), config);
}
function printPathFiltersWarning(config: configUtils.Config, logger: Logger) {
export function printPathFiltersWarning(
config: configUtils.Config,
logger: Logger,
) {
// Index include/exclude/filters only work in javascript/python/ruby.
// If any other languages are detected/configured then show a warning.
if (
(config.originalUserInput.paths?.length !== 0 ||
config.originalUserInput["paths-ignore"]?.length !== 0) &&
(config.originalUserInput.paths?.length ||
config.originalUserInput["paths-ignore"]?.length) &&
!config.languages.every(isScannedLanguage)
) {
logger.warning(

View file

@ -211,14 +211,21 @@ export function mockLanguagesInRepo(languages: string[]) {
/**
* Constructs a `VersionInfo` object for testing purposes only.
*/
export const makeVersionInfo = (version: string): CodeQL.VersionInfo => ({
export const makeVersionInfo = (
version: string,
features?: { [name: string]: boolean },
): CodeQL.VersionInfo => ({
version,
features,
});
export function mockCodeQLVersion(version: string) {
export function mockCodeQLVersion(
version: string,
features?: { [name: string]: boolean },
) {
return {
async getVersion() {
return makeVersionInfo(version);
return makeVersionInfo(version, features);
},
} as CodeQL.CodeQL;
}

View file

@ -22,3 +22,17 @@ test("isSupportedToolsFeature", async (t) => {
),
);
});
test("setsCodeqlRunnerEnvVar", async (t) => {
const versionInfo = makeVersionInfo("1.0.0");
t.false(
isSupportedToolsFeature(versionInfo, ToolsFeature.SetsCodeqlRunnerEnvVar),
);
versionInfo.features = { setsCodeqlRunnerEnvVar: true };
t.true(
isSupportedToolsFeature(versionInfo, ToolsFeature.SetsCodeqlRunnerEnvVar),
);
});

View file

@ -2,6 +2,7 @@ import { VersionInfo } from "./codeql";
export enum ToolsFeature {
IndirectTracingSupportsStaticBinaries = "indirectTracingSupportsStaticBinaries",
SetsCodeqlRunnerEnvVar = "setsCodeqlRunnerEnvVar",
}
/**

View file

@ -5,7 +5,7 @@ import test from "ava";
import * as configUtils from "./config-utils";
import { Language } from "./languages";
import { setupTests } from "./testing-utils";
import { makeVersionInfo, setupTests } from "./testing-utils";
import { getCombinedTracerConfig } from "./tracer-config";
import * as util from "./util";
@ -33,7 +33,10 @@ test("getCombinedTracerConfig - return undefined when no languages are traced la
const config = getTestConfig(tmpDir);
// No traced languages
config.languages = [Language.javascript, Language.python];
t.deepEqual(await getCombinedTracerConfig(config), undefined);
t.deepEqual(
await getCombinedTracerConfig(makeVersionInfo("1.0.0"), config),
undefined,
);
});
});
@ -66,7 +69,10 @@ test("getCombinedTracerConfig - with start-tracing.json environment file", async
);
fs.writeFileSync(startTracingJson, JSON.stringify(startTracingEnv));
const result = await getCombinedTracerConfig(config);
const result = await getCombinedTracerConfig(
makeVersionInfo("1.0.0"),
config,
);
t.notDeepEqual(result, undefined);
const expectedEnv = startTracingEnv;
@ -93,3 +99,42 @@ test("getCombinedTracerConfig - with start-tracing.json environment file", async
});
});
});
test("getCombinedTracerConfig - with SetsCodeqlRunnerEnvVar feature enabled in CLI", async (t) => {
await util.withTmpDir(async (tmpDir) => {
const config = getTestConfig(tmpDir);
const bundlePath = path.join(tmpDir, "bundle");
const codeqlPlatform =
process.platform === "win32"
? "win64"
: process.platform === "darwin"
? "osx64"
: "linux64";
const startTracingEnv = {
foo: "bar",
CODEQL_DIST: bundlePath,
CODEQL_PLATFORM: codeqlPlatform,
};
const tracingEnvironmentDir = path.join(
config.dbLocation,
"temp",
"tracingEnvironment",
);
fs.mkdirSync(tracingEnvironmentDir, { recursive: true });
const startTracingJson = path.join(
tracingEnvironmentDir,
"start-tracing.json",
);
fs.writeFileSync(startTracingJson, JSON.stringify(startTracingEnv));
const result = await getCombinedTracerConfig(
makeVersionInfo("1.0.0", { setsCodeqlRunnerEnvVar: true }),
config,
);
t.notDeepEqual(result, undefined);
t.false(Object.prototype.hasOwnProperty.call(result?.env, "CODEQL_RUNNER"));
});
});

View file

@ -1,8 +1,10 @@
import * as fs from "fs";
import * as path from "path";
import { VersionInfo } from "./codeql";
import * as configUtils from "./config-utils";
import { isTracedLanguage } from "./languages";
import { ToolsFeature, isSupportedToolsFeature } from "./tools-features";
export type TracerConfig = {
env: { [key: string]: string };
@ -59,6 +61,7 @@ export async function getTracerConfigForCluster(
}
export async function getCombinedTracerConfig(
versionInfo: VersionInfo,
config: configUtils.Config,
): Promise<TracerConfig | undefined> {
// Abort if there are no traced languages as there's nothing to do
@ -69,17 +72,25 @@ export async function getCombinedTracerConfig(
const mainTracerConfig = await getTracerConfigForCluster(config);
// On macos it's necessary to prefix the build command with the runner executable
// on order to trace when System Integrity Protection is enabled.
// The executable also exists and works for other platforms so we output this env
// var with a path to the runner regardless so it's always available.
const runnerExeName = process.platform === "win32" ? "runner.exe" : "runner";
mainTracerConfig.env["CODEQL_RUNNER"] = path.join(
mainTracerConfig.env["CODEQL_DIST"],
"tools",
mainTracerConfig.env["CODEQL_PLATFORM"],
runnerExeName,
);
// If the CLI doesn't yet support setting the CODEQL_RUNNER environment variable to
// the runner executable path, we set it here in the Action.
if (
!isSupportedToolsFeature(versionInfo, ToolsFeature.SetsCodeqlRunnerEnvVar)
) {
// On MacOS when System Integrity Protection is enabled, it's necessary to prefix
// the build command with the runner executable for indirect tracing, so we expose
// it here via the CODEQL_RUNNER environment variable.
// The executable also exists and works for other platforms so we unconditionally
// set the environment variable.
const runnerExeName =
process.platform === "win32" ? "runner.exe" : "runner";
mainTracerConfig.env["CODEQL_RUNNER"] = path.join(
mainTracerConfig.env["CODEQL_DIST"],
"tools",
mainTracerConfig.env["CODEQL_PLATFORM"],
runnerExeName,
);
}
return mainTracerConfig;
}