Skip to content

Commit e0e119e

Browse files
g-planebestander
authored andcommitted
Do not save lockfile when no dependencies (#3395)
1 parent 4d5dcc6 commit e0e119e

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

__tests__/commands/install/lockfiles.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ test.concurrent("writes new lockfile if existing one isn't satisfied", async ():
4444
});
4545
});
4646

47-
test.concurrent('writes a lockfile even when there are no dependencies', (): Promise<void> => {
48-
// https://github.com/yarnpkg/yarn/issues/679
47+
test.concurrent("doesn't write a lockfile when there are no dependencies", (): Promise<void> => {
4948
return runInstall({}, 'install-without-dependencies', async config => {
5049
const lockfileExists = await fs.exists(path.join(config.cwd, 'yarn.lock'));
5150
const installedDepFiles = await fs.walk(path.join(config.cwd, 'node_modules'));
5251

53-
expect(lockfileExists).toEqual(true);
54-
// 1 for integrity file (located in node_modules)
55-
expect(installedDepFiles).toHaveLength(1);
52+
expect(lockfileExists).toEqual(false);
53+
expect(installedDepFiles).toHaveLength(0);
5654
});
5755
});
5856

__tests__/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ test.concurrent('should install if no args', async () => {
208208

209209
test.concurrent('should install if first arg looks like a flag', async () => {
210210
const stdout = await execCommand('--json', [], 'run-add', true);
211-
expect(stdout[stdout.length - 1]).toEqual('{"type":"success","data":"Saved lockfile."}');
211+
expect(stdout[stdout.length - 1]).toEqual('{"type":"info","data":"Lockfile not saved, no dependencies."}');
212212
});
213213

214214
test.concurrent('should not output JSON activity/progress if given --no-progress option', async () => {

src/cli/commands/install.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,12 @@ export class Install {
503503
}
504504

505505
// fin!
506-
await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout);
506+
// The second condition is to make sure lockfile can be updated when running `remove` command.
507+
if (topLevelPatterns.length || (await fs.exists(path.join(this.config.cwd, constants.LOCKFILE_FILENAME)))) {
508+
await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout);
509+
} else {
510+
this.reporter.info(this.reporter.lang('notSavedLockfileNoDependencies'));
511+
}
507512
this.maybeOutputUpdate();
508513
this.config.requestManager.clearCache();
509514
return flattenedTopLevelPatterns;

src/cli/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const args = process.argv.slice(2, doubleDashIndex === -1 ? process.argv.length
2929
const endArgs = doubleDashIndex === -1 ? [] : process.argv.slice(doubleDashIndex + 1, process.argv.length);
3030

3131
// set global options
32-
commander.version(version);
32+
commander.version(version, '--version');
3333
commander.usage('[command] [flags]');
3434
commander.option('--verbose', 'output verbose messages on internal operations');
3535
commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache');

src/reporters/lang/en.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ const messages = {
182182
foundWarnings: 'Found $0 warnings.',
183183
foundErrors: 'Found $0 errors.',
184184

185+
notSavedLockfileNoDependencies: 'Lockfile not saved, no dependencies.',
186+
185187
savedLockfile: 'Saved lockfile.',
186188
noRequiredLockfile: 'No lockfile in this directory. Run `yarn install` to generate one.',
187189
noLockfileFound: 'No lockfile found.',

0 commit comments

Comments
 (0)