diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 10e20a3..ae889e6 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -41,10 +41,25 @@ async function readTarget(opts: { readonly standalone: boolean }) { const { version, packageJsonFile, standalone } = opts - - if (version) return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` - const { GITHUB_WORKSPACE } = process.env + + let packageManager; + + if (GITHUB_WORKSPACE) { + ({ packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8'))) + } + + if (version) { + if (typeof packageManager === 'string') { + throw new Error(`Multiple versions of pnpm specified: + - version ${version} in the GitHub Action config with the key "version" + - version ${packageManager} in the package.json with the key "packageManager" +Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`) + } + + return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` + } + if (!GITHUB_WORKSPACE) { throw new Error(`No workspace is found. If you're intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file, @@ -52,7 +67,6 @@ please run the actions/checkout before pnpm/action-setup. Otherwise, please specify the pnpm version in the action configuration.`) } - const { packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8')) if (typeof packageManager !== 'string') { throw new Error(`No pnpm version is specified. Please specify it by one of the following ways: @@ -64,7 +78,7 @@ Please specify it by one of the following ways: throw new Error('Invalid packageManager field in package.json') } - if(standalone){ + if (standalone){ return packageManager.replace('pnpm@', '@pnpm/exe@') }