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

fix(npm): yarnDedupeHighest for Yarn 2 #7200

Merged
merged 2 commits into from
Sep 8, 2020
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
2 changes: 1 addition & 1 deletion docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ If enabled Renovate will pin docker images by means of their sha256 digest and n
- `gomodTidy`: Run `go mod tidy` after Go module updates
- `npmDedupe`: Run `npm dedupe` after `package-lock.json` updates
- `yarnDedupeFewer`: Run `yarn-deduplicate --strategy fewer` after `yarn.lock` updates
- `yarnDedupeHighest`: Run `yarn-deduplicate --strategy highest` after `yarn.lock` updates
- `yarnDedupeHighest`: Run `yarn-deduplicate --strategy highest` (`yarn dedupe --strategy highest` for Yarn >=2.2.0) after `yarn.lock` updates
Comment on lines 1104 to +1105
Copy link
Member

Choose a reason for hiding this comment

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

Should we differentiate more bewteen yarn v1 and v2 here?


## postUpgradeTasks

Expand Down
86 changes: 86 additions & 0 deletions lib/manager/npm/post-update/__snapshots__/yarn.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,49 @@ Array [
]
`;

exports[`manager/npm/post-update/yarn generates lock files using yarn v2.2.0 1`] = `
Array [
Object {
"cmd": "yarn install",
"options": Object {
"cwd": "some-dir",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"timeout": 900000,
},
},
Object {
"cmd": "yarn dedupe --strategy highest",
"options": Object {
"cwd": "some-dir",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"timeout": 900000,
},
},
]
`;

exports[`manager/npm/post-update/yarn performs lock file maintenance using yarn v1.22.0 1`] = `
Array [
Object {
Expand Down Expand Up @@ -250,6 +293,49 @@ Array [
]
`;

exports[`manager/npm/post-update/yarn performs lock file maintenance using yarn v2.2.0 1`] = `
Array [
Object {
"cmd": "yarn install",
"options": Object {
"cwd": "some-dir",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"timeout": 900000,
},
},
Object {
"cmd": "yarn dedupe --strategy highest",
"options": Object {
"cwd": "some-dir",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"timeout": 900000,
},
},
]
`;

exports[`manager/npm/post-update/yarn performs lock file updates and full install using yarn v1.22.0 1`] = `
Array [
Object {
Expand Down
18 changes: 10 additions & 8 deletions lib/manager/npm/post-update/yarn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ describe(getName(__filename), () => {
env.getChildProcessEnv.mockReturnValue(envMock.basic);
});
it.each([
['1.22.0', 2],
['2.1.0', 1],
['1.22.0', '^1.10.0', 2],
['2.1.0', '>= 2.0.0', 1],
['2.2.0', '2.2.0', 1],
])(
'generates lock files using yarn v%s',
async (yarnVersion, expectedFsCalls) => {
async (yarnVersion, yarnCompatibility, expectedFsCalls) => {
const execSnapshots = mockExecAll(exec, {
stdout: yarnVersion,
stderr: '',
Expand All @@ -49,7 +50,7 @@ describe(getName(__filename), () => {
const config = {
dockerMapDotfiles: true,
compatibility: {
yarn: yarnVersion === '1.22.0' ? '^1.10.0' : '>= 2.0.0',
yarn: yarnCompatibility,
},
postUpdateOptions: ['yarnDedupeFewer', 'yarnDedupeHighest'],
};
Expand Down Expand Up @@ -103,11 +104,12 @@ describe(getName(__filename), () => {
}
);
it.each([
['1.22.0', 2],
['2.1.0', 1],
['1.22.0', '^1.10.0', 2],
['2.1.0', '>= 2.0.0', 1],
['2.2.0', '2.2.0', 1],
])(
'performs lock file maintenance using yarn v%s',
async (yarnVersion, expectedFsCalls) => {
async (yarnVersion, yarnCompatibility, expectedFsCalls) => {
const execSnapshots = mockExecAll(exec, {
stdout: yarnVersion,
stderr: '',
Expand All @@ -123,7 +125,7 @@ describe(getName(__filename), () => {
const config = {
dockerMapDotfiles: true,
compatibility: {
yarn: yarnVersion === '1.22.0' ? '^1.10.0' : '>= 2.0.0',
yarn: yarnCompatibility,
},
postUpdateOptions: ['yarnDedupeFewer', 'yarnDedupeHighest'],
};
Expand Down
27 changes: 18 additions & 9 deletions lib/manager/npm/post-update/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import is from '@sindresorhus/is';
import { minVersion, validRange } from 'semver';
import { gte, minVersion, validRange } from 'semver';
import { quote } from 'shlex';
import { join } from 'upath';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../../constants/error-messages';
Expand Down Expand Up @@ -48,12 +48,14 @@ export async function generateLockFile(
let lockFile = null;
try {
const yarnCompatibility = config.compatibility?.yarn;
const isValidYarnRange = validRange(yarnCompatibility);
const isYarn1 =
!isValidYarnRange || minVersion(yarnCompatibility).major === 1;
const minYarnVersion =
validRange(yarnCompatibility) && minVersion(yarnCompatibility);
const isYarn1 = !minYarnVersion || minYarnVersion.major === 1;
const isYarnDedupeAvailable =
minYarnVersion && gte(minYarnVersion, '2.2.0');

let installYarn = 'npm i -g yarn';
if (isYarn1 && isValidYarnRange) {
if (isYarn1 && minYarnVersion) {
installYarn += `@${quote(yarnCompatibility)}`;
}

Expand Down Expand Up @@ -133,11 +135,18 @@ export async function generateLockFile(
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
}
if (isYarn1 && config.postUpdateOptions?.includes('yarnDedupeHighest')) {
if (
(isYarn1 || isYarnDedupeAvailable) &&
config.postUpdateOptions?.includes('yarnDedupeHighest')
) {
logger.debug('Performing yarn dedupe highest');
commands.push('npx yarn-deduplicate --strategy highest');
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
if (isYarn1) {
commands.push('npx yarn-deduplicate --strategy highest');
// Run yarn again in case any changes are necessary
commands.push(`yarn install ${cmdOptions}`.trim());
} else {
commands.push('yarn dedupe --strategy highest');
}
}

if (upgrades.find((upgrade) => upgrade.isLockFileMaintenance)) {
Expand Down