Skip to content

Commit e13c3dc

Browse files
henrymercerCopilot
andcommitted
Only clean up a toolcache on the workspace filesystem
Some runner images keep the toolcache on a different volume to the workspace, so deleting the tools there frees up disk space that the analysis cannot use, and costs a later step that wanted them in the toolcache a download. Windows runners are laid out this way, with the toolcache on `C:` and the workspace on `D:`. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 1331773 commit e13c3dc

4 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎lib/entry-points.js‎

Lines changed: 11 additions & 1 deletion
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,51 @@ test.serial(
13711371
});
13721372
},
13731373
);
1374+
1375+
test.serial(
1376+
"downloadCodeQL does not clean up a toolcache on a different filesystem to the workspace",
1377+
async (t) => {
1378+
// Some runner images keep the toolcache on a different volume to the workspace, in which case
1379+
// deleting the tools frees up disk space that the analysis cannot use.
1380+
await withTmpDir(async (tmpDir) => {
1381+
setupActionsVars(tmpDir, tmpDir);
1382+
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted";
1383+
1384+
const staleDirectory = createToolcacheEntry(
1385+
tmpDir,
1386+
"CodeQL",
1387+
CLEANUP_STALE_VERSION,
1388+
);
1389+
1390+
sinon
1391+
.stub(toolsDownload, "isToolcacheOnWorkspaceFilesystem")
1392+
.returns(false);
1393+
1394+
const cleanupDiagnostic = await runDownloadCodeQL(tmpDir, [
1395+
Feature.CleanupToolcacheBundles,
1396+
]);
1397+
1398+
t.true(fs.existsSync(staleDirectory));
1399+
t.is(cleanupDiagnostic, undefined);
1400+
});
1401+
},
1402+
);
1403+
1404+
test.serial(
1405+
"isToolcacheOnWorkspaceFilesystem compares the toolcache against the workspace",
1406+
async (t) => {
1407+
await withTmpDir(async (tmpDir) => {
1408+
const logger = getRunnerLogger(true);
1409+
1410+
setupActionsVars(tmpDir, tmpDir);
1411+
t.true(toolsDownload.isToolcacheOnWorkspaceFilesystem(logger));
1412+
1413+
// If we can't tell, we assume the toolcache is not somewhere we can reclaim space from.
1414+
process.env[ActionsEnvVars.RUNNER_TOOL_CACHE] = path.join(
1415+
tmpDir,
1416+
"does-not-exist",
1417+
);
1418+
t.false(toolsDownload.isToolcacheOnWorkspaceFilesystem(logger));
1419+
});
1420+
},
1421+
);

‎src/setup-codeql.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
deleteToolcacheBundles,
3737
downloadAndExtract,
3838
getToolcacheDirectory,
39+
isToolcacheOnWorkspaceFilesystem,
3940
ToolcacheCleanupResult,
4041
ToolsDownloadStatusReport,
4142
writeToolcacheMarkerFile,
@@ -900,6 +901,7 @@ async function tryDeleteToolcacheBundles(
900901

901902
if (
902903
!isGitHubHostedRunner() ||
904+
!isToolcacheOnWorkspaceFilesystem(logger) ||
903905
!(await features.getValue(Feature.CleanupToolcacheBundles))
904906
) {
905907
return;

‎src/tools-download.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,27 @@ export function getToolcacheDirectory(version: string): string {
219219
);
220220
}
221221

222+
/**
223+
* Whether the toolcache is on the same filesystem as the workspace, and so whether deleting the
224+
* tools frees up disk space that the analysis can use.
225+
*
226+
* These are separate volumes on some runner images. Windows runners, for example, keep the
227+
* toolcache on `C:` while the workspace is on `D:`.
228+
*/
229+
export function isToolcacheOnWorkspaceFilesystem(logger: Logger): boolean {
230+
try {
231+
return (
232+
fs.statSync(getRequiredEnvParam("RUNNER_TOOL_CACHE")).dev ===
233+
fs.statSync(getRequiredEnvParam("GITHUB_WORKSPACE")).dev
234+
);
235+
} catch (e) {
236+
logger.debug(
237+
`Could not determine whether the toolcache is on the same filesystem as the workspace: ${getErrorMessage(e)}`,
238+
);
239+
return false;
240+
}
241+
}
242+
222243
/** The outcome of trying to reclaim disk space by deleting the CodeQL tools from the toolcache. */
223244
export interface ToolcacheCleanupResult {
224245
/** The versions of the CodeQL tools that were deleted. */

0 commit comments

Comments
 (0)