Hi y'all. Having a thorny issue with remote cachin...
# general
m
Hi y'all. Having a thorny issue with remote caching. Hoping someone might have some insight here as I don't like the workaround I landed on but don't see a very good solution. And apologies, first post so please let me know if I should post this elsewhere) Remote caching + CodeArtifact: rotating token in
[python-repos].indexes
busts cache keys for Docker PEX builds
🧵
We're setting up Depot Cache (REAPI) for remote caching in CI. We've gotten lint/check/test cache keys stable by removing rotating credentials from
[subprocess-environment].env_vars
. However, we're stuck on
pants package
(PEX builds inside
docker_environment
). Setup: Pants 2.31.0 • CodeArtifact as our PyPI index, configured via
[python-repos].indexes
with token interpolation:
indexes = ["<https://aws>:%(env.CODEARTIFACT_AUTH_TOKEN)<mailto:s@our-domain.d.codeartifact.us-east-1.amazonaws.com|s@our-domain.d.codeartifact.us-east-1.amazonaws.com>/pypi/repo/simple/"]
Depot Cache (
<grpcs://cache.depot.dev>
) as REAPI remote cache • PEX binaries built in a
docker_environment
(for cross-platform Linux builds) The problem: The CodeArtifact token rotates every run (STS-based, max 12h lifetime). Since the resolved index URL (with embedded token) becomes part of PEX resolver process args, every token rotation changes the cache key for PEX resolution processes. This cascades -- different PEX output means different Docker build inputs, so
docker build
processes also miss cache. What we tried:
.netrc
approach (token outside Pants fingerprint): Works for local processes (lint/check/test) but not inside
docker_environment
-- the host
.netrc
file isn't accessible in Docker. • Writing
.netrc
into the Docker named-cache volume: The
run
goal doesn't execute in Docker environments ("The
run
goal only runs in the local environment"), so we can't trigger volume creation before needing it. •
docker_environment
mounts
field: PR #20322 was closed without merging. •
subprocess_environment_env_vars
override on `docker_environment`: Can pass env vars but can't get the
.netrc
file into the container. Current workaround: We're considering a scheduled workflow that refreshes the CodeArtifact token every 10 hours and stores it as a stable GitHub secret, so all CI runs within that window share the same token. This works but means ~2 cache busts per day. Question: Is there a better way to authenticate to a private PyPI index from inside a
docker_environment
without the credentials ending up in process cache keys? Are there plans to revisit the
mounts
feature (PR #20322) or support credential helpers / keyring for
docker_environment
processes?
h
Totally fine place to post this
Hmm, i don't think there is a solution today. We could hack something up specifically for keyring in docker, but it sounds like the robust solution would be allowing listing env vars that are available to the process but are not part of its cache key? This could potentially have other uses
🙏 1
Just checking that this would address the issue for you?
m
Yes. I believe it would solve the problem completely. If Pants had "unhashed env vars" (available to the process but excluded from the cache key fingerprint), we could pass
CODEARTIFACT_AUTH_TOKEN
to Docker PEX processes without it busting cache keys. No need for the stable token workaround, no scheduled refresh workflow, no
.netrc
tricks. That's essentially what the linked PR proposed ("reified environment variables") -- env vars whose actual values are applied after cache lookup, not before.
s
allowing listing env vars that are available to the process but are not part of its cache key? This could potentially have other uses
This would be nice to have! Another use case from my job: We have some integration tests run via Pants that need access to GCP. In CI, we use google-github-actions/gcp-auth which stores the credentials (and sets
GOOGLE_APPLICATION_CREDENTIALS
) at a unique path for each run (maybe timestamp based?). This kept busting our cache. It was easy enough to work around (just move the file to a constant path like
/tmp/google_creds.json
& overwrite the env var). But it would be nice to have a built-in option to ignore certain env vars for cache lookups
💯 1
m
so this ended up being an absolute nightmare to find. But ultimately it was this:
Copy code
- name: Install OpenTofu
        uses: opentofu/setup-opentofu@fc711fa910b93cba0f3fbecaafc9f42fd0c411cb
        with:
          tofu_version: 1.10.8
Root cause found:
setup-opentofu
GitHub Action modifies PATH before Pants runs We isolated the issue through binary search of our CI workflow steps. The
opentofu/setup-opentofu
action was the sole culprit. The mechanism:
setup-opentofu
calls
addPath(pathToCLI)
which prepends the OpenTofu binary directory to
PATH
via
GITHUB_PATH
. This modified
PATH
is inherited by pantsd when it starts, and Pants includes
PATH
in process cache key computation. Since the path includes runner-specific or temp-specific components, it differs between CI runs, busting every test cache key. The fix: Move
setup-opentofu
to after all Pants goals in the workflow. OpenTofu is only needed for infrastructure plan/apply steps which run after lint/check/test/package. Why lint/check/package weren't affected: We believe those goals have fewer processes whose cache keys include
PATH
, or their process construction resolves
PATH
differently. This could use further investigation
agree with Darren that it would be really nice to just ignore certain env vars for cache
h
Oh wow, that's a pain. But that sounds like a separate issue from the
CODEARTIFACT_AUTH_TOKEN
issue you mentioned above?
So that one is still pertinent?
m
Ended up just working around the
CODEARTIFACT_AUTH_TOKEN
it's certainly a bit hacky but basically just mint the token for 12h so it only busts the cache on expiry. Definitely far from a perfect solution. Here's the code snippet if it's helpful for anyone.
didn't try something like this suggestion Didn't try something like this workaround from @some-insurance-58590 yet
It was easy enough to work around (just move the file to a constant path like
/tmp/google_creds.json
& overwrite the env var). But it would be nice to have a built-in option to ignore certain env vars for cache lookups
but yeah, i think the feature request would still be helpful
h
Just to clarify, when you say "Root cause found" above, that seems like an unrelated thing (with the same underlying cache misses as the shared phenomena)?
m
Yeah. Sorry. I might have published in the wrong thread so: 1. The first cache miss issue was
CODE_ARTIFACT_TOKEN
changed every build and busted the cache. This one we 🩹 with just only refreshing the token every ~11-12 hours. 2. The second issue was specifically the cache being busted on
pants test
specifically in CI as a result of the open_tofu binary being installed on a different
tmp
path each time. I still don't understand why this affected the test cache only or why it affected it at all 🤔
so root proximate cause found 🙃
h
Got it, thanks for clarifying.