Skip to content

Append username to the default tmp directory#187

Open
laurentlb wants to merge 1 commit into
uber:mainfrom
laurentlb:main
Open

Append username to the default tmp directory#187
laurentlb wants to merge 1 commit into
uber:mainfrom
laurentlb:main

Conversation

@laurentlb

Copy link
Copy Markdown

When two people build code on the same machine, there can be a collision. The second person may not be able to write to zig-cache if it was created by someone else. When it happens, the error message is quite confusing (see _compile_failed variable).

With this commit, each user should get a different directory by default. It's still possible to change the default using HERMETIC_CC_TOOLCHAIN_CACHE_PREFIX.

When two people build code on the same machine, there can be a collision. The second person may not be able to write to zig-cache if it was created by someone else. When it happens, the error message is quite confusing (see `_compile_failed` variable).

With this commit, each user should get a different directory by default. It's still possible to change the default using HERMETIC_CC_TOOLCHAIN_CACHE_PREFIX.

Signed-off-by: Laurent Le Brun <laurentlb@gmail.com>
@CLAassistant

CLAassistant commented Jun 24, 2024

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@laurentlb

Copy link
Copy Markdown
Author

It looks like this will conflict with #183. We can close this PR if the other one gets merged.

@linzhp

linzhp commented Jul 7, 2024

Copy link
Copy Markdown
Contributor

yeah, and quoting a discussion with @sluongng:

The concern of setting UID or USER into the path is that it would be stamped into the wrapper binary as a constant.
And as it's being part of the toolchain, it will invalidate remote cache between different users.

@ctcjab

ctcjab commented May 22, 2026

Copy link
Copy Markdown
Contributor

I've submitted #254 which takes a similar approach (appending the username) but resolves it at runtime in zig-wrapper instead of at build time. This addresses the remote cache invalidation concern from #187 (comment) — the compiled binary is identical for all users since the username is read from the environment at execution time, not baked in as a constant.

linzhp pushed a commit that referenced this pull request Jun 23, 2026
## Summary

Fixes #83 — multiple users on the same machine collide on the shared zig
cache directory (`/tmp/zig-cache`), causing permission errors.

This PR appends the OS username to the zig cache directory **at
runtime** in `zig-wrapper`, so each user gets a separate cache directory
(e.g. `/tmp/zig-cache-alice`, `/tmp/zig-cache-bob`).

### Why runtime, not build time?

Previous approaches (#187) embedded the username into the compiled
`zig-wrapper` binary via template substitution. This means the binary
differs per user, which [invalidates remote build
cache](#187 (comment))
for every C++ action. By resolving the username at runtime instead, the
compiled binary is identical for all users and remote cache is
preserved.

### Changes

- **`toolchain/zig-wrapper.zig`**: Read `USER` (Unix) or `USERNAME`
(Windows) from the environment at runtime and append `-{username}` to
the compiled-in cache directory base. Falls back to the base directory
if the env var is unset (containers, headless environments).
- **`toolchain/defs.bzl`**: The repository rule's compile step (which
compiles zig-wrapper itself) also uses a per-user cache directory to
prevent collisions during toolchain setup. The template substitution
still uses the base prefix — the binary is not affected.
- **`.github/workflows/ci.yaml`**: Updated cache paths to include the
runner username (`runner` on Linux/macOS, `runneradmin` on Windows).
- **`README.md`**: Updated cache directory documentation to reflect
per-user behavior.

## Test plan

- [x] `zig test zig-wrapper.zig` — new test verifies cache dir includes
username suffix
- [x] `tools/bazel test //...` — all 7 tests pass (4 skipped for
non-matching platforms)
- [x] CI will validate cross-platform behavior

🤖 Generated with [Claude Code](https://claude.ai/code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants