fix: allow multiple invocations with caching enabled

This fix addresses the issue where calling setup-go multiple times with
caching enabled in the same workflow would fail because the second
invocation attempted to save to the same cache key.

Changes:
- Add tracking of processed cache keys using state variables to prevent
  duplicate cache save attempts
- Add helper functions in constants.ts for state management:
  - getAlreadyCachedKey()/setAlreadyCachedKey(): Track keys already in cache
  - getPrimaryCacheKey()/setPrimaryCacheKey(): Track the primary key for
    each invocation
  - getCachedGoModPath()/setCachedGoModPath(): Track which go.mod was cached
- Modify cache-restore.ts to store state about the cache operation
- Modify cache-save.ts to check if cache was already saved for this
  go.mod path before attempting to save again
- Add comprehensive tests for the multiple invocation scenario

This enables workflows that need to setup Go with different configurations
(e.g., different working directories) multiple times without cache
conflicts.

Assisted-By: cagent
This commit is contained in:
maxcleme 2026-03-03 18:14:01 +01:00
parent 27fdb267c1
commit 49fe0b8fcc
No known key found for this signature in database
GPG key ID: 83447FC995D32C99
7 changed files with 642 additions and 31 deletions

53
dist/setup/index.js vendored
View file

@ -76820,6 +76820,16 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa
const linuxVersion = process.env.RUNNER_OS === 'Linux' ? `${process.env.ImageOS}-` : '';
const primaryKey = `setup-go-${platform}-${arch}-${linuxVersion}go-${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`);
// Check if this key was already processed in a previous invocation
const existingKeys = getExistingPrimaryKeys();
if (existingKeys.includes(primaryKey)) {
core.info(`Cache key ${primaryKey} already processed in this job, skipping restore`);
core.setOutput(constants_1.Outputs.CacheHit, true);
return;
}
// Save state for post step - accumulate keys for multiple invocations
addPrimaryKey(primaryKey);
// Legacy single-key state (for backward compatibility)
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey);
core.setOutput(constants_1.Outputs.CacheHit, Boolean(cacheKey));
@ -76828,6 +76838,9 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa
core.setOutput(constants_1.Outputs.CacheHit, false);
return;
}
// Save matched key state - accumulate for multiple invocations
addMatchedKey(cacheKey);
// Legacy single-key state (for backward compatibility)
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
core.info(`Cache restored from key: ${cacheKey}`);
});
@ -76842,6 +76855,43 @@ const findDependencyFile = (packageManager) => {
}
return path_1.default.join(workspace, dependencyFile);
};
// Helper functions for managing multiple cache keys
function getExistingPrimaryKeys() {
try {
const keysJson = core.getState(constants_1.State.CachePrimaryKeys);
if (!keysJson)
return [];
return JSON.parse(keysJson);
}
catch (_a) {
return [];
}
}
function addPrimaryKey(key) {
const existingKeys = getExistingPrimaryKeys();
if (!existingKeys.includes(key)) {
existingKeys.push(key);
core.saveState(constants_1.State.CachePrimaryKeys, JSON.stringify(existingKeys));
}
}
function getExistingMatchedKeys() {
try {
const keysJson = core.getState(constants_1.State.CacheMatchedKeys);
if (!keysJson)
return [];
return JSON.parse(keysJson);
}
catch (_a) {
return [];
}
}
function addMatchedKey(key) {
const existingKeys = getExistingMatchedKeys();
if (!existingKeys.includes(key)) {
existingKeys.push(key);
core.saveState(constants_1.State.CacheMatchedKeys, JSON.stringify(existingKeys));
}
}
/***/ }),
@ -76972,6 +77022,9 @@ var State;
(function (State) {
State["CachePrimaryKey"] = "CACHE_KEY";
State["CacheMatchedKey"] = "CACHE_RESULT";
// For multiple invocations support - stores JSON arrays of keys
State["CachePrimaryKeys"] = "CACHE_KEYS";
State["CacheMatchedKeys"] = "CACHE_RESULTS";
})(State || (exports.State = State = {}));
var Outputs;
(function (Outputs) {