Allow `dart pub add` with existing package, update constraint instead of adding (#3570)
diff --git a/lib/src/command/add.dart b/lib/src/command/add.dart index 78b1e5d..15b64a4 100644 --- a/lib/src/command/add.dart +++ b/lib/src/command/add.dart
@@ -213,11 +213,10 @@ final range = package.ref.withConstraint(package.constraint ?? VersionConstraint.any); if (isDev) { - /// TODO(walnut): Change the error message once pub upgrade --bump is - /// released if (devDependencyNames.contains(name)) { - dataError('"$name" is already in "dev_dependencies". ' - 'Use "pub upgrade $name" to upgrade to a later version!'); + log.message('"$name" is already in "dev_dependencies". ' + 'Will try to update the constraint.'); + devDependencies.removeWhere((element) => element.name == name); } /// If package is originally in dependencies and we wish to add it to @@ -232,11 +231,10 @@ devDependencies.add(range); } else { - /// TODO(walnut): Change the error message once pub upgrade --bump is - /// released if (dependencyNames.contains(name)) { - dataError('"$name" is already in "dependencies". ' - 'Use "pub upgrade $name" to upgrade to a later version!'); + log.message( + '"$name" is already in "dependencies". Will try to update the constraint.'); + dependencies.removeWhere((element) => element.name == name); } /// If package is originally in dev_dependencies and we wish to add it to @@ -245,7 +243,7 @@ if (devDependencyNames.contains(name)) { log.message('"$name" was found in dev_dependencies. ' 'Removing "$name" and adding it to dependencies instead.'); - devDependencies = devDependencies.where((d) => d.name != name).toList(); + devDependencies.removeWhere((element) => element.name == name); } dependencies.add(range);
diff --git a/test/add/common/add_test.dart b/test/add/common/add_test.dart index f1e76a7..25f9d8d 100644 --- a/test/add/common/add_test.dart +++ b/test/add/common/add_test.dart
@@ -185,7 +185,7 @@ await d.appDir({'foo': '1.2.3'}).validate(); }); - group('warns user to use pub upgrade if package exists', () { + group('notifies user about existing constraint', () { test('if package is added without a version constraint', () async { await servePackages() ..serve('foo', '1.2.3') @@ -194,13 +194,13 @@ await d.appDir({'foo': '1.2.2'}).create(); await pubAdd( - args: ['foo'], - exitCode: exit_codes.DATA, - error: - contains('"foo" is already in "dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + args: ['foo'], + output: contains( + '"foo" is already in "dependencies". Will try to update the constraint.', + ), + ); - await d.appDir({'foo': '1.2.2'}).validate(); + await d.appDir({'foo': '^1.2.3'}).validate(); }); test('if package is added with a specific version constraint', () async { @@ -211,13 +211,13 @@ await d.appDir({'foo': '1.2.2'}).create(); await pubAdd( - args: ['foo:1.2.3'], - exitCode: exit_codes.DATA, - error: - contains('"foo" is already in "dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + args: ['foo:1.2.3'], + output: contains( + '"foo" is already in "dependencies". Will try to update the constraint.', + ), + ); - await d.appDir({'foo': '1.2.2'}).validate(); + await d.appDir({'foo': '1.2.3'}).validate(); }); test('if package is added with a version constraint range', () async { @@ -229,12 +229,10 @@ await pubAdd( args: ['foo:>=1.2.2'], - exitCode: exit_codes.DATA, - error: - contains('"foo" is already in "dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + output: contains( + '"foo" is already in "dependencies". Will try to update the constraint.')); - await d.appDir({'foo': '1.2.2'}).validate(); + await d.appDir({'foo': '>=1.2.2'}).validate(); }); }); @@ -517,7 +515,7 @@ ]).validate(); }); - group('warns user to use pub upgrade if package exists', () { + group('notifies user if package exists', () { test('if package is added without a version constraint', () async { await servePackages() ..serve('foo', '1.2.3') @@ -532,15 +530,13 @@ await pubAdd( args: ['foo', '--dev'], - exitCode: exit_codes.DATA, - error: contains( - '"foo" is already in "dev_dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + output: contains( + '"foo" is already in "dev_dependencies". Will try to update the constraint.')); await d.dir(appPath, [ d.pubspec({ 'name': 'myapp', - 'dev_dependencies': {'foo': '1.2.2'} + 'dev_dependencies': {'foo': '^1.2.3'} }) ]).validate(); }); @@ -559,15 +555,13 @@ await pubAdd( args: ['foo:1.2.3', '--dev'], - exitCode: exit_codes.DATA, - error: contains( - '"foo" is already in "dev_dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + output: contains( + '"foo" is already in "dev_dependencies". Will try to update the constraint.')); await d.dir(appPath, [ d.pubspec({ 'name': 'myapp', - 'dev_dependencies': {'foo': '1.2.2'} + 'dev_dependencies': {'foo': '1.2.3'} }) ]).validate(); }); @@ -586,15 +580,13 @@ await pubAdd( args: ['foo:>=1.2.2', '--dev'], - exitCode: exit_codes.DATA, - error: contains( - '"foo" is already in "dev_dependencies". Use "pub upgrade ' - 'foo" to upgrade to a later version!')); + output: contains( + '"foo" is already in "dev_dependencies". Will try to update the constraint.')); await d.dir(appPath, [ d.pubspec({ 'name': 'myapp', - 'dev_dependencies': {'foo': '1.2.2'} + 'dev_dependencies': {'foo': '>=1.2.2'} }) ]).validate(); });