Compare commits

...

3 Commits

Author SHA1 Message Date
Satishchoudhary94
1b5409412c
Merge 844d397646 into efcb663fc6 2026-02-21 01:48:51 +09:00
Marco Ippolito
efcb663fc6
fix: remove hardcoded bearer (#1467) 2026-02-19 11:58:14 -06:00
Satishchoudhary94
844d397646 fix(#1027): Improve yarn v4+ corepack support with better error handling
This change improves the action's handling of yarn v4+ which requires corepack.

Changes:
- Add enableCorepackIfSupported() helper function to automatically enable corepack
- Update yarn getCacheFolderPath to call corepack enable before checking yarn version
- Detect corepack-related errors and provide clear, actionable error messages
- Users can either enable corepack before the action or disable caching

The error message now clearly explains:
- The requirement for corepack with yarn v4+
- How to enable corepack: 'corepack enable'
- Alternative: disable caching with 'package-manager-cache: false'
- Link to GitHub issue for more context

Fixes #1027
Related: https://github.com/actions/setup-node/issues/1027
2026-01-18 14:11:36 +00:00
5 changed files with 119 additions and 10 deletions

View File

@ -71586,7 +71586,24 @@ exports.supportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async (projectDir) => {
const yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion;
try {
yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
}
catch (err) {
// Check if this is a corepack error message
const errorMsg = err.message;
if (errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')) {
throw new Error(`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`);
}
throw err;
}
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
const stdOut = yarnVersion.startsWith('1.')
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
@ -71598,6 +71615,24 @@ exports.supportedPackageManagers = {
}
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async () => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
}
catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
const getCommandOutput = async (toolCommand, cwd) => {
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
if (exitCode) {

39
dist/setup/index.js vendored
View File

@ -81224,7 +81224,24 @@ exports.supportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async (projectDir) => {
const yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion;
try {
yarnVersion = await (0, exports.getCommandOutputNotEmpty)(`yarn --version`, 'Could not retrieve version of yarn', projectDir);
}
catch (err) {
// Check if this is a corepack error message
const errorMsg = err.message;
if (errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')) {
throw new Error(`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`);
}
throw err;
}
core.debug(`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir || ''}")`);
const stdOut = yarnVersion.startsWith('1.')
? await (0, exports.getCommandOutput)('yarn cache dir', projectDir)
@ -81236,6 +81253,24 @@ exports.supportedPackageManagers = {
}
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async () => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
}
catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
const getCommandOutput = async (toolCommand, cwd) => {
let { stdout, stderr, exitCode } = await exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true, ...(cwd && { cwd }) });
if (exitCode) {
@ -81660,7 +81695,7 @@ class BaseDistribution {
const dataUrl = `${initialUrl}/index.json`;
const headers = {};
if (this.nodeInfo.mirrorToken) {
headers['Authorization'] = `Bearer ${this.nodeInfo.mirrorToken}`;
headers['Authorization'] = this.nodeInfo.mirrorToken;
}
const response = await this.httpClient.getJson(dataUrl, headers);
return response.result || [];

View File

@ -470,7 +470,7 @@ Please refer to the [Ensuring workflow access to your package - Configuring a pa
It is possible to use a private mirror hosting Node.js binaries. This mirror must be a full mirror of the official Node.js distribution.
The mirror URL can be set using the `mirror` input.
It is possible to specify a token to authenticate with the mirror using the `mirror-token` input.
The token will be passed as a bearer token in the `Authorization` header.
The token will be passed in the `Authorization` header.
```yaml
- uses: actions/setup-node@v6

View File

@ -40,11 +40,32 @@ export const supportedPackageManagers: SupportedPackageManagers = {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async projectDir => {
const yarnVersion = await getCommandOutputNotEmpty(
`yarn --version`,
'Could not retrieve version of yarn',
projectDir
);
// Try to enable corepack first if available
// This helps with yarn v2+ which requires corepack
await enableCorepackIfSupported();
let yarnVersion: string;
try {
yarnVersion = await getCommandOutputNotEmpty(
`yarn --version`,
'Could not retrieve version of yarn',
projectDir
);
} catch (err) {
// Check if this is a corepack error message
const errorMsg = (err as Error).message;
if (
errorMsg.includes('packageManager') &&
errorMsg.includes('Corepack')
) {
throw new Error(
`Yarn v4+ requires corepack to be enabled. Please run 'corepack enable' before using ` +
`actions/setup-node with yarn, or disable caching with 'package-manager-cache: false'. ` +
`See: https://github.com/actions/setup-node/issues/1027 for more information.`
);
}
throw err;
}
core.debug(
`Consumed yarn version is ${yarnVersion} (working dir: "${
@ -66,6 +87,24 @@ export const supportedPackageManagers: SupportedPackageManagers = {
}
};
/**
* Tries to enable corepack for Node.js versions that support it (16.9+)
* This helps with yarn v2+ which requires corepack
* See: https://github.com/actions/setup-node/issues/1027
*/
const enableCorepackIfSupported = async (): Promise<void> => {
try {
await exec.exec('corepack', ['enable'], {
ignoreReturnCode: true,
silent: true
});
core.debug('Corepack enabled successfully');
} catch {
// Corepack not available or failed silently
core.debug('Corepack not available on this system');
}
};
export const getCommandOutput = async (
toolCommand: string,
cwd?: string

View File

@ -103,7 +103,7 @@ export default abstract class BaseDistribution {
const headers = {};
if (this.nodeInfo.mirrorToken) {
headers['Authorization'] = `Bearer ${this.nodeInfo.mirrorToken}`;
headers['Authorization'] = this.nodeInfo.mirrorToken;
}
const response = await this.httpClient.getJson<INodeVersion[]>(