Backport #1151: Fix missing wrapper cache distributions (#1153)

Skip optional Maven and Gradle wrapper cache saves when their distribution paths do not exist, while allowing the main dependency cache to save.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c0b19ff2-ef52-4ac1-a832-0f8f72c43219
This commit is contained in:
Bruno Borges 2026-07-28 21:46:39 -04:00 committed by GitHub
parent 6a3384db74
commit e498d2a66a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 82 additions and 9 deletions

View File

@ -377,6 +377,10 @@ describe('dependency cache', () => {
ReturnType<typeof cache.saveCache>, ReturnType<typeof cache.saveCache>,
Parameters<typeof cache.saveCache> Parameters<typeof cache.saveCache>
>; >;
let spyGlobCreate: jest.SpyInstance<
ReturnType<typeof glob.create>,
Parameters<typeof glob.create>
>;
beforeEach(() => { beforeEach(() => {
spyCacheSave = jest spyCacheSave = jest
@ -384,6 +388,9 @@ describe('dependency cache', () => {
.mockImplementation((paths: string[], key: string) => .mockImplementation((paths: string[], key: string) =>
Promise.resolve(0) Promise.resolve(0)
); );
spyGlobCreate = jest
.spyOn(glob, 'create')
.mockResolvedValue(createGlobber(['wrapper-path']));
spyWarning.mockImplementation(() => null); spyWarning.mockImplementation(() => null);
}); });
@ -469,17 +476,24 @@ describe('dependency cache', () => {
}); });
it('does not fail the post step when the wrapper distribution path is missing', async () => { it('does not fail the post step when the wrapper distribution path is missing', async () => {
createFile(join(workspace, 'pom.xml')); createFile(join(workspace, 'pom.xml'));
createDirectory(join(workspace, '.mvn'));
createDirectory(join(workspace, '.mvn', 'wrapper'));
createFile(
join(workspace, '.mvn', 'wrapper', 'maven-wrapper.properties')
);
createStateForWrapperRestore('maven-wrapper', false); createStateForWrapperRestore('maven-wrapper', false);
spyCacheSave.mockImplementation((paths: string[], key: string) => { spyGlobCreate.mockResolvedValue(createGlobber([]));
if (paths.includes(join(os.homedir(), '.m2', 'wrapper', 'dists'))) {
return Promise.reject(
new cache.ValidationError('Path Validation Error')
);
}
return Promise.resolve(0);
});
await expect(save('maven')).resolves.not.toThrow(); await expect(save('maven')).resolves.toBeUndefined();
expect(spyCacheSave).not.toHaveBeenCalledWith(
[join(os.homedir(), '.m2', 'wrapper', 'dists')],
expect.any(String)
);
expect(spyCacheSave).toHaveBeenCalledWith(
[join(os.homedir(), '.m2', 'repository')],
'setup-java-cache-primary-key'
);
expect(spyWarning).not.toHaveBeenCalled();
}); });
}); });
describe('for gradle', () => { describe('for gradle', () => {
@ -553,6 +567,23 @@ describe('dependency cache', () => {
expect.any(String) expect.any(String)
); );
}); });
it('does not fail the post step when the wrapper distribution path is missing', async () => {
createFile(join(workspace, 'build.gradle'));
createFile(join(workspace, 'gradle-wrapper.properties'));
createStateForWrapperRestore('gradle-wrapper', false);
spyGlobCreate.mockResolvedValue(createGlobber([]));
await expect(save('gradle')).resolves.toBeUndefined();
expect(spyCacheSave).not.toHaveBeenCalledWith(
[join(os.homedir(), '.gradle', 'wrapper')],
expect.any(String)
);
expect(spyCacheSave).toHaveBeenCalledWith(
[join(os.homedir(), '.gradle', 'caches')],
'setup-java-cache-primary-key'
);
expect(spyWarning).not.toHaveBeenCalled();
});
}); });
describe('for sbt', () => { describe('for sbt', () => {
it('uploads cache even if no build.sbt found', async () => { it('uploads cache even if no build.sbt found', async () => {
@ -663,6 +694,20 @@ function createDirectory(path: string) {
fs.mkdirSync(path); fs.mkdirSync(path);
} }
function createGlobber(
paths: string[]
): Awaited<ReturnType<typeof glob.create>> {
return {
getSearchPaths: () => [],
glob: () => Promise.resolve(paths),
globGenerator: async function* () {
for (const path of paths) {
yield path;
}
}
};
}
function projectRoot(workspace: string): string { function projectRoot(workspace: string): string {
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
return `/private${workspace}`; return `/private${workspace}`;

View File

@ -50898,6 +50898,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`); core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
return; return;
} }
const globber = yield glob.create(additionalCache.path.join('\n'), {
implicitDescendants: false
});
const cachePaths = yield globber.glob();
if (cachePaths.length === 0) {
core.debug(`${additionalCache.name} cache paths do not exist, not saving cache.`);
return;
}
try { try {
const cacheId = yield cache.saveCache(additionalCache.path, primaryKey); const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
if (cacheId === -1) { if (cacheId === -1) {

8
dist/setup/index.js vendored
View File

@ -76733,6 +76733,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`); core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
return; return;
} }
const globber = yield glob.create(additionalCache.path.join('\n'), {
implicitDescendants: false
});
const cachePaths = yield globber.glob();
if (cachePaths.length === 0) {
core.debug(`${additionalCache.name} cache paths do not exist, not saving cache.`);
return;
}
try { try {
const cacheId = yield cache.saveCache(additionalCache.path, primaryKey); const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
if (cacheId === -1) { if (cacheId === -1) {

View File

@ -319,6 +319,18 @@ async function saveAdditionalCache(
); );
return; return;
} }
const globber = await glob.create(additionalCache.path.join('\n'), {
implicitDescendants: false
});
const cachePaths = await globber.glob();
if (cachePaths.length === 0) {
core.debug(
`${additionalCache.name} cache paths do not exist, not saving cache.`
);
return;
}
try { try {
const cacheId = await cache.saveCache(additionalCache.path, primaryKey); const cacheId = await cache.saveCache(additionalCache.path, primaryKey);
if (cacheId === -1) { if (cacheId === -1) {