Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when multiple versions specified #122

Merged
merged 6 commits into from
May 6, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/install-pnpm/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,40 @@ async function readTarget(opts: {
readonly standalone: boolean
}) {
const { version, packageJsonFile, standalone } = opts
const { GITHUB_WORKSPACE } = process.env

if (version) return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}`
let packageManager

if (GITHUB_WORKSPACE) {
try {
({ packageManager } = JSON.parse(await readFile(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8')))
} catch (error) {
// Swallow error if package.json doesn't exist in root
if (error.code !== 'ENOENT') throw error
}
}

if (version) {
if (
typeof packageManager === 'string' &&
packageManager.replace('pnpm@', '') !== version
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't enough, there can be a hash at end. see recent corepack release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry for any breakage! Can you provide an example of the hash + delimiter? I can open a new PR.

Copy link
Contributor Author

@karlhorky karlhorky May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this behavior in Corepack just today, the packageManager specifier looks like this:

   "packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"

Looks like it was released in corepack@0.26.0 in the following PR:

Copy link
Contributor Author

@karlhorky karlhorky May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viceice but maybe this is actually desirable behavior, since:

  • the version input on pnpm/action-setup supports a specifier with a Corepack hash (pnpm/action-setup uses pnpm.cjs install, see output of pnpm install below)
  • technically it could be argued that 9.1.1 is not the same as 9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195, so stripping the hash is maybe not wanted
$ pnpm install pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195

Packages: +1
+
Progress: resolved 1, reused 0, downloaded 1, added 1, done

dependencies:
+ pnpm 9.1.1

Done in 1.3s

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but does it also work with the standalone version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it, yep:

$ pnpm install @pnpm/exe@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195
Packages: +2
++
Downloading @pnpm/macos-arm64@9.1.1: 22.84 MB/22.84 MB, done
Progress: resolved 7, reused 1, downloaded 2, added 2, done
node_modules/.pnpm/@pnpm+exe@9.1.1/node_modules/@pnpm/exe: Running preinstall script, done in 46ms

dependencies:
+ @pnpm/exe 9.1.1

Done in 8.6s

) {
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}`
}

const { GITHUB_WORKSPACE } = process.env
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,
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:
Expand All @@ -64,7 +86,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@')
}

Expand Down