set Zig cache directories to the workspace-local .zig-cache
This accomplishes two things: * It prevents unbounded growth in disk space usage on self-hosted runners. * It prevents races with other jobs when clearing the cache.
This commit is contained in:
@@ -55,7 +55,7 @@ be https://ziglang.org/download to avoid the official website being hit with lar
|
||||
If you've experienced issues with a default mirror, please [open an issue][report-bad-mirror] on the Zig
|
||||
website repository, which is where the list of mirrors is maintained.
|
||||
|
||||
If necessary, the caching of the global Zig cache directory can be disabled by setting the option
|
||||
If necessary, the caching of the global and local Zig cache directories can be disabled by setting the option
|
||||
`use-cache: false`. Don't do this without reason: preserving the Zig cache will typically speed things up
|
||||
and decrease the load on your Actions runners.
|
||||
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ inputs:
|
||||
required: false
|
||||
default: ''
|
||||
use-cache:
|
||||
description: 'Whether to cache the global Zig cache directory.'
|
||||
description: 'Whether to cache the global and local Zig cache directories.'
|
||||
required: true
|
||||
default: true
|
||||
cache-key:
|
||||
|
||||
@@ -16,9 +16,6 @@ const CACHE_PREFIX = "setup-zig-global-cache-";
|
||||
// See: https://github.com/marler8997/anyzig?tab=readme-ov-file#mach-versions-and-download-mirror
|
||||
const MACH_ZIG_VERSION_REGEX = /\.\s*mach_zig_version\s*=\s*"(.*?)"/;
|
||||
const MINIMUM_ZIG_VERSION_REGEX = /\.\s*minimum_zig_version\s*=\s*"(.*?)"/;
|
||||
// This is tied quite precisely to the output of `zig env`. It's just a temporary workaround until
|
||||
// I get around to implementing a ZON parser here.
|
||||
const ZIG_ENV_CACHE_DIR_REGEX = /^\s*\.global_cache_dir = "(.*)",$/m;
|
||||
|
||||
let _cached_version = null;
|
||||
async function getVersion() {
|
||||
@@ -203,50 +200,11 @@ async function getCachePrefix() {
|
||||
const job_name = github.context.job.replaceAll(/[^\w]/g, "_");
|
||||
const user_key = core.getInput('cache-key');
|
||||
|
||||
return `setup-zig-cache-${job_name}-${tarball_name}-${user_key}-`;
|
||||
return `setup-zig-cache-v2-${job_name}-${tarball_name}-${user_key}-`;
|
||||
}
|
||||
|
||||
async function getZigCachePath() {
|
||||
const env_zon = (await exec.getExecOutput('zig', ['env'])).stdout;
|
||||
if (env_zon[0] !== '.') {
|
||||
// JSON (legacy)
|
||||
return JSON.parse(env_zon)['global_cache_dir'];
|
||||
}
|
||||
const match = ZIG_ENV_CACHE_DIR_REGEX.exec(env_zon);
|
||||
if (!match) throw new Error("Failed to parse cache directory from 'zig env' output");
|
||||
return parseZigString(match[1]);
|
||||
}
|
||||
function parseZigString(raw) {
|
||||
// This function is neither complete (Unicode codepoint literals), nor correct (byte-escapes
|
||||
// aren't really compatible with JS "strings"). It's just a temporary best-effort implementation
|
||||
// which can hopefully handle any real-world directory path we encounter.
|
||||
let result = "";
|
||||
let i = 0;
|
||||
while (i < raw.length) {
|
||||
if (raw[i] != '\\') {
|
||||
result += raw[i];
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
i += 2;
|
||||
switch (raw[i - 1]) {
|
||||
case 'n': result += '\n'; break;
|
||||
case 'r': result += '\r'; break;
|
||||
case '\\': result += '\\'; break;
|
||||
case 't': result += '\t'; break;
|
||||
case '\'': result += '\''; break;
|
||||
case '"': result += '"'; break;
|
||||
case 'x': {
|
||||
const byte_val = parseInt(raw.slice(i, i + 2), 16);
|
||||
result += String.fromCharCode(byte_val);
|
||||
i += 2;
|
||||
break;
|
||||
}
|
||||
case 'u': throw new Error("unsupported Unicode codepoint literal in string");
|
||||
default: throw new Error("invalid escape code in string");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
function getZigCachePath() {
|
||||
return path.join(process.env['GITHUB_WORKSPACE'] ?? process.cwd(), '.zig-cache');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -162,12 +162,13 @@ async function main() {
|
||||
|
||||
core.addPath(zig_dir);
|
||||
|
||||
// Direct Zig to use the global cache as every local cache, so that we get maximum benefit from the caching below.
|
||||
core.exportVariable('ZIG_LOCAL_CACHE_DIR', await common.getZigCachePath());
|
||||
const cache_path = common.getZigCachePath();
|
||||
core.exportVariable('ZIG_GLOBAL_CACHE_DIR', cache_path);
|
||||
core.exportVariable('ZIG_LOCAL_CACHE_DIR', cache_path);
|
||||
|
||||
if (core.getBooleanInput('use-cache')) {
|
||||
core.info('Attempting restore of Zig cache');
|
||||
await cache.restoreCache([await common.getZigCachePath()], await common.getCachePrefix());
|
||||
await cache.restoreCache([cache_path], await common.getCachePrefix());
|
||||
}
|
||||
} catch (err) {
|
||||
core.setFailed(err.message);
|
||||
|
||||
Reference in New Issue
Block a user