Skip to content

Commit

Permalink
Throw on incorrect pnpm resolution-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lolmaus committed Sep 26, 2023
1 parent 188b123 commit 86328ea
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 172 deletions.
78 changes: 21 additions & 57 deletions lib/dependency-manager-adapters/pnpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const semverGte = require('semver/functions/gte');

const PACKAGE_JSON = 'package.json';
const PNPM_LOCKFILE = 'pnpm-lock.yaml';
const NPMRC_CONFIG = '.npmrc';

module.exports = CoreObject.extend({
// This still needs to be `npm` because we're still reading the dependencies
Expand All @@ -26,8 +25,8 @@ module.exports = CoreObject.extend({
},

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

async changeToDependencySet(depSet) {
Expand All @@ -54,7 +53,6 @@ module.exports = CoreObject.extend({
await this.backup.restoreFiles([PACKAGE_JSON, PNPM_LOCKFILE]);
await this.backup.cleanUp();
await this._install();
await this._revertNpmRc();
} catch (e) {
console.log('Error cleaning up scenario:', e); // eslint-disable-line no-console
}
Expand Down Expand Up @@ -85,72 +83,38 @@ module.exports = CoreObject.extend({
* arguments
* @returns Promise<void>
*/
async _updateNpmRc(version) {
async _throwOnResolutionMode(version) {
if (!version) {
version = await this._getPnpmVersion();
}

if (!this._doesPnpmRequireResolutionModeFix(version)) {
return;
}

let npmrcPath = path.join(this.cwd, NPMRC_CONFIG);

if (fs.existsSync(npmrcPath)) {
await this.backup.addFile(NPMRC_CONFIG);

await fs.appendFileSync(npmrcPath, '\nresolution-mode = highest\n');
} else {
fs.writeFileSync(npmrcPath, 'resolution-mode = highest\n');
if (await this._isResolutionModeWrong(version)) {
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`.'
);
}
},

/**
* pnpm versions 8.0.0 through 8.6.* have the `resolution-mode` setting inverted to
* `lowest-direct`, which breaks ember-try. This method conditionally adds the necessary config to
* .npmrc to fix this.
*
* It covers the following cases:
* - pnpm version is out of dangerious range or cannot be retrieved — do not do anything
* - pnpm version is within dangerous range and the backup does not exist — delete the .npmrc
* - pnpm version is within dangerous range and the backup exists — rename the backup to .npmrc,
* overwriting the current .npmrc
*
* @param {undefined | string} version — this is only used in testing. Call this fucntion without
* arguments
* @returns Promise<void>
*/
async _revertNpmRc(version) {
if (!version) {
version = await this._getPnpmVersion();
}

if (!this._doesPnpmRequireResolutionModeFix(version)) {
return;
}

let npmrcPath = path.join(this.cwd, NPMRC_CONFIG);
async _getPnpmVersion(command = 'pnpm --version') {
let result = await exec(command, { cwd: this.cwd });
return result.stdout.split('\n')[0];
},

if (this.backup.hasFile(NPMRC_CONFIG)) {
await this.backup.restoreFile(NPMRC_CONFIG);
} else {
if (fs.existsSync(npmrcPath)) {
fs.removeSync(npmrcPath);
}
}
async _getResolutionMode() {
let result = await exec('pnpm config get resolution-mode', { cwd: this.cwd });
return result.stdout.split('\n')[0];
},

async _getPnpmVersion(command = 'pnpm --version') {
try {
let result = await exec(command);
return result.stdout.split('\n')[0];
} catch (error) {
return null;
async _isResolutionModeWrong(versionStr = this._getPnpmVersion()) {
if (!versionStr) {
return false;
}
},

_doesPnpmRequireResolutionModeFix(versionStr) {
return versionStr ? semverGte(versionStr, '8.0.0') && semverLt(versionStr, '8.7.0') : false;
return (
semverGte(versionStr, '8.0.0') &&
semverLt(versionStr, '8.7.0') &&
(await this._getResolutionMode()) !== 'highest'
);
},

async _install(depSet) {
Expand Down
1 change: 1 addition & 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 Down
Loading

0 comments on commit 86328ea

Please sign in to comment.