Skip to content

Commit e498d2a

Browse files
brunoborgesCopilot
andauthored
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
1 parent 6a3384d commit e498d2a

4 files changed

Lines changed: 82 additions & 9 deletions

File tree

‎__tests__/cache.test.ts‎

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,20 @@ describe('dependency cache', () => {
377377
ReturnType<typeof cache.saveCache>,
378378
Parameters<typeof cache.saveCache>
379379
>;
380+
let spyGlobCreate: jest.SpyInstance<
381+
ReturnType<typeof glob.create>,
382+
Parameters<typeof glob.create>
383+
>;
380384

381385
beforeEach(() => {
382386
spyCacheSave = jest
383387
.spyOn(cache, 'saveCache')
384388
.mockImplementation((paths: string[], key: string) =>
385389
Promise.resolve(0)
386390
);
391+
spyGlobCreate = jest
392+
.spyOn(glob, 'create')
393+
.mockResolvedValue(createGlobber(['wrapper-path']));
387394
spyWarning.mockImplementation(() => null);
388395
});
389396

@@ -469,17 +476,24 @@ describe('dependency cache', () => {
469476
});
470477
it('does not fail the post step when the wrapper distribution path is missing', async () => {
471478
createFile(join(workspace, 'pom.xml'));
479+
createDirectory(join(workspace, '.mvn'));
480+
createDirectory(join(workspace, '.mvn', 'wrapper'));
481+
createFile(
482+
join(workspace, '.mvn', 'wrapper', 'maven-wrapper.properties')
483+
);
472484
createStateForWrapperRestore('maven-wrapper', false);
473-
spyCacheSave.mockImplementation((paths: string[], key: string) => {
474-
if (paths.includes(join(os.homedir(), '.m2', 'wrapper', 'dists'))) {
475-
return Promise.reject(
476-
new cache.ValidationError('Path Validation Error')
477-
);
478-
}
479-
return Promise.resolve(0);
480-
});
485+
spyGlobCreate.mockResolvedValue(createGlobber([]));
481486

482-
await expect(save('maven')).resolves.not.toThrow();
487+
await expect(save('maven')).resolves.toBeUndefined();
488+
expect(spyCacheSave).not.toHaveBeenCalledWith(
489+
[join(os.homedir(), '.m2', 'wrapper', 'dists')],
490+
expect.any(String)
491+
);
492+
expect(spyCacheSave).toHaveBeenCalledWith(
493+
[join(os.homedir(), '.m2', 'repository')],
494+
'setup-java-cache-primary-key'
495+
);
496+
expect(spyWarning).not.toHaveBeenCalled();
483497
});
484498
});
485499
describe('for gradle', () => {
@@ -553,6 +567,23 @@ describe('dependency cache', () => {
553567
expect.any(String)
554568
);
555569
});
570+
it('does not fail the post step when the wrapper distribution path is missing', async () => {
571+
createFile(join(workspace, 'build.gradle'));
572+
createFile(join(workspace, 'gradle-wrapper.properties'));
573+
createStateForWrapperRestore('gradle-wrapper', false);
574+
spyGlobCreate.mockResolvedValue(createGlobber([]));
575+
576+
await expect(save('gradle')).resolves.toBeUndefined();
577+
expect(spyCacheSave).not.toHaveBeenCalledWith(
578+
[join(os.homedir(), '.gradle', 'wrapper')],
579+
expect.any(String)
580+
);
581+
expect(spyCacheSave).toHaveBeenCalledWith(
582+
[join(os.homedir(), '.gradle', 'caches')],
583+
'setup-java-cache-primary-key'
584+
);
585+
expect(spyWarning).not.toHaveBeenCalled();
586+
});
556587
});
557588
describe('for sbt', () => {
558589
it('uploads cache even if no build.sbt found', async () => {
@@ -663,6 +694,20 @@ function createDirectory(path: string) {
663694
fs.mkdirSync(path);
664695
}
665696

697+
function createGlobber(
698+
paths: string[]
699+
): Awaited<ReturnType<typeof glob.create>> {
700+
return {
701+
getSearchPaths: () => [],
702+
glob: () => Promise.resolve(paths),
703+
globGenerator: async function* () {
704+
for (const path of paths) {
705+
yield path;
706+
}
707+
}
708+
};
709+
}
710+
666711
function projectRoot(workspace: string): string {
667712
if (os.platform() === 'darwin') {
668713
return `/private${workspace}`;

‎dist/cleanup/index.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50898,6 +50898,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
5089850898
core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
5089950899
return;
5090050900
}
50901+
const globber = yield glob.create(additionalCache.path.join('\n'), {
50902+
implicitDescendants: false
50903+
});
50904+
const cachePaths = yield globber.glob();
50905+
if (cachePaths.length === 0) {
50906+
core.debug(`${additionalCache.name} cache paths do not exist, not saving cache.`);
50907+
return;
50908+
}
5090150909
try {
5090250910
const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
5090350911
if (cacheId === -1) {

‎dist/setup/index.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76733,6 +76733,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
7673376733
core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
7673476734
return;
7673576735
}
76736+
const globber = yield glob.create(additionalCache.path.join('\n'), {
76737+
implicitDescendants: false
76738+
});
76739+
const cachePaths = yield globber.glob();
76740+
if (cachePaths.length === 0) {
76741+
core.debug(`${additionalCache.name} cache paths do not exist, not saving cache.`);
76742+
return;
76743+
}
7673676744
try {
7673776745
const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
7673876746
if (cacheId === -1) {

‎src/cache.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,18 @@ async function saveAdditionalCache(
319319
);
320320
return;
321321
}
322+
323+
const globber = await glob.create(additionalCache.path.join('\n'), {
324+
implicitDescendants: false
325+
});
326+
const cachePaths = await globber.glob();
327+
if (cachePaths.length === 0) {
328+
core.debug(
329+
`${additionalCache.name} cache paths do not exist, not saving cache.`
330+
);
331+
return;
332+
}
333+
322334
try {
323335
const cacheId = await cache.saveCache(additionalCache.path, primaryKey);
324336
if (cacheId === -1) {

0 commit comments

Comments
 (0)