Capture stderr from extracting .tar.zst

This commit is contained in:
Henry Mercer 2024-10-02 15:32:34 +01:00
parent 1aa7f6f05d
commit 3da852e107
6 changed files with 147 additions and 28 deletions

12
lib/setup-codeql.js generated
View file

@ -373,7 +373,7 @@ async function tryGetFallbackToolcacheVersion(cliVersion, tagName, logger) {
} }
// Exported using `export const` for testing purposes. Specifically, we want to // Exported using `export const` for testing purposes. Specifically, we want to
// be able to stub this function and have other functions in this file use that stub. // be able to stub this function and have other functions in this file use that stub.
const downloadCodeQL = async function (codeqlURL, maybeBundleVersion, maybeCliVersion, apiDetails, tempDir, logger) { const downloadCodeQL = async function (codeqlURL, maybeBundleVersion, maybeCliVersion, apiDetails, tarVersion, tempDir, logger) {
const parsedCodeQLURL = new URL(codeqlURL); const parsedCodeQLURL = new URL(codeqlURL);
const searchParams = new URLSearchParams(parsedCodeQLURL.search); const searchParams = new URLSearchParams(parsedCodeQLURL.search);
const headers = { const headers = {
@ -405,7 +405,7 @@ const downloadCodeQL = async function (codeqlURL, maybeBundleVersion, maybeCliVe
logger.debug(`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`); logger.debug(`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`);
logger.debug("Extracting CodeQL bundle."); logger.debug("Extracting CodeQL bundle.");
const extractionStart = perf_hooks_1.performance.now(); const extractionStart = perf_hooks_1.performance.now();
const extractedBundlePath = await tar.extract(archivedBundlePath, compressionMethod); const extractedBundlePath = await tar.extract(archivedBundlePath, compressionMethod, tarVersion);
const extractionDurationMs = Math.round(perf_hooks_1.performance.now() - extractionStart); const extractionDurationMs = Math.round(perf_hooks_1.performance.now() - extractionStart);
logger.debug(`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`); logger.debug(`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`);
await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger); await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger);
@ -492,6 +492,10 @@ async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defau
} }
catch (e) { catch (e) {
zstdFailureReason = util.getErrorMessage(e) || "unknown error"; zstdFailureReason = util.getErrorMessage(e) || "unknown error";
if (e instanceof actions_util_1.CommandInvocationError) {
zstdFailureReason += ` Full error: ${e.stderr}`;
logger.debug(`Invocation output the following to stderr: ${e.stderr}`);
}
logger.warning(`Failed to set up CodeQL tools with zstd. Falling back to gzipped version. Error: ${util.getErrorMessage(e)}`); logger.warning(`Failed to set up CodeQL tools with zstd. Falling back to gzipped version. Error: ${util.getErrorMessage(e)}`);
} }
} }
@ -510,7 +514,7 @@ async function setupCodeQLBundleWithCompressionMethod(toolsInput, apiDetails, te
switch (source.sourceType) { switch (source.sourceType) {
case "local": { case "local": {
const compressionMethod = tar.inferCompressionMethod(source.codeqlTarPath); const compressionMethod = tar.inferCompressionMethod(source.codeqlTarPath);
codeqlFolder = await tar.extract(source.codeqlTarPath, compressionMethod); codeqlFolder = await tar.extract(source.codeqlTarPath, compressionMethod, zstdAvailability.version);
toolsSource = ToolsSource.Local; toolsSource = ToolsSource.Local;
break; break;
} }
@ -520,7 +524,7 @@ async function setupCodeQLBundleWithCompressionMethod(toolsInput, apiDetails, te
toolsSource = ToolsSource.Toolcache; toolsSource = ToolsSource.Toolcache;
break; break;
case "download": { case "download": {
const result = await (0, exports.downloadCodeQL)(source.codeqlURL, source.bundleVersion, source.cliVersion, apiDetails, tempDir, logger); const result = await (0, exports.downloadCodeQL)(source.codeqlURL, source.bundleVersion, source.cliVersion, apiDetails, zstdAvailability.version, tempDir, logger);
toolsVersion = result.toolsVersion; toolsVersion = result.toolsVersion;
codeqlFolder = result.codeqlFolder; codeqlFolder = result.codeqlFolder;
toolsDownloadStatusReport = result.statusReport; toolsDownloadStatusReport = result.statusReport;

File diff suppressed because one or more lines are too long

66
lib/tar.js generated
View file

@ -22,13 +22,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod); __setModuleDefault(result, mod);
return result; return result;
}; };
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.isZstdAvailable = isZstdAvailable; exports.isZstdAvailable = isZstdAvailable;
exports.extract = extract; exports.extract = extract;
exports.extractTarZst = extractTarZst;
exports.inferCompressionMethod = inferCompressionMethod; exports.inferCompressionMethod = inferCompressionMethod;
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const toolrunner_1 = require("@actions/exec/lib/toolrunner"); const toolrunner_1 = require("@actions/exec/lib/toolrunner");
const toolcache = __importStar(require("@actions/tool-cache")); const toolcache = __importStar(require("@actions/tool-cache"));
const safe_which_1 = require("@chrisgavin/safe-which"); const safe_which_1 = require("@chrisgavin/safe-which");
const uuid_1 = require("uuid");
const actions_util_1 = require("./actions-util");
const util_1 = require("./util"); const util_1 = require("./util");
const MIN_REQUIRED_BSD_TAR_VERSION = "3.4.3"; const MIN_REQUIRED_BSD_TAR_VERSION = "3.4.3";
const MIN_REQUIRED_GNU_TAR_VERSION = "1.31"; const MIN_REQUIRED_GNU_TAR_VERSION = "1.31";
@ -90,21 +98,59 @@ async function isZstdAvailable(logger) {
return { available: false }; return { available: false };
} }
} }
async function extract(path, compressionMethod) { async function extract(tarPath, compressionMethod, tarVersion) {
switch (compressionMethod) { switch (compressionMethod) {
case "gzip": case "gzip":
// While we could also ask tar to autodetect the compression method, // Defensively continue to call the toolcache API as requesting a gzipped
// we defensively keep the gzip call identical as requesting a gzipped // bundle may be a fallback option.
// bundle will soon be a fallback option. return await toolcache.extractTar(tarPath);
return await toolcache.extractTar(path);
case "zstd": case "zstd":
// By specifying only the "x" flag, we ask tar to autodetect the if (!tarVersion) {
// compression method. throw new Error("Could not determine tar version, which is required to extract a Zstandard archive.");
return await toolcache.extractTar(path, undefined, "x"); }
return await extractTarZst(tarPath, tarVersion);
} }
} }
function inferCompressionMethod(path) { /**
if (path.endsWith(".tar.gz")) { * Extract a compressed tar archive
*
* @param file path to the tar
* @param dest destination directory. Optional.
* @returns path to the destination directory
*/
async function extractTarZst(file, tarVersion) {
if (!file) {
throw new Error("parameter 'file' is required");
}
// Create dest
const dest = await createExtractFolder();
// Initialize args
const args = ["-x", "-v"];
let destArg = dest;
let fileArg = file;
if (process.platform === "win32" && tarVersion.type === "gnu") {
args.push("--force-local");
destArg = dest.replace(/\\/g, "/");
// Technically only the dest needs to have `/` but for aesthetic consistency
// convert slashes in the file arg too.
fileArg = file.replace(/\\/g, "/");
}
if (tarVersion.type === "gnu") {
// Suppress warnings when using GNU tar to extract archives created by BSD tar
args.push("--warning=no-unknown-keyword");
args.push("--overwrite");
}
args.push("-C", destArg, "-f", fileArg);
await (0, actions_util_1.runTool)(`tar`, args);
return dest;
}
async function createExtractFolder() {
const dest = path_1.default.join((0, actions_util_1.getTemporaryDirectory)(), (0, uuid_1.v4)());
fs.mkdirSync(dest, { recursive: true });
return dest;
}
function inferCompressionMethod(tarPath) {
if (tarPath.endsWith(".tar.gz")) {
return "gzip"; return "gzip";
} }
return "zstd"; return "zstd";

View file

@ -1 +1 @@
{"version":3,"file":"tar.js","sourceRoot":"","sources":["../src/tar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,0CA4BC;AAID,0BAeC;AAED,wDAKC;AA3GD,6DAA0D;AAC1D,+DAAiD;AACjD,uDAAmD;AAGnD,iCAAqC;AAErC,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAC7C,MAAM,4BAA4B,GAAG,MAAM,CAAC;AAO5C,KAAK,UAAU,aAAa;IAC1B,MAAM,GAAG,GAAG,MAAM,IAAA,sBAAS,EAAC,KAAK,CAAC,CAAC;IACnC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,MAAM,QAAQ,GAAG,MAAM,IAAI,uBAAU,CAAC,GAAG,EAAE,CAAC,WAAW,CAA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,EAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAE3B,OAAO,IAAI,CAAC;AACd,CAAC;AAED,KAAK,UAAU,mBAAmB;IAChC,MAAM,IAAI,GAAG,cAAI,CAAC,IAAI,CAAC,IAAA,oCAAqB,GAAE,EAAE,IAAA,SAAM,GAAE,CAAC,CAAC;IAC1D,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACxC,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,sBAAsB,CAAC,OAAe;IACpD,IAAI,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAChC,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}

View file

@ -9,7 +9,7 @@ import { default as deepEqual } from "fast-deep-equal";
import * as semver from "semver"; import * as semver from "semver";
import { v4 as uuidV4 } from "uuid"; import { v4 as uuidV4 } from "uuid";
import { isRunningLocalAction } from "./actions-util"; import { CommandInvocationError, isRunningLocalAction } from "./actions-util";
import * as api from "./api-client"; import * as api from "./api-client";
// Note: defaults.json is referenced from the CodeQL Action sync tool and the Actions runner image // Note: defaults.json is referenced from the CodeQL Action sync tool and the Actions runner image
// creation scripts. Ensure that any changes to the format of this file are compatible with both of // creation scripts. Ensure that any changes to the format of this file are compatible with both of
@ -497,6 +497,7 @@ export const downloadCodeQL = async function (
maybeBundleVersion: string | undefined, maybeBundleVersion: string | undefined,
maybeCliVersion: string | undefined, maybeCliVersion: string | undefined,
apiDetails: api.GitHubApiDetails, apiDetails: api.GitHubApiDetails,
tarVersion: tar.TarVersion | undefined,
tempDir: string, tempDir: string,
logger: Logger, logger: Logger,
): Promise<{ ): Promise<{
@ -554,6 +555,7 @@ export const downloadCodeQL = async function (
const extractedBundlePath = await tar.extract( const extractedBundlePath = await tar.extract(
archivedBundlePath, archivedBundlePath,
compressionMethod, compressionMethod,
tarVersion,
); );
const extractionDurationMs = Math.round(performance.now() - extractionStart); const extractionDurationMs = Math.round(performance.now() - extractionStart);
logger.debug( logger.debug(
@ -700,6 +702,10 @@ export async function setupCodeQLBundle(
); );
} catch (e) { } catch (e) {
zstdFailureReason = util.getErrorMessage(e) || "unknown error"; zstdFailureReason = util.getErrorMessage(e) || "unknown error";
if (e instanceof CommandInvocationError) {
zstdFailureReason += ` Full error: ${e.stderr}`;
logger.debug(`Invocation output the following to stderr: ${e.stderr}`);
}
logger.warning( logger.warning(
`Failed to set up CodeQL tools with zstd. Falling back to gzipped version. Error: ${util.getErrorMessage( `Failed to set up CodeQL tools with zstd. Falling back to gzipped version. Error: ${util.getErrorMessage(
e, e,
@ -755,7 +761,11 @@ async function setupCodeQLBundleWithCompressionMethod(
const compressionMethod = tar.inferCompressionMethod( const compressionMethod = tar.inferCompressionMethod(
source.codeqlTarPath, source.codeqlTarPath,
); );
codeqlFolder = await tar.extract(source.codeqlTarPath, compressionMethod); codeqlFolder = await tar.extract(
source.codeqlTarPath,
compressionMethod,
zstdAvailability.version,
);
toolsSource = ToolsSource.Local; toolsSource = ToolsSource.Local;
break; break;
} }
@ -770,6 +780,7 @@ async function setupCodeQLBundleWithCompressionMethod(
source.bundleVersion, source.bundleVersion,
source.cliVersion, source.cliVersion,
apiDetails, apiDetails,
zstdAvailability.version,
tempDir, tempDir,
logger, logger,
); );

View file

@ -1,7 +1,12 @@
import * as fs from "fs";
import path from "path";
import { ToolRunner } from "@actions/exec/lib/toolrunner"; import { ToolRunner } from "@actions/exec/lib/toolrunner";
import * as toolcache from "@actions/tool-cache"; import * as toolcache from "@actions/tool-cache";
import { safeWhich } from "@chrisgavin/safe-which"; import { safeWhich } from "@chrisgavin/safe-which";
import { v4 as uuidV4 } from "uuid";
import { getTemporaryDirectory, runTool } from "./actions-util";
import { Logger } from "./logging"; import { Logger } from "./logging";
import { assertNever } from "./util"; import { assertNever } from "./util";
@ -84,24 +89,77 @@ export async function isZstdAvailable(
export type CompressionMethod = "gzip" | "zstd"; export type CompressionMethod = "gzip" | "zstd";
export async function extract( export async function extract(
path: string, tarPath: string,
compressionMethod: CompressionMethod, compressionMethod: CompressionMethod,
tarVersion: TarVersion | undefined,
): Promise<string> { ): Promise<string> {
switch (compressionMethod) { switch (compressionMethod) {
case "gzip": case "gzip":
// While we could also ask tar to autodetect the compression method, // Defensively continue to call the toolcache API as requesting a gzipped
// we defensively keep the gzip call identical as requesting a gzipped // bundle may be a fallback option.
// bundle will soon be a fallback option. return await toolcache.extractTar(tarPath);
return await toolcache.extractTar(path);
case "zstd": case "zstd":
// By specifying only the "x" flag, we ask tar to autodetect the if (!tarVersion) {
// compression method. throw new Error(
return await toolcache.extractTar(path, undefined, "x"); "Could not determine tar version, which is required to extract a Zstandard archive.",
);
}
return await extractTarZst(tarPath, tarVersion);
} }
} }
export function inferCompressionMethod(path: string): CompressionMethod { /**
if (path.endsWith(".tar.gz")) { * Extract a compressed tar archive
*
* @param file path to the tar
* @param dest destination directory. Optional.
* @returns path to the destination directory
*/
export async function extractTarZst(
file: string,
tarVersion: TarVersion,
): Promise<string> {
if (!file) {
throw new Error("parameter 'file' is required");
}
// Create dest
const dest = await createExtractFolder();
// Initialize args
const args = ["-x", "-v"];
let destArg = dest;
let fileArg = file;
if (process.platform === "win32" && tarVersion.type === "gnu") {
args.push("--force-local");
destArg = dest.replace(/\\/g, "/");
// Technically only the dest needs to have `/` but for aesthetic consistency
// convert slashes in the file arg too.
fileArg = file.replace(/\\/g, "/");
}
if (tarVersion.type === "gnu") {
// Suppress warnings when using GNU tar to extract archives created by BSD tar
args.push("--warning=no-unknown-keyword");
args.push("--overwrite");
}
args.push("-C", destArg, "-f", fileArg);
await runTool(`tar`, args);
return dest;
}
async function createExtractFolder(): Promise<string> {
const dest = path.join(getTemporaryDirectory(), uuidV4());
fs.mkdirSync(dest, { recursive: true });
return dest;
}
export function inferCompressionMethod(tarPath: string): CompressionMethod {
if (tarPath.endsWith(".tar.gz")) {
return "gzip"; return "gzip";
} }
return "zstd"; return "zstd";