feat: split Go module and build caches

Reuse downloaded modules across Go versions, runner architectures, and
runner images on the same OS instead of duplicating them in each build
cache archive.

Restore and save each entry independently, running operations in parallel
when safe. Serialize restores for overlapping or aliased paths, preserve
the original paths for the post step, and report cache-hit only when both
entries match their primary keys.

Update the documented manual restore keys and generated action bundles.
This commit is contained in:
qmuntal
2026-10-05 15:05:38 +02:00
parent 90ad2b35f6
commit ad9941188f
13 changed files with 1035 additions and 335 deletions
+88 -12
View File
@@ -1,11 +1,24 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import fs from 'fs';
import path from 'path';
import {
supportedPackageManagers,
PackageManagerInfo
PackageManagerInfo,
CacheKind
} from './package-managers.js';
export interface CacheDirectory {
kind: CacheKind;
path: string;
}
export interface CacheEntry extends CacheDirectory {
primaryKey: string;
matchedKey?: string;
}
export const getCommandOutput = async (toolCommand: string) => {
let {stdout, stderr, exitCode} = await exec.getExecOutput(
toolCommand,
@@ -36,24 +49,24 @@ export const getPackageManagerInfo = async (packageManager: string) => {
export const getCacheDirectoryPath = async (
packageManagerInfo: PackageManagerInfo
) => {
): Promise<CacheDirectory[]> => {
const pathOutputs = await Promise.allSettled(
packageManagerInfo.cacheFolderCommandList.map(async command =>
getCommandOutput(command)
)
packageManagerInfo.cacheFolderCommandList.map(async ({kind, command}) => ({
kind,
path: await getCommandOutput(command)
}))
);
const results = pathOutputs.map(item => {
const cachePaths: CacheDirectory[] = [];
for (const item of pathOutputs) {
if (item.status === 'fulfilled') {
return item.value;
if (item.value.path) {
cachePaths.push(item.value);
}
} else {
core.info(`[warning]getting cache directory path failed: ${item.reason}`);
}
return '';
});
const cachePaths = results.filter(item => item);
}
if (!cachePaths.length) {
throw new Error(`Could not get cache folder paths.`);
@@ -62,6 +75,69 @@ export const getCacheDirectoryPath = async (
return cachePaths;
};
export function canRestoreCachesInParallel(
directories: CacheDirectory[]
): boolean {
try {
const paths = directories.map(directory => {
const resolvedPath = resolveCachePath(directory.path);
// Conservatively handle case-insensitive Windows/macOS filesystems.
return process.platform === 'darwin' || process.platform === 'win32'
? resolvedPath.toLowerCase()
: resolvedPath;
});
// A cache archive can replace symlinks traversed by the other restore.
// Canonical destinations alone do not capture that extraction footprint.
if (
paths.some((resolvedPath, index) => {
const absolutePath = path.resolve(directories[index].path);
const normalizedPath =
process.platform === 'darwin' || process.platform === 'win32'
? absolutePath.toLowerCase()
: absolutePath;
return resolvedPath !== normalizedPath;
})
) {
return false;
}
return paths.every((parent, index) =>
paths.every((child, otherIndex) => {
if (index === otherIndex) {
return true;
}
const relative = path.relative(parent, child);
return (
relative === '..' ||
relative.startsWith(`..${path.sep}`) ||
path.isAbsolute(relative)
);
})
);
} catch {
// If the paths cannot be resolved safely, avoid concurrent extraction.
return false;
}
}
function resolveCachePath(directory: string): string {
const absolutePath = path.resolve(directory);
try {
fs.lstatSync(absolutePath);
} catch (error) {
const parent = path.dirname(absolutePath);
if (
(error as NodeJS.ErrnoException).code !== 'ENOENT' ||
parent === absolutePath
) {
throw error;
}
// Cold cache directories may not exist yet. Resolve their existing
// ancestors so symlink aliases still participate in the overlap check.
return path.join(resolveCachePath(parent), path.basename(absolutePath));
}
return fs.realpathSync(absolutePath);
}
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'