mirror of
https://github.com/jdx/mise-action.git
synced 2026-05-14 22:00:34 +00:00
fix(cache): isolate cache keys per working_directory in monorepos (#360)
Problem
-------
mise-action hashes ALL mise config files in the repo to compute a single
default cache key. In a monorepo with multiple projects (e.g.,
apps/frontend, apps/backend), this causes cache pollution:
1. Job A runs for apps/frontend, installs only frontend tools
2. Cache is saved with a key based on ALL configs
3. Job B runs for apps/backend, gets cache HIT (same key)
4. Job B finds frontend tools but not backend tools
5. Job B has to install all tools because they are missing from cache
Additionally, any change to an unrelated project config would bust the
cache for all projects.
Solution
--------
When working_directory is set, compute the default cache key using only
the config files that affect that directory (detected via `mise config
ls --json`) instead of globbing all configs in the repo.
This required separating binary and tools caching:
- Binary cache: restored first so mise is available for `mise config ls`
- Tools cache: default key computed after mise is installed
Key Implementation Details
--------------------------
1. Cache separation:
- restoreMiseBinaryCache/saveMiseBinaryCache for the mise binary
- restoreToolsCache/saveToolsCache for the full mise directory
- Binary cache key: `{prefix}-binary-{platform}-{version}-{dirHash}`
- Tools default cache key: based on config file contents for
working_directory
2. Binary backup during tools cache restore: The tools cache includes
bin/, which could overwrite the binary that setupMise() just installed.
We use withBinaryBackup() to backup the binary before restoring the
tools cache and restore it afterward.
An alternative approach would be to only cache installs/ and shims/
instead of the full miseDir(), but that would change the caching
behavior for existing users. Using withBinaryBackup() retains the
original caching behavior while preventing the binary from being
overwritten.
3. Binary cache key includes mise_dir hash: Prevents cache collision
when users change mise_dir between runs. Without this, a cache hit could
restore the binary to the wrong location.
4. Explicit mise binary path: Uses full path to mise binary instead of
relying on PATH lookup, avoiding potential race conditions with
core.addPath().
5. Lock file handling:
- .toml files: look for corresponding .lock file
- .tool-versions: look for mise.lock in the same directory
6. Graceful degradation: If `mise config ls` fails when
working_directory is set, caching is disabled with a warning rather
than:
- Failing the action entirely, or
- Falling back to glob patterns (which would reintroduce the bug)
Backward Compatibility
----------------------
- working_directory not set: No change, uses existing glob of all
configs
- working_directory set: Default cache key based on `mise config ls`
output
Note on cache_key input: The `cache_key` input now only controls the
tools cache key. The binary cache key is always computed automatically
based on platform, version, and mise_dir. This is generally better since
the binary cache is version-stable and does not need custom key logic.
Test Coverage
-------------
Added test-monorepo-cache.yml with 8 test scenarios:
- install-backend/restore-frontend: Verify cache isolation
- install-frontend/unrelated-change-no-bust: Verify unrelated changes do
not bust cache
- parent-config-change: Verify parent config changes bust child cache
- lock-file-change: Verify lock file changes bust cache
- install-default-mise-dir/restore-custom-mise-dir: Verify mise_dir in
cache key
Final Note
----------
Currently, the default tool cache key includes the mise version. This
was in place prior, so it was left intact. With this change and the
splitting of the mise version cache from the tool cache, we could safely
remove the mise version from the tool cache key. Left this for a
subsequent change if desired.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Fixes cache key pollution in monorepos by scoping tool cache keys to
the `working_directory`'s config hierarchy and separating binary vs.
tools caching.
>
> - New cache flow: `restoreMiseBinaryCache/saveMiseBinaryCache` (key:
`{prefix}-binary-{platform}-{version}-{dirHash}`) runs before
installation; `restoreToolsCache/saveToolsCache` uses a default key
derived from `mise config ls --json` for the specified
`working_directory`
> - Uses explicit `mise` binary path and preserves it during tools cache
restore via `withBinaryBackup` to avoid overwrites
> - Default tools key still supports template inputs; includes lockfile
handling and guards to disable caching on failures
> - `mise_dir` hash included in binary cache key to prevent cross-dir
collisions
> - Adds `.github/workflows/test-monorepo-cache.yml` with scenarios
verifying monorepo cache isolation, unrelated-config no-bust,
parent-config change bust, lockfile change bust, identical-content
different-path isolation, and `mise_dir`-key differentiation
> - Updates compiled `dist/` artifacts; minor docs entry `AGENTS.md`
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
434d5feca5. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
This commit is contained in:
parent
23096c7b0b
commit
891faa7084
5 changed files with 691 additions and 81 deletions
297
src/index.ts
297
src/index.ts
|
|
@ -40,23 +40,38 @@ const MISE_CONFIG_FILE_PATTERNS = [
|
|||
|
||||
// Default cache key template
|
||||
const DEFAULT_CACHE_KEY_TEMPLATE =
|
||||
'{{cache_key_prefix}}-{{platform}}-{{file_hash}}{{#if version}}-{{version}}{{/if}}{{#if mise_env}}-{{mise_env}}{{/if}}{{#if install_args_hash}}-{{install_args_hash}}{{/if}}'
|
||||
'{{cache_key_prefix}}-{{platform}}-{{file_hash}}-{{dir_hash}}{{#if version}}-{{version}}{{/if}}{{#if mise_env}}-{{mise_env}}{{/if}}{{#if install_args_hash}}-{{install_args_hash}}{{/if}}'
|
||||
|
||||
interface CacheState {
|
||||
key: string
|
||||
hit: boolean
|
||||
}
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
await setToolVersions()
|
||||
await setMiseToml()
|
||||
|
||||
let cacheKey: string | undefined
|
||||
if (core.getBooleanInput('cache')) {
|
||||
cacheKey = await restoreMiseCache()
|
||||
} else {
|
||||
core.setOutput('cache-hit', false)
|
||||
const version = core.getInput('version')
|
||||
const cacheEnabled = core.getBooleanInput('cache')
|
||||
|
||||
// Restore binary cache, install mise, save binary cache if needed
|
||||
let binaryCache: CacheState = { key: '', hit: false }
|
||||
if (cacheEnabled) {
|
||||
binaryCache = await restoreMiseBinaryCache(version)
|
||||
}
|
||||
await setupMise(version, core.getBooleanInput('fetch_from_github'))
|
||||
if (cacheEnabled) {
|
||||
await saveMiseBinaryCache(binaryCache)
|
||||
}
|
||||
|
||||
const version = core.getInput('version')
|
||||
const fetchFromGitHub = core.getBooleanInput('fetch_from_github')
|
||||
await setupMise(version, fetchFromGitHub)
|
||||
// Restore tools cache (needs mise installed to compute key for working_directory)
|
||||
let toolsCache: CacheState = { key: '', hit: false }
|
||||
if (cacheEnabled) {
|
||||
toolsCache = await restoreToolsCache()
|
||||
}
|
||||
core.setOutput('cache-hit', binaryCache.hit && toolsCache.hit)
|
||||
|
||||
await setEnvVars()
|
||||
if (core.getBooleanInput('reshim')) {
|
||||
await miseReshim()
|
||||
|
|
@ -64,13 +79,12 @@ async function run(): Promise<void> {
|
|||
await testMise()
|
||||
if (core.getBooleanInput('install')) {
|
||||
await miseInstall()
|
||||
if (cacheKey && core.getBooleanInput('cache_save')) {
|
||||
await saveCache(cacheKey)
|
||||
if (cacheEnabled) {
|
||||
await saveToolsCache(toolsCache)
|
||||
}
|
||||
}
|
||||
await miseLs()
|
||||
const loadEnv = core.getBooleanInput('env')
|
||||
if (loadEnv) {
|
||||
if (core.getBooleanInput('env')) {
|
||||
await exportMiseEnv()
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
@ -194,27 +208,104 @@ async function setEnvVars(): Promise<void> {
|
|||
}
|
||||
}
|
||||
|
||||
async function restoreMiseCache(): Promise<string | undefined> {
|
||||
core.startGroup('Restoring mise cache')
|
||||
const cachePath = miseDir()
|
||||
async function restoreMiseBinaryCache(version: string): Promise<CacheState> {
|
||||
const binPath = path.join(miseDir(), 'bin')
|
||||
const platform = await getTarget()
|
||||
const resolvedVersion = version || 'latest'
|
||||
const cacheKeyPrefix = core.getInput('cache_key_prefix') || 'mise-v0'
|
||||
// Include hash of miseDir path to handle custom mise_dir configurations
|
||||
const dirHash = crypto
|
||||
.createHash('sha256')
|
||||
.update(miseDir())
|
||||
.digest('hex')
|
||||
.slice(0, 8)
|
||||
const key = `${cacheKeyPrefix}-binary-${platform}-${resolvedVersion}-${dirHash}`
|
||||
|
||||
// Use custom cache key if provided, otherwise use default template
|
||||
const cacheKeyTemplate =
|
||||
core.getInput('cache_key') || DEFAULT_CACHE_KEY_TEMPLATE
|
||||
const primaryKey = await processCacheKeyTemplate(cacheKeyTemplate)
|
||||
const cacheKey = await core.group('Restoring mise binary cache', async () => {
|
||||
const restored = await cache.restoreCache([binPath], key)
|
||||
if (restored) {
|
||||
core.info(`mise binary cache restored from key: ${restored}`)
|
||||
} else {
|
||||
core.info(`mise binary cache not found for ${key}`)
|
||||
}
|
||||
return restored
|
||||
})
|
||||
|
||||
core.saveState('PRIMARY_KEY', primaryKey)
|
||||
core.saveState('MISE_DIR', cachePath)
|
||||
return { key, hit: Boolean(cacheKey) }
|
||||
}
|
||||
|
||||
const cacheKey = await cache.restoreCache([cachePath], primaryKey)
|
||||
core.setOutput('cache-hit', Boolean(cacheKey))
|
||||
|
||||
if (!cacheKey) {
|
||||
core.info(`mise cache not found for ${primaryKey}`)
|
||||
return primaryKey
|
||||
async function saveMiseBinaryCache(state: CacheState): Promise<void> {
|
||||
if (!core.getBooleanInput('cache_save') || state.hit || !state.key) {
|
||||
return
|
||||
}
|
||||
|
||||
core.info(`mise cache restored from key: ${cacheKey}`)
|
||||
await core.group('Saving mise binary cache', async () => {
|
||||
const binPath = path.join(miseDir(), 'bin')
|
||||
if (!fs.existsSync(binPath)) {
|
||||
return
|
||||
}
|
||||
|
||||
const cacheId = await cache.saveCache([binPath], state.key)
|
||||
if (cacheId !== -1) {
|
||||
core.info(`Binary cache saved with key: ${state.key}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a function while preserving the mise binary. The tools cache includes
|
||||
* bin/, so restoring it could overwrite the binary that setupMise() just
|
||||
* installed. This backs up the binary before and restores it after.
|
||||
*/
|
||||
async function withBinaryBackup<T>(fn: () => Promise<T>): Promise<T> {
|
||||
const binPath = path.join(miseDir(), 'bin')
|
||||
const binBackup = path.join(
|
||||
os.tmpdir(),
|
||||
`mise-bin-backup-${crypto.randomBytes(8).toString('hex')}`
|
||||
)
|
||||
|
||||
if (!fs.existsSync(binPath)) {
|
||||
throw new Error(`Expected binary at ${binPath} but it does not exist`)
|
||||
}
|
||||
|
||||
await io.cp(binPath, binBackup, { recursive: true })
|
||||
|
||||
try {
|
||||
return await fn()
|
||||
} finally {
|
||||
try {
|
||||
await io.cp(binBackup, binPath, { recursive: true, force: true })
|
||||
} finally {
|
||||
// cleanup even if the restore fails
|
||||
await io.rmRF(binBackup)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreToolsCache(): Promise<CacheState> {
|
||||
const cacheKeyTemplate =
|
||||
core.getInput('cache_key') || DEFAULT_CACHE_KEY_TEMPLATE
|
||||
const key = await processCacheKeyTemplate(cacheKeyTemplate)
|
||||
|
||||
if (!key) {
|
||||
core.info('Tools caching disabled')
|
||||
return { key: '', hit: false }
|
||||
}
|
||||
|
||||
const cacheKey = await withBinaryBackup(() =>
|
||||
core.group('Restoring mise tools cache', async () => {
|
||||
const cachePath = miseDir()
|
||||
const restored = await cache.restoreCache([cachePath], key)
|
||||
if (restored) {
|
||||
core.info(`mise tools cache restored from key: ${restored}`)
|
||||
} else {
|
||||
core.info(`mise tools cache not found for ${key}`)
|
||||
}
|
||||
return restored
|
||||
})
|
||||
)
|
||||
|
||||
return { key, hit: Boolean(cacheKey) }
|
||||
}
|
||||
|
||||
async function setupMise(
|
||||
|
|
@ -377,9 +468,6 @@ const writeFile = async (p: fs.PathLike, body: string): Promise<void> =>
|
|||
run()
|
||||
|
||||
function miseDir(): string {
|
||||
const dir = core.getState('MISE_DIR')
|
||||
if (dir) return dir
|
||||
|
||||
const miseDir = core.getInput('mise_dir')
|
||||
if (miseDir) return miseDir
|
||||
|
||||
|
|
@ -392,18 +480,23 @@ function miseDir(): string {
|
|||
return path.join(os.homedir(), '.local', 'share', 'mise')
|
||||
}
|
||||
|
||||
async function saveCache(cacheKey: string): Promise<void> {
|
||||
await core.group(`Saving mise cache`, async () => {
|
||||
async function saveToolsCache(state: CacheState): Promise<void> {
|
||||
if (!core.getBooleanInput('cache_save') || state.hit || !state.key) {
|
||||
return
|
||||
}
|
||||
|
||||
await core.group('Saving mise tools cache', async () => {
|
||||
const cachePath = miseDir()
|
||||
|
||||
if (!fs.existsSync(cachePath)) {
|
||||
throw new Error(`Cache folder path does not exist on disk: ${cachePath}`)
|
||||
core.warning(`Cache folder path does not exist: ${cachePath}`)
|
||||
return
|
||||
}
|
||||
|
||||
const cacheId = await cache.saveCache([cachePath], cacheKey)
|
||||
if (cacheId === -1) return
|
||||
|
||||
core.info(`Cache saved from ${cachePath} with key: ${cacheKey}`)
|
||||
const cacheId = await cache.saveCache([cachePath], state.key)
|
||||
if (cacheId !== -1) {
|
||||
core.info(`Tools cache saved with key: ${state.key}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -425,16 +518,48 @@ async function getTarget(): Promise<string> {
|
|||
}
|
||||
}
|
||||
|
||||
async function processCacheKeyTemplate(template: string): Promise<string> {
|
||||
async function processCacheKeyTemplate(
|
||||
template: string
|
||||
): Promise<string | null> {
|
||||
// Get all available variables
|
||||
const version = core.getInput('version')
|
||||
const installArgs = core.getInput('install_args')
|
||||
const cacheKeyPrefix = core.getInput('cache_key_prefix') || 'mise-v0'
|
||||
const miseEnv = process.env.MISE_ENV?.replace(/,/g, '-')
|
||||
const platform = await getTarget()
|
||||
const workingDirectory = core.getInput('working_directory')
|
||||
|
||||
// Calculate file hash
|
||||
const fileHash = await glob.hashFiles(MISE_CONFIG_FILE_PATTERNS.join('\n'))
|
||||
// When working_directory is set, use mise config ls to get only relevant config files
|
||||
// Otherwise, use the glob pattern to get all config files in the repo
|
||||
let fileHash: string
|
||||
if (workingDirectory) {
|
||||
const configFiles = await configFilesForPath(workingDirectory)
|
||||
if (configFiles === null) {
|
||||
// Failed to get config files, skip caching
|
||||
return null
|
||||
}
|
||||
// Hash the contents of the config files
|
||||
// Include file path in each update to prevent collisions between different
|
||||
// file arrangements with the same concatenated content (e.g., "ab"+"cdef"
|
||||
// vs "abc"+"def")
|
||||
const hash = crypto.createHash('sha256')
|
||||
try {
|
||||
for (const file of configFiles) {
|
||||
hash.update(file)
|
||||
const content = await fs.promises.readFile(file)
|
||||
hash.update(content)
|
||||
}
|
||||
fileHash = hash.digest('hex')
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Failed to read config file for cache key: ${error}. Caching will be disabled.`
|
||||
)
|
||||
return null
|
||||
}
|
||||
} else {
|
||||
fileHash = await glob.hashFiles(MISE_CONFIG_FILE_PATTERNS.join('\n'))
|
||||
}
|
||||
|
||||
// Calculate install args hash
|
||||
let installArgsHash = ''
|
||||
|
|
@ -449,12 +574,21 @@ async function processCacheKeyTemplate(template: string): Promise<string> {
|
|||
}
|
||||
}
|
||||
|
||||
// Calculate mise dir hash to isolate caches for different mise_dir configurations
|
||||
// This matches the binary cache key which also includes dir_hash
|
||||
const dirHash = crypto
|
||||
.createHash('sha256')
|
||||
.update(miseDir())
|
||||
.digest('hex')
|
||||
.slice(0, 8)
|
||||
|
||||
// Prepare base template data
|
||||
const baseTemplateData = {
|
||||
version,
|
||||
cache_key_prefix: cacheKeyPrefix,
|
||||
platform,
|
||||
file_hash: fileHash,
|
||||
dir_hash: dirHash,
|
||||
mise_env: miseEnv,
|
||||
install_args_hash: installArgsHash
|
||||
}
|
||||
|
|
@ -483,3 +617,84 @@ async function isMusl() {
|
|||
})
|
||||
return stderr.indexOf('musl') > -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file path is contained within a directory.
|
||||
*
|
||||
* Uses path.relative() to compute the relative path from parent to child.
|
||||
* If the result starts with ".." or is absolute, the child is outside the parent.
|
||||
*
|
||||
* @example
|
||||
* isPathWithin("/workspace", "/workspace/src/file.ts") // true
|
||||
* isPathWithin("/workspace", "/workspace-other/file.ts") // false (not a child)
|
||||
* isPathWithin("/workspace", "/etc/passwd") // false (unrelated)
|
||||
* isPathWithin("C:\\work", "D:\\other\\file.ts") // false (different drive on Windows)
|
||||
*/
|
||||
function isPathWithin(parent: string, child: string): boolean {
|
||||
const relative = path.relative(parent, child)
|
||||
return !relative.startsWith('..') && !path.isAbsolute(relative)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config files that affect the given working directory using `mise config ls --json`.
|
||||
* This returns the hierarchy of configs (directory's own config + inherited parents).
|
||||
* Filters to only files within GITHUB_WORKSPACE and adds corresponding .lock files.
|
||||
*/
|
||||
async function configFilesForPath(
|
||||
workingDirectory: string
|
||||
): Promise<string[] | null> {
|
||||
const githubWorkspace = process.env.GITHUB_WORKSPACE || process.cwd()
|
||||
// Use explicit path to mise binary instead of relying on PATH
|
||||
const miseBinPath = path.join(
|
||||
miseDir(),
|
||||
'bin',
|
||||
process.platform === 'win32' ? 'mise.exe' : 'mise'
|
||||
)
|
||||
|
||||
try {
|
||||
const output = await exec.getExecOutput(
|
||||
miseBinPath,
|
||||
['config', 'ls', '--json'],
|
||||
{
|
||||
cwd: workingDirectory,
|
||||
silent: true
|
||||
}
|
||||
)
|
||||
|
||||
const configs: Array<{ path: string }> = JSON.parse(output.stdout)
|
||||
const configFiles: string[] = []
|
||||
|
||||
for (const config of configs) {
|
||||
const configPath = config.path
|
||||
// Filter to only files within GITHUB_WORKSPACE
|
||||
if (!isPathWithin(githubWorkspace, configPath)) {
|
||||
continue
|
||||
}
|
||||
configFiles.push(configPath)
|
||||
|
||||
// Include corresponding lock files if they exist
|
||||
let lockPath: string | undefined
|
||||
if (configPath.endsWith('.toml')) {
|
||||
// mise.toml -> mise.lock
|
||||
lockPath = configPath.replace(/\.toml$/, '.lock')
|
||||
} else if (configPath.endsWith('.tool-versions')) {
|
||||
// .tool-versions -> mise.lock in the same directory
|
||||
lockPath = path.join(path.dirname(configPath), 'mise.lock')
|
||||
}
|
||||
if (
|
||||
lockPath &&
|
||||
fs.existsSync(lockPath) &&
|
||||
!configFiles.includes(lockPath)
|
||||
) {
|
||||
configFiles.push(lockPath)
|
||||
}
|
||||
}
|
||||
|
||||
return configFiles.sort()
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Failed to get config files for working_directory "${workingDirectory}": ${error}. Caching will be disabled.`
|
||||
)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue