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

Optimize Temurin tool-cache fast path with lazy loading (#1179)

* Optimize Temurin tool-cache fast path

- Lazy-load distribution installers so only the selected distro module is initialized
- Defer cache feature/cache module loading until cache input is provided
- Start cache restore early and await it safely alongside Java setup flow
- Update orchestration and lazy-loading tests; regenerate dist artifacts

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: cc2c0256-c55e-4a35-a584-5bed7e8b11ad

* Address PR review comments

- Lazy-load cache save in cleanup path so no-cache runs avoid cache module init in post action
- Stage dist/setup/package.json in release script for chunked setup bundle completeness

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: cc2c0256-c55e-4a35-a584-5bed7e8b11ad

* Fix CodeQL comment tag filter finding

Patch is-unsafe's XML comment-close detector during builds so generated bundles recognize both HTML comment end forms and satisfy CodeQL until the dependency publishes a fix.

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: 277302b1-aa95-4012-817b-9752cdaee14e

* Rebuild generated dist bundles

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: cc2c0256-c55e-4a35-a584-5bed7e8b11ad

---------

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: cc2c0256-c55e-4a35-a584-5bed7e8b11ad
Copilot-Session: 277302b1-aa95-4012-817b-9752cdaee14e
parent 6937f5eb
Loading
Loading
Loading
Loading
+80
Changes for __tests__/cache-feature.test.ts: 80 added lines, 0 removed lines.
Original line number Diff line number Diff line
import {jest, describe, it, expect, afterEach} from '@jest/globals';

jest.unstable_mockModule('@actions/cache', () => ({
  isFeatureAvailable: jest.fn()
}));

jest.unstable_mockModule('@actions/core', () => ({
  warning: jest.fn(),
  debug: jest.fn(),
  getInput: jest.fn(),
  getBooleanInput: jest.fn(),
  getMultilineInput: jest.fn(),
  info: jest.fn(),
  error: jest.fn(),
  notice: jest.fn(),
  setFailed: jest.fn(),
  setOutput: jest.fn(),
  addPath: jest.fn(),
  exportVariable: jest.fn(),
  saveState: jest.fn(),
  getState: jest.fn(),
  setSecret: jest.fn(),
  isDebug: jest.fn(() => false),
  startGroup: jest.fn(),
  endGroup: jest.fn(),
  group: jest.fn((_name: string, fn: () => Promise<unknown>) => fn()),
  toPlatformPath: jest.fn((p: string) => p),
  toWin32Path: jest.fn((p: string) => p),
  toPosixPath: jest.fn((p: string) => p)
}));

const cache = await import('@actions/cache');
const core = await import('@actions/core');
const {isCacheFeatureAvailable} = await import('../src/cache-feature.js');

describe('isCacheFeatureAvailable', () => {
  it('is disabled on GHES when cache feature is unavailable', () => {
    (cache.isFeatureAvailable as jest.Mock<any>).mockImplementation(
      () => false
    );
    const warningMock = core.warning as jest.Mock;
    const message =
      'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.';

    try {
      process.env['GITHUB_SERVER_URL'] = 'http://example.com';
      expect(isCacheFeatureAvailable()).toBe(false);
      expect(warningMock).toHaveBeenCalledWith(message);
    } finally {
      delete process.env['GITHUB_SERVER_URL'];
    }
  });

  it('is disabled on dotcom when cache feature is unavailable', () => {
    (cache.isFeatureAvailable as jest.Mock<any>).mockImplementation(
      () => false
    );
    const warningMock = core.warning as jest.Mock;
    const message =
      'The runner was not able to contact the cache service. Caching will be skipped';

    try {
      process.env['GITHUB_SERVER_URL'] = 'http://github.com';
      expect(isCacheFeatureAvailable()).toBe(false);
      expect(warningMock).toHaveBeenCalledWith(message);
    } finally {
      delete process.env['GITHUB_SERVER_URL'];
    }
  });

  it('is enabled when cache feature is available', () => {
    (cache.isFeatureAvailable as jest.Mock<any>).mockImplementation(() => true);
    expect(isCacheFeatureAvailable()).toBe(true);
  });

  afterEach(() => {
    jest.resetAllMocks();
    jest.clearAllMocks();
  });
});
+23
Changes for __tests__/distributors/distribution-factory.lazy-loading.test.ts: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
import {jest, describe, it, expect} from '@jest/globals';

jest.unstable_mockModule('../../src/distributions/zulu/installer.js', () => {
  throw new Error(
    'Zulu installer module must not be imported on the Temurin fast path'
  );
});

const {getJavaDistribution} =
  await import('../../src/distributions/distribution-factory.js');

describe('distribution factory lazy loading', () => {
  it('does not load non-selected distribution installers for Temurin', async () => {
    const distribution = await getJavaDistribution('temurin', {
      version: '21',
      architecture: 'x64',
      packageType: 'jdk',
      checkLatest: false
    });

    expect(distribution).not.toBeNull();
  });
});
+28 −25
Changes for __tests__/distributors/distribution-factory.test.ts: 28 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ const installerOptions = (packageType: string, version = '25') => ({
describe('getJavaDistribution', () => {
  it.each(supportedDistributionsOnCurrentPlatform)(
    'uses the shared retrying HTTP client for %s',
    distributionName => {
      const distribution = getJavaDistribution(distributionName, {
    async distributionName => {
      const distribution = await getJavaDistribution(distributionName, {
        version: '25',
        architecture: 'x64',
        packageType: 'jdk',
@@ -51,43 +51,46 @@ describe('getJavaDistribution', () => {
          ? packageTypes.map(packageType => [distributionName, packageType])
          : []
    )
  )('accepts %s with java-package %s', (distributionName, packageType) => {
  )(
    'accepts %s with java-package %s',
    async (distributionName, packageType) => {
      expect(
      getJavaDistribution(
        await getJavaDistribution(
          distributionName,
          installerOptions(packageType as string)
        )
      ).not.toBeNull();
  });
    }
  );

  it.each(Object.entries(JAVA_PACKAGE_CAPABILITIES))(
    'rejects unsupported java-package values for %s',
    (distributionName, packageTypes) => {
      expect(() =>
    async (distributionName, packageTypes) => {
      await expect(
        getJavaDistribution(distributionName, installerOptions('jdk+typo'))
      ).toThrow(
      ).rejects.toThrow(
        `Java package 'jdk+typo' is not supported for distribution '${distributionName}'. Supported package types: ${packageTypes.join(', ')}.`
      );
    }
  );

  it("rejects java-package 'jdk+jmods' for non-Temurin distributions", () => {
    expect(() =>
  it("rejects java-package 'jdk+jmods' for non-Temurin distributions", async () => {
    await expect(
      getJavaDistribution('zulu', installerOptions('jdk+jmods'))
    ).toThrow(
    ).rejects.toThrow(
      "Java package 'jdk+jmods' is not supported for distribution 'zulu'. Supported package types: jdk, jre, jdk+fx, jre+fx, jdk+crac, jre+crac."
    );
  });

  it.each(['8', '23.x', '23.0.1.1', '<24'])(
    "rejects Temurin java-package 'jdk+jmods' for version %s",
    version => {
      expect(() =>
    async version => {
      await expect(
        getJavaDistribution(
          JavaDistribution.Temurin,
          installerOptions('jdk+jmods', version)
        )
      ).toThrow(
      ).rejects.toThrow(
        `Java package 'jdk+jmods' is not supported for distribution 'temurin'. Supported package types: jdk, jre, jdk+jmods. Package 'jdk+jmods' requires Java 24 or later; requested version '${version}'.`
      );
    }
@@ -95,9 +98,9 @@ describe('getJavaDistribution', () => {

  it.each(['24', '24.0.1.1', '25-ea', '>=21', 'latest'])(
    "accepts Temurin java-package 'jdk+jmods' for version %s",
    version => {
    async version => {
      expect(
        getJavaDistribution(
        await getJavaDistribution(
          JavaDistribution.Temurin,
          installerOptions('jdk+jmods', version)
        )
@@ -105,9 +108,9 @@ describe('getJavaDistribution', () => {
    }
  );

  it('preserves unsupported distribution handling', () => {
  it('preserves unsupported distribution handling', async () => {
    expect(
      getJavaDistribution(
      await getJavaDistribution(
        'not-a-distribution',
        installerOptions('not-a-package')
      )
@@ -118,8 +121,8 @@ describe('getJavaDistribution', () => {
    ['amd64', 'x64'],
    ['ia32', 'x86'],
    ['arm64', 'aarch64']
  ])('passes normalized architecture %s as %s', (input, expected) => {
    const normalized = getJavaDistribution(JavaDistribution.JdkFile, {
  ])('passes normalized architecture %s as %s', async (input, expected) => {
    const normalized = await getJavaDistribution(JavaDistribution.JdkFile, {
      ...installerOptions('jdk'),
      architecture: input
    });
@@ -127,8 +130,8 @@ describe('getJavaDistribution', () => {
    expect(normalized!['architecture']).toBe(expected);
  });

  it('uses the runner architecture when the input is empty', () => {
    const distribution = getJavaDistribution(JavaDistribution.Temurin, {
  it('uses the runner architecture when the input is empty', async () => {
    const distribution = await getJavaDistribution(JavaDistribution.Temurin, {
      ...installerOptions('jdk'),
      architecture: ''
    });
@@ -137,12 +140,12 @@ describe('getJavaDistribution', () => {
    expect(distribution!['architecture']).toBe(expected);
  });

  it('rejects an unsupported combination before creating an HTTP client', () => {
    expect(() =>
  it('rejects an unsupported combination before creating an HTTP client', async () => {
    await expect(
      getJavaDistribution(JavaDistribution.Oracle, {
        ...installerOptions('jdk'),
        architecture: 'x86'
      })
    ).toThrow(/does not support operating system/);
    ).rejects.toThrow(/does not support operating system/);
  });
});
+5 −2
Changes for __tests__/distributors/graalvm-installer.test.ts: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -1403,8 +1403,11 @@ describe('distribution factory', () => {
    checkLatest: false
  };

  it('should map graalvm-community to the community installer', () => {
    const community = getJavaDistribution('graalvm-community', defaultOptions);
  it('should map graalvm-community to the community installer', async () => {
    const community = await getJavaDistribution(
      'graalvm-community',
      defaultOptions
    );

    expect(community).toBeInstanceOf(GraalVMCommunityDistribution);
  });
+2 −2
Changes for __tests__/distributors/openjdk-installer.test.ts: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -233,8 +233,8 @@ describe('OpenJdkDistribution', () => {
    expect(windowsRelease[0].url.endsWith('.tar.gz')).toBe(true);
  });

  it('is registered in the distribution factory', () => {
    const distribution = getJavaDistribution('oracle-openjdk', {
  it('is registered in the distribution factory', async () => {
    const distribution = await getJavaDistribution('oracle-openjdk', {
      version: '26',
      architecture: 'x64',
      packageType: 'jdk',
Loading