From 274ad334e3acfaf9c831403184a723f416c218f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 27 Nov 2025 20:11:57 +0100 Subject: [PATCH] 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. --- README.md | 2 +- action.yml | 2 +- common.js | 48 +++--------------------------------------------- main.js | 7 ++++--- 4 files changed, 9 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index da91541..23e5daf 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/action.yml b/action.yml index c594df5..640d5bb 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/common.js b/common.js index bb1c4f1..2ff5ddc 100644 --- a/common.js +++ b/common.js @@ -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 = { diff --git a/main.js b/main.js index 6d9e6e1..837647d 100644 --- a/main.js +++ b/main.js @@ -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);