Skip to content

Commit 1331773

Browse files
henrymercerCopilot
andcommitted
Address Copilot review feedback
Run the cleanup even when the download will not be cached in the toolcache, since the toolcache shares a filesystem with the directory we extract to, so freeing it helps either way, and report an error other than the toolcache being absent as a failure rather than as an empty toolcache. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 2681b03 commit 1331773

4 files changed

Lines changed: 89 additions & 21 deletions

File tree

‎lib/entry-points.js‎

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

‎src/setup-codeql.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ function createToolcacheEntry(
974974
async function runDownloadCodeQL(
975975
toolcacheRoot: string,
976976
features: Feature[],
977+
bundleVersion: string | undefined = CLEANUP_BUNDLE_VERSION,
977978
): Promise<toolsDownload.ToolcacheCleanupResult | undefined> {
978979
sinon
979980
.stub(toolsDownload, "downloadAndExtract")
@@ -988,7 +989,7 @@ async function runDownloadCodeQL(
988989
await setupCodeql.downloadCodeQL(
989990
"https://example.com/codeql-bundle.tar.gz",
990991
"gzip",
991-
CLEANUP_BUNDLE_VERSION,
992+
bundleVersion,
992993
CLEANUP_CLI_VERSION,
993994
SAMPLE_DOTCOM_API_DETAILS,
994995
undefined, // tarVersion
@@ -1309,3 +1310,64 @@ test.serial(
13091310
});
13101311
},
13111312
);
1313+
1314+
test.serial(
1315+
"downloadCodeQL cleans up the toolcache even when the download will not be cached",
1316+
async (t) => {
1317+
// A `tools` URL we can't derive a bundle version from is extracted to a temporary directory
1318+
// rather than the toolcache, but the toolcache is on the same filesystem, so emptying it still
1319+
// frees up space for the analysis.
1320+
await withTmpDir(async (tmpDir) => {
1321+
setupActionsVars(tmpDir, tmpDir);
1322+
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted";
1323+
1324+
const staleDirectory = createToolcacheEntry(
1325+
tmpDir,
1326+
"CodeQL",
1327+
CLEANUP_STALE_VERSION,
1328+
);
1329+
1330+
const cleanupDiagnostic = await runDownloadCodeQL(
1331+
tmpDir,
1332+
[Feature.CleanupToolcacheBundles],
1333+
undefined, // bundleVersion
1334+
);
1335+
1336+
t.false(fs.existsSync(staleDirectory));
1337+
t.deepEqual(cleanupDiagnostic, {
1338+
deletedVersions: [CLEANUP_STALE_VERSION],
1339+
failed: false,
1340+
});
1341+
});
1342+
},
1343+
);
1344+
1345+
test.serial(
1346+
"downloadCodeQL reports a failure when the toolcache cannot be inspected",
1347+
async (t) => {
1348+
await withTmpDir(async (tmpDir) => {
1349+
setupActionsVars(tmpDir, tmpDir);
1350+
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted";
1351+
1352+
createToolcacheEntry(tmpDir, "CodeQL", CLEANUP_STALE_VERSION);
1353+
1354+
const lstatStub = sinon.stub(fs.promises, "lstat").rejects(
1355+
Object.assign(new Error("permission denied"), {
1356+
code: "EACCES",
1357+
}),
1358+
);
1359+
1360+
const cleanupDiagnostic = await runDownloadCodeQL(tmpDir, [
1361+
Feature.CleanupToolcacheBundles,
1362+
]);
1363+
1364+
lstatStub.restore();
1365+
1366+
t.deepEqual(
1367+
cleanupDiagnostic,
1368+
{ deletedVersions: [], failed: true },
1369+
"An error other than the toolcache being absent must not be reported as success.",
1370+
);
1371+
});
1372+
},
1373+
);

‎src/setup-codeql.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ export const downloadCodeQL = async function (
823823
const extractedBundlePath =
824824
toolcacheInfo?.path ?? getTempExtractionDir(tempDir);
825825

826-
await tryDeleteToolcacheBundles(toolcacheInfo?.version, features, logger);
826+
await tryDeleteToolcacheBundles(features, logger);
827827

828828
const statusReport = await downloadAndExtract(
829829
codeqlURL,
@@ -881,13 +881,10 @@ function getToolcacheDestinationInfo(
881881
* Reclaims disk space by deleting the CodeQL tools from the toolcache, if enabled.
882882
*
883883
* On GitHub-hosted runners the toolcache shares a filesystem with the workspace, so tools left in
884-
* the toolcache take up space that the analysis could use instead.
885-
*
886-
* @param destinationVersion The toolcache version number that the tools will be stored under, or
887-
* `undefined` if they will not be stored in the toolcache.
884+
* the toolcache take up space that the analysis could use instead. This holds wherever we extract
885+
* the tools we are obtaining, since the toolcache is on that filesystem either way.
888886
*/
889887
async function tryDeleteToolcacheBundles(
890-
destinationVersion: string | undefined,
891888
features: FeatureEnablement,
892889
logger: Logger,
893890
): Promise<void> {
@@ -901,10 +898,7 @@ async function tryDeleteToolcacheBundles(
901898
return;
902899
}
903900

904-
// If we are not going to add the tools to the toolcache, we are extracting them somewhere else
905-
// and emptying the toolcache would not buy us the space we need.
906901
if (
907-
destinationVersion === undefined ||
908902
!isGitHubHostedRunner() ||
909903
!(await features.getValue(Feature.CleanupToolcacheBundles))
910904
) {

‎src/tools-download.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,17 @@ export async function deleteToolcacheBundles(
255255
);
256256
return { deletedVersions: [], failed: true };
257257
}
258-
} catch {
259-
logger.debug(
260-
`There are no CodeQL tools at ${toolDirectory} to delete from the toolcache.`,
258+
} catch (e: any) {
259+
if (e?.code === "ENOENT") {
260+
logger.debug(
261+
`There are no CodeQL tools at ${toolDirectory} to delete from the toolcache.`,
262+
);
263+
return { deletedVersions: [], failed: false };
264+
}
265+
logger.info(
266+
`Failed to inspect the CodeQL tools at ${toolDirectory}: ${getErrorMessage(e)}`,
261267
);
262-
return { deletedVersions: [], failed: false };
268+
return { deletedVersions: [], failed: true };
263269
}
264270

265271
try {

0 commit comments

Comments
 (0)