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

Enforce resolution-mode=highest for pnpm versions 8.0.0 to 8.6.* #966

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ If you include `useYarn: true` in your `ember-try` config, all npm scenarios wil

If you include `usePnpm: true` in your `ember-try` config, all npm scenarios will use `pnpm` for install with the `--no-lockfile` options. At cleanup, your dependencies will be restored to their prior state.

> ⚠ pnpm versions from 8.0.0 to 8.6.x have the default value of [resolution-mode](https://pnpm.io/npmrc#resolution-mode) setting changed to `lowest-direct`. This violates `ember-try` expectations as `resolution-mode` is expected to be `highest`, like in `npm` and `pnpm` versions < 8.0.0 and >= 8.7.0.
>
> If you run into this issue, we recommend to upgrade pnpm to latest version. If you are unable to upgrade, you can set `resolution-mode = highest` in the `.npmrc` file.

##### A note on npm scenarios with lockfiles

Expand Down
47 changes: 46 additions & 1 deletion lib/dependency-manager-adapters/pnpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const fs = require('fs-extra');
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:pnpm');
const Backup = require('../utils/backup');
const semverLt = require('semver/functions/lt');
const semverGte = require('semver/functions/gte');

const PACKAGE_JSON = 'package.json';
const PNPM_LOCKFILE = 'pnpm-lock.yaml';
Expand All @@ -21,12 +23,12 @@ module.exports = CoreObject.extend({
},

async setup() {
await this._throwOnResolutionMode();
await this.backup.addFiles([PACKAGE_JSON, PNPM_LOCKFILE]);
},

async changeToDependencySet(depSet) {
await this.applyDependencySet(depSet);

await this._install(depSet);

let deps = Object.assign({}, depSet.dependencies, depSet.devDependencies);
Expand Down Expand Up @@ -63,6 +65,49 @@ module.exports = CoreObject.extend({
}
},

/**
* pnpm versions 8.0.0 through 8.6.* have the `resolution-mode` setting inverted to
* `lowest-direct`, which breaks ember-try. This method throws a helpful error in the following
* case: pnpm version is within dangerous range and `pnpm config get resolution-mode` reports an
* empty value.
*
* @returns Promise<void>
*/
async _throwOnResolutionMode() {
let version = await this._getPnpmVersion();
let resolutionMode = await this._getResolutionMode();

if (this._isResolutionModeWrong(version, resolutionMode)) {
throw new Error(
'You are using an old version of pnpm that uses wrong resolution mode that violates ember-try expectations. Please either upgrade pnpm or set `resolution-mode` to `highest` in `.npmrc`.'
);
}
},

async _getPnpmVersion() {
let result = await this.run('pnpm', ['--version'], { cwd: this.cwd, stdio: 'pipe' });
return result.stdout.split('\n')[0];
},

async _getResolutionMode() {
let result = await this.run('pnpm', ['config', 'get', 'resolution-mode'], {
cwd: this.cwd,
stdio: 'pipe',
});

return result.stdout.split('\n')[0];
},

_isResolutionModeWrong(versionStr, resolutionMode) {
// The `resolution-mode` is not set explicitly, and the current pnpm version makes it default
// to `lowest-direct`
if (!resolutionMode.length && semverGte(versionStr, '8.0.0') && semverLt(versionStr, '8.7.0')) {
return true;
}

return false;
},

async _install(depSet) {
let mgrOptions = this.managerOptions || [];

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"devDependencies": {
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"codecov": "^3.8.3",
"ember-cli": "~3.22.0",
"eslint": "^7.31.0",
Expand All @@ -59,6 +60,7 @@
"release-it": "^14.11.6",
"release-it-lerna-changelog": "^3.1.0",
"rsvp": "^4.7.0",
"sinon": "^15.2.0",
"tmp-sync": "^1.1.0"
},
"engines": {
Expand Down
Loading