diff --git a/dist/index.js b/dist/index.js index 56b4c4d..d554a1e 100644 Binary files a/dist/index.js and b/dist/index.js differ diff --git a/src/install-pnpm/run.ts b/src/install-pnpm/run.ts index 10e20a3..76a33e1 100644 --- a/src/install-pnpm/run.ts +++ b/src/install-pnpm/run.ts @@ -1,8 +1,10 @@ import { addPath, exportVariable } from '@actions/core' import { spawn } from 'child_process' -import { rm, writeFile, readFile, mkdir } from 'fs/promises' +import { rm, writeFile, mkdir } from 'fs/promises' +import { readFileSync } from 'fs' import path from 'path' import { execPath } from 'process' +import util from 'util' import { Inputs } from '../inputs' export async function runSelfInstaller(inputs: Inputs): Promise { @@ -41,10 +43,33 @@ 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) { + try { + ({ packageManager } = JSON.parse(readFileSync(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8'))) + } catch (error: unknown) { + // Swallow error if package.json doesn't exist in root + if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error + } + } + + if (version) { + if ( + typeof packageManager === 'string' && + packageManager.replace('pnpm@', '') !== version + ) { + 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 +77,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 +88,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@') }