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): properly handle lock file maintenance for nested independent yarn workspaces #16451

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
228 changes: 228 additions & 0 deletions lib/modules/manager/npm/post-update/__snapshots__/yarn.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,234 @@ Array [
]
`;

exports[`modules/manager/npm/post-update/yarn performs lock file maintenance in subdirectory independent workspaces using yarn v1.22.0 1`] = `
Array [
Object {
"cmd": "yarn install --ignore-engines --ignore-platform --network-timeout 100000 --ignore-scripts",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "npx yarn-deduplicate --strategy fewer",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "yarn install --ignore-engines --ignore-platform --network-timeout 100000 --ignore-scripts",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "npx yarn-deduplicate --strategy highest",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "yarn install --ignore-engines --ignore-platform --network-timeout 100000 --ignore-scripts",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
]
`;

exports[`modules/manager/npm/post-update/yarn performs lock file maintenance in subdirectory independent workspaces using yarn v2.1.0 1`] = `
Array [
Object {
"cmd": "yarn install",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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_GLOBAL_CACHE": "1",
"YARN_ENABLE_IMMUTABLE_INSTALLS": "false",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
]
`;

exports[`modules/manager/npm/post-update/yarn performs lock file maintenance in subdirectory independent workspaces using yarn v2.2.0 1`] = `
Array [
Object {
"cmd": "yarn install",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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_GLOBAL_CACHE": "1",
"YARN_ENABLE_IMMUTABLE_INSTALLS": "false",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "yarn dedupe --strategy highest",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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_GLOBAL_CACHE": "1",
"YARN_ENABLE_IMMUTABLE_INSTALLS": "false",
"YARN_ENABLE_SCRIPTS": "0",
"YARN_HTTP_TIMEOUT": "100000",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
]
`;

exports[`modules/manager/npm/post-update/yarn performs lock file maintenance in subdirectory independent workspaces using yarn v3.0.0 1`] = `
Array [
Object {
"cmd": "yarn install --mode=update-lockfile",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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_GLOBAL_CACHE": "1",
"YARN_ENABLE_IMMUTABLE_INSTALLS": "false",
"YARN_HTTP_TIMEOUT": "100000",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
Object {
"cmd": "yarn dedupe --strategy highest --mode=update-lockfile",
"options": Object {
"cwd": "some-dir/sub_workspace",
"encoding": "utf-8",
"env": Object {
"CI": "true",
"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_GLOBAL_CACHE": "1",
"YARN_ENABLE_IMMUTABLE_INSTALLS": "false",
"YARN_HTTP_TIMEOUT": "100000",
},
"maxBuffer": 10485760,
"timeout": 900000,
},
},
]
`;

exports[`modules/manager/npm/post-update/yarn performs lock file maintenance using yarn v1.22.0 1`] = `
Array [
Object {
Expand Down
58 changes: 56 additions & 2 deletions lib/modules/manager/npm/post-update/yarn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,62 @@ describe('modules/manager/npm/post-update/yarn', () => {
{ isLockFileMaintenance: true },
]);
expect(fs.readFile).toHaveBeenCalledTimes(expectedFsCalls);
expect(fs.remove).toHaveBeenCalledTimes(1);
expect(res.lockFile).toBeNull();
expect(fs.remove).toHaveBeenCalledTimes(0);

// expected the lock file not to be deleted.
expect(res.lockFile).toBe('');
expect(fixSnapshots(execSnapshots)).toMatchSnapshot();
}
);

it.each([
['1.22.0', '^1.10.0', 2],
['2.1.0', '>= 2.0.0', 1],
['2.2.0', '2.2.0', 1],
['3.0.0', '3.0.0', 1],
])(
'performs lock file maintenance in subdirectory independent workspaces using yarn v%s',
async (yarnVersion, yarnCompatibility, expectedFsReadCalls) => {
Fixtures.mock(
{
'.yarnrc': null,
'package.json': JSON.stringify({ name: 'main-workspace' }),
'yarn.lock': 'main-workspace-lock-contents',
'sub_workspace/package.json': JSON.stringify({
name: 'sub-workspace',
}),
'sub_workspace/yarn.lock': 'sub-workspace-lock-contents',
},
'some-dir'
);
const execSnapshots = mockExecAll(exec, {
stdout: yarnVersion,
stderr: '',
});
const config = {
constraints: {
yarn: yarnCompatibility,
},
postUpdateOptions: ['yarnDedupeFewer', 'yarnDedupeHighest'],
};
const res = await yarnHelper.generateLockFile(
'some-dir/sub_workspace',
{},
config,
[{ isLockFileMaintenance: true }]
);
expect(fs.readFile).toHaveBeenCalledTimes(expectedFsReadCalls);
expect(fs.remove).toHaveBeenCalledTimes(0);

// Expect the lock file to be not deleted before `yarn install` is run.
// The lock file should exist but just be empty. This is necessary for
// subdirectory isolated workspaces to work with Yarn 2+.
expect(res.lockFile).toBe('');
expect(fs.outputFile).toHaveBeenCalledTimes(1);
expect(fs.outputFile).toHaveBeenCalledWith(
'some-dir/sub_workspace/yarn.lock',
''
);
expect(fixSnapshots(execSnapshots)).toMatchSnapshot();
}
);
Expand Down
12 changes: 9 additions & 3 deletions lib/modules/manager/npm/post-update/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
ToolConstraint,
} from '../../../../util/exec/types';
import {
deleteLocalFile,
localPathIsFile,
readLocalFile,
writeLocalFile,
Expand Down Expand Up @@ -251,12 +250,19 @@ export async function generateLockFile(
logger.debug(
`Removing ${lockFileName} first due to lock file maintenance upgrade`
);

// Note: Instead of just deleting the `yarn.lock` file, we just wipe it
// and keep an empty lock file. Deleting the lock file could result in different
// Yarn semantics. e.g. Yarn 2+ will error when `yarn install` is executed in
// a subdirectory which is not part of a Yarn workspace. Yarn suggests to create
// an empty lock file if a subdirectory should be treated as its own workspace.
// https://github.com/yarnpkg/berry/blob/20612e82d26ead5928cc27bf482bb8d62dde87d3/packages/yarnpkg-core/sources/Project.ts#L284.
try {
await deleteLocalFile(lockFileName);
await writeLocalFile(lockFileName, '');
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ err, lockFileName },
'Error removing yarn.lock for lock file maintenance'
'Error clearing `yarn.lock` for lock file maintenance'
);
}
}
Expand Down