Unverified Commit e498d2a6 authored by Bruno Borges's avatar Bruno Borges Committed by GitHub
Browse files

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: default avatarCopilot App <[email protected]>
Copilot-Session: c0b19ff2-ef52-4ac1-a832-0f8f72c43219
parent 6a3384db
Loading
Loading
Loading
Loading
+54 −9
Changes for __tests__/cache.test.ts: 54 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -377,6 +377,10 @@ describe('dependency cache', () => {
      ReturnType<typeof cache.saveCache>,
      Parameters<typeof cache.saveCache>
    >;
    let spyGlobCreate: jest.SpyInstance<
      ReturnType<typeof glob.create>,
      Parameters<typeof glob.create>
    >;

    beforeEach(() => {
      spyCacheSave = jest
@@ -384,6 +388,9 @@ describe('dependency cache', () => {
        .mockImplementation((paths: string[], key: string) =>
          Promise.resolve(0)
        );
      spyGlobCreate = jest
        .spyOn(glob, 'create')
        .mockResolvedValue(createGlobber(['wrapper-path']));
      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 () => {
        createFile(join(workspace, 'pom.xml'));
        createStateForWrapperRestore('maven-wrapper', false);
        spyCacheSave.mockImplementation((paths: string[], key: string) => {
          if (paths.includes(join(os.homedir(), '.m2', 'wrapper', 'dists'))) {
            return Promise.reject(
              new cache.ValidationError('Path Validation Error')
        createDirectory(join(workspace, '.mvn'));
        createDirectory(join(workspace, '.mvn', 'wrapper'));
        createFile(
          join(workspace, '.mvn', 'wrapper', 'maven-wrapper.properties')
        );
          }
          return Promise.resolve(0);
        });
        createStateForWrapperRestore('maven-wrapper', false);
        spyGlobCreate.mockResolvedValue(createGlobber([]));

        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', () => {
@@ -553,6 +567,23 @@ describe('dependency cache', () => {
          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', () => {
      it('uploads cache even if no build.sbt found', async () => {
@@ -663,6 +694,20 @@ function createDirectory(path: string) {
  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 {
  if (os.platform() === 'darwin') {
    return `/private${workspace}`;
+8
Changes for dist/cleanup/index.js: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -50898,6 +50898,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
            core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
            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 {
            const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
            if (cacheId === -1) {
+8
Changes for dist/setup/index.js: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -76733,6 +76733,14 @@ function saveAdditionalCache(packageManager, additionalCache) {
            core.info(`Cache hit occurred on the ${additionalCache.name} primary key ${primaryKey}, not saving cache.`);
            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 {
            const cacheId = yield cache.saveCache(additionalCache.path, primaryKey);
            if (cacheId === -1) {
+12
Changes for src/cache.ts: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -319,6 +319,18 @@ async function saveAdditionalCache(
    );
    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 {
    const cacheId = await cache.saveCache(additionalCache.path, primaryKey);
    if (cacheId === -1) {