Compare commits

..

2 Commits

Author SHA1 Message Date
priya-kinthali
d7a11313b5 Enhance caching in setup-node with automatic package manager detection (#1348)
* setup node in local

* Enhance caching in setup-node with package manager filed detection

* updated with array

* update the field
2025-08-25 21:40:12 -05:00
gowridurgad
5e2628c959 Bumps form-data (#1332)
Co-authored-by: “gowridurgad” <“hgowridurgad@github.com>
2025-07-31 15:39:40 -05:00
9 changed files with 4211 additions and 1908 deletions

View File

@@ -6,7 +6,7 @@ on:
- '**.md' - '**.md'
push: push:
branches: branches:
- test-macos-x64-runner - main
- releases/* - releases/*
paths-ignore: paths-ignore:
- '**.md' - '**.md'
@@ -18,7 +18,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22] node-version: [18, 20, 22]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -41,7 +41,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22] node-version: [18, 20, 22]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -74,7 +74,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20] node-version: [18, 20]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -106,7 +106,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22] node-version: [18, 20, 22]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -243,3 +243,28 @@ jobs:
cache-dependency-path: | cache-dependency-path: |
sub2/*.lock sub2/*.lock
sub3/*.lock sub3/*.lock
node-npm-package-manager-cache:
name: Test enabling cache if package manager field is present (Node ${{ matrix.node-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- name: Create package.json with packageManager field
run: |
echo '{ "name": "test-project", "version": "1.0.0", "packageManager": "npm@8.0.0" }' > package.json
- name: Clean global cache
run: npm cache clean --force
- name: Setup Node with caching enabled
uses: ./
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Verify node and npm
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
shell: bash

View File

@@ -6,7 +6,7 @@ on:
- '**.md' - '**.md'
push: push:
branches: branches:
- test-macos-x64-runner - main
- releases/* - releases/*
paths-ignore: paths-ignore:
- '**.md' - '**.md'
@@ -17,7 +17,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22] node-version: [18, 20, 22]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -34,7 +34,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-13]
node-version: [lts/dubnium, lts/erbium, lts/fermium, lts/*, lts/-1] node-version: [lts/dubnium, lts/erbium, lts/fermium, lts/*, lts/-1]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -56,7 +56,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: node-version:
[ [
'20-v8-canary', '20-v8-canary',
@@ -81,7 +81,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [20-nightly, 21-nightly, 18.0.0-nightly] node-version: [20-nightly, 21-nightly, 18.0.0-nightly]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -101,7 +101,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [20.0.0-rc.1, 18.0.0-rc.2, 19.0.0-rc.0] node-version: [20.0.0-rc.1, 18.0.0-rc.2, 19.0.0-rc.0]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -121,7 +121,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18.20.0, 20.10.0, 22.0.0] node-version: [18.20.0, 20.10.0, 22.0.0]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -138,7 +138,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [18, 20, 22] node-version: [18, 20, 22]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -156,7 +156,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version-file: node-version-file:
[.nvmrc, .tool-versions, .tool-versions-node, package.json] [.nvmrc, .tool-versions, .tool-versions-node, package.json]
steps: steps:
@@ -173,7 +173,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Setup node from node version file - name: Setup node from node version file
@@ -188,7 +188,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Setup node from node version file - name: Setup node from node version file
@@ -203,7 +203,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [17, 19] node-version: [17, 19]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -220,7 +220,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-13]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
# test old versions which didn't have npm and layout different # test old versions which didn't have npm and layout different
@@ -250,7 +250,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [macos-14-large] os: [ubuntu-latest, windows-latest, macos-latest, macos-13]
node-version: [current, latest, node] node-version: [current, latest, node]
steps: steps:
- name: Get node version - name: Get node version

View File

@@ -135,7 +135,19 @@ It's **always** recommended to commit the lockfile of your package manager for s
## Caching global packages data ## Caching global packages data
The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under the hood for caching global packages data but requires less configuration settings. Supported package managers are `npm`, `yarn`, `pnpm` (v6.10+). The `cache` input is optional, and caching is turned off by default. The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under the hood for caching global packages data but requires less configuration settings. Supported package managers are `npm`, `yarn`, `pnpm` (v6.10+). The `cache` input is optional.
Caching is turned on by default when a `packageManager` field is detected in the `package.json` file. The `package-manager-cache` input provides control over this automatic caching behavior. By default, `package-manager-cache` is set to `true`, which enables caching when a valid package manager field is detected in the `package.json` file. To disable this automatic caching, set the `package-manager-cache` input to `false`.
```yaml
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
package-manager-cache: false
- run: npm ci
```
> If no valid `packageManager` field is detected in the `package.json` file, caching will remain disabled unless explicitly configured.
The action defaults to search for the dependency file (`package-lock.json`, `npm-shrinkwrap.json` or `yarn.lock`) in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases when multiple dependency files are used, or they are located in different subdirectories. The action defaults to search for the dependency file (`package-lock.json`, `npm-shrinkwrap.json` or `yarn.lock`) in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases when multiple dependency files are used, or they are located in different subdirectories.

View File

@@ -20,6 +20,7 @@ describe('main tests', () => {
let infoSpy: jest.SpyInstance; let infoSpy: jest.SpyInstance;
let warningSpy: jest.SpyInstance; let warningSpy: jest.SpyInstance;
let saveStateSpy: jest.SpyInstance;
let inSpy: jest.SpyInstance; let inSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance; let setOutputSpy: jest.SpyInstance;
let startGroupSpy: jest.SpyInstance; let startGroupSpy: jest.SpyInstance;
@@ -53,6 +54,8 @@ describe('main tests', () => {
setOutputSpy.mockImplementation(() => {}); setOutputSpy.mockImplementation(() => {});
warningSpy = jest.spyOn(core, 'warning'); warningSpy = jest.spyOn(core, 'warning');
warningSpy.mockImplementation(() => {}); warningSpy.mockImplementation(() => {});
saveStateSpy = jest.spyOn(core, 'saveState');
saveStateSpy.mockImplementation(() => {});
startGroupSpy = jest.spyOn(core, 'startGroup'); startGroupSpy = jest.spyOn(core, 'startGroup');
startGroupSpy.mockImplementation(() => {}); startGroupSpy.mockImplementation(() => {});
endGroupSpy = jest.spyOn(core, 'endGroup'); endGroupSpy = jest.spyOn(core, 'endGroup');
@@ -280,4 +283,65 @@ describe('main tests', () => {
); );
}); });
}); });
describe('cache feature tests', () => {
it('Should enable caching with the resolved package manager from packageManager field in package.json when the cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
packageManager: 'yarn@3.2.0'
})
);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'yarn');
});
it('Should not enable caching if the packageManager field is missing in package.json and the cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
//packageManager field is not present
})
);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should skip caching when package-manager-cache is false', async () => {
inputs['package-manager-cache'] = 'false';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should enable caching with cache input explicitly provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = 'npm'; // Explicit cache input provided
inSpy.mockImplementation(name => inputs[name]);
isCacheActionAvailable.mockReturnValue(true);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
});
});
}); });

View File

@@ -23,6 +23,9 @@ inputs:
default: ${{ github.server_url == 'https://github.com' && github.token || '' }} default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
cache: cache:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.' description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
package-manager-cache:
description: 'Set to false to disable automatic caching based on the package manager field in package.json. By default, caching is enabled if the package manager field is present.'
default: true
cache-dependency-path: cache-dependency-path:
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.' description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
mirror: mirror:

1382
dist/cache-save/index.js vendored

File diff suppressed because it is too large Load Diff

4558
dist/setup/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,7 @@ import {getPackageManagerInfo} from './cache-utils';
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn. // throw an uncaught exception. Instead of failing this action, just warn.
process.on('uncaughtException', e => { process.on('uncaughtException', e => {
const warningPrefix = '[warning]'; const warningPrefix = '[warning]';
core.info(`${warningPrefix}${e.message}`); core.info(`${warningPrefix}${e.message}`);

View File

@@ -1,6 +1,7 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import os from 'os'; import os from 'os';
import fs from 'fs';
import * as auth from './authutil'; import * as auth from './authutil';
import * as path from 'path'; import * as path from 'path';
@@ -20,6 +21,9 @@ export async function run() {
let arch = core.getInput('architecture'); let arch = core.getInput('architecture');
const cache = core.getInput('cache'); const cache = core.getInput('cache');
const packagemanagercache =
(core.getInput('package-manager-cache') || 'true').toUpperCase() ===
'TRUE';
// if architecture supplied but node-version is not // if architecture supplied but node-version is not
// if we don't throw a warning, the already installed x64 node will be used which is not probably what user meant. // if we don't throw a warning, the already installed x64 node will be used which is not probably what user meant.
@@ -63,10 +67,14 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth); auth.configAuthentication(registryUrl, alwaysAuth);
} }
const resolvedPackageManager = getNameFromPackageManagerField();
const cacheDependencyPath = core.getInput('cache-dependency-path');
if (cache && isCacheFeatureAvailable()) { if (cache && isCacheFeatureAvailable()) {
core.saveState(State.CachePackageManager, cache); core.saveState(State.CachePackageManager, cache);
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath); await restoreCache(cache, cacheDependencyPath);
} else if (resolvedPackageManager && packagemanagercache) {
core.saveState(State.CachePackageManager, resolvedPackageManager);
await restoreCache(resolvedPackageManager, cacheDependencyPath);
} }
const matchersPath = path.join(__dirname, '../..', '.github'); const matchersPath = path.join(__dirname, '../..', '.github');
@@ -117,3 +125,27 @@ function resolveVersionInput(): string {
return version; return version;
} }
export function getNameFromPackageManagerField(): string | undefined {
// Check packageManager field in package.json
const SUPPORTED_PACKAGE_MANAGERS = ['npm', 'yarn', 'pnpm'];
try {
const packageJson = JSON.parse(
fs.readFileSync(
path.join(process.env.GITHUB_WORKSPACE!, 'package.json'),
'utf-8'
)
);
const pm = packageJson.packageManager;
if (typeof pm === 'string') {
const regex = new RegExp(
`^(?:\\^)?(${SUPPORTED_PACKAGE_MANAGERS.join('|')})@`
);
const match = pm.match(regex);
return match ? match[1] : undefined;
}
return undefined;
} catch (err) {
return undefined;
}
}