Skip to content

Commit 274ad33

Browse files
alexrpmlugg
authored andcommitted
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.
1 parent 9ffc387 commit 274ad33

File tree

4 files changed

+9
-50
lines changed

4 files changed

+9
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ be https://ziglang.org/download to avoid the official website being hit with lar
5555
If you've experienced issues with a default mirror, please [open an issue][report-bad-mirror] on the Zig
5656
website repository, which is where the list of mirrors is maintained.
5757

58-
If necessary, the caching of the global Zig cache directory can be disabled by setting the option
58+
If necessary, the caching of the global and local Zig cache directories can be disabled by setting the option
5959
`use-cache: false`. Don't do this without reason: preserving the Zig cache will typically speed things up
6060
and decrease the load on your Actions runners.
6161

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ inputs:
99
required: false
1010
default: ''
1111
use-cache:
12-
description: 'Whether to cache the global Zig cache directory.'
12+
description: 'Whether to cache the global and local Zig cache directories.'
1313
required: true
1414
default: true
1515
cache-key:

common.js

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ const CACHE_PREFIX = "setup-zig-global-cache-";
1616
// See: https://github.com/marler8997/anyzig?tab=readme-ov-file#mach-versions-and-download-mirror
1717
const MACH_ZIG_VERSION_REGEX = /\.\s*mach_zig_version\s*=\s*"(.*?)"/;
1818
const MINIMUM_ZIG_VERSION_REGEX = /\.\s*minimum_zig_version\s*=\s*"(.*?)"/;
19-
// This is tied quite precisely to the output of `zig env`. It's just a temporary workaround until
20-
// I get around to implementing a ZON parser here.
21-
const ZIG_ENV_CACHE_DIR_REGEX = /^\s*\.global_cache_dir = "(.*)",$/m;
2219

2320
let _cached_version = null;
2421
async function getVersion() {
@@ -203,50 +200,11 @@ async function getCachePrefix() {
203200
const job_name = github.context.job.replaceAll(/[^\w]/g, "_");
204201
const user_key = core.getInput('cache-key');
205202

206-
return `setup-zig-cache-${job_name}-${tarball_name}-${user_key}-`;
203+
return `setup-zig-cache-v2-${job_name}-${tarball_name}-${user_key}-`;
207204
}
208205

209-
async function getZigCachePath() {
210-
const env_zon = (await exec.getExecOutput('zig', ['env'])).stdout;
211-
if (env_zon[0] !== '.') {
212-
// JSON (legacy)
213-
return JSON.parse(env_zon)['global_cache_dir'];
214-
}
215-
const match = ZIG_ENV_CACHE_DIR_REGEX.exec(env_zon);
216-
if (!match) throw new Error("Failed to parse cache directory from 'zig env' output");
217-
return parseZigString(match[1]);
218-
}
219-
function parseZigString(raw) {
220-
// This function is neither complete (Unicode codepoint literals), nor correct (byte-escapes
221-
// aren't really compatible with JS "strings"). It's just a temporary best-effort implementation
222-
// which can hopefully handle any real-world directory path we encounter.
223-
let result = "";
224-
let i = 0;
225-
while (i < raw.length) {
226-
if (raw[i] != '\\') {
227-
result += raw[i];
228-
i += 1;
229-
continue;
230-
}
231-
i += 2;
232-
switch (raw[i - 1]) {
233-
case 'n': result += '\n'; break;
234-
case 'r': result += '\r'; break;
235-
case '\\': result += '\\'; break;
236-
case 't': result += '\t'; break;
237-
case '\'': result += '\''; break;
238-
case '"': result += '"'; break;
239-
case 'x': {
240-
const byte_val = parseInt(raw.slice(i, i + 2), 16);
241-
result += String.fromCharCode(byte_val);
242-
i += 2;
243-
break;
244-
}
245-
case 'u': throw new Error("unsupported Unicode codepoint literal in string");
246-
default: throw new Error("invalid escape code in string");
247-
}
248-
}
249-
return result;
206+
function getZigCachePath() {
207+
return path.join(process.env['GITHUB_WORKSPACE'] ?? process.cwd(), '.zig-cache');
250208
}
251209

252210
module.exports = {

main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ async function main() {
162162

163163
core.addPath(zig_dir);
164164

165-
// Direct Zig to use the global cache as every local cache, so that we get maximum benefit from the caching below.
166-
core.exportVariable('ZIG_LOCAL_CACHE_DIR', await common.getZigCachePath());
165+
const cache_path = common.getZigCachePath();
166+
core.exportVariable('ZIG_GLOBAL_CACHE_DIR', cache_path);
167+
core.exportVariable('ZIG_LOCAL_CACHE_DIR', cache_path);
167168

168169
if (core.getBooleanInput('use-cache')) {
169170
core.info('Attempting restore of Zig cache');
170-
await cache.restoreCache([await common.getZigCachePath()], await common.getCachePrefix());
171+
await cache.restoreCache([cache_path], await common.getCachePrefix());
171172
}
172173
} catch (err) {
173174
core.setFailed(err.message);

0 commit comments

Comments
 (0)