Dependency services Fix applying when only a single pubspec has dependency (#4338)
diff --git a/lib/src/command/dependency_services.dart b/lib/src/command/dependency_services.dart
index 82065cc..ec8de3e 100644
--- a/lib/src/command/dependency_services.dart
+++ b/lib/src/command/dependency_services.dart
@@ -325,30 +325,36 @@
final targetVersion = p.version;
late final section = pubspec.dependencies[targetPackage] != null
? 'dependencies'
- : 'dev_dependencies';
- if (targetConstraint != null) {
- final packageConfig =
- pubspecEditor.parseAt([section, targetPackage]).value;
- if (packageConfig == null || packageConfig is String) {
- pubspecEditor
- .update([section, targetPackage], targetConstraint.toString());
- } else if (packageConfig is Map) {
- pubspecEditor.update(
- [section, targetPackage, 'version'],
- targetConstraint.toString(),
- );
- } else {
- fail(
- 'The dependency $targetPackage does not have a map or string as a description',
- );
- }
- } else if (targetVersion != null) {
- final constraint = _constraintOf(pubspec, targetPackage);
- if (constraint != null && !constraint.allows(targetVersion)) {
- pubspecEditor.update(
- [section, targetPackage],
- VersionConstraint.compatibleWith(targetVersion).toString(),
- );
+ : pubspec.devDependencies[targetPackage] != null
+ ? 'dev_dependencies'
+ : null;
+ if (section != null) {
+ if (targetConstraint != null) {
+ final packageConfig =
+ pubspecEditor.parseAt([section, targetPackage]).value;
+ if (packageConfig == null || packageConfig is String) {
+ pubspecEditor.update(
+ [section, targetPackage],
+ targetConstraint.toString(),
+ );
+ } else if (packageConfig is Map) {
+ pubspecEditor.update(
+ [section, targetPackage, 'version'],
+ targetConstraint.toString(),
+ );
+ } else {
+ fail(
+ 'The dependency $targetPackage does not have a map or string as a description',
+ );
+ }
+ } else if (targetVersion != null) {
+ final constraint = _constraintOf(pubspec, targetPackage);
+ if (constraint != null && !constraint.allows(targetVersion)) {
+ pubspecEditor.update(
+ [section, targetPackage],
+ VersionConstraint.compatibleWith(targetVersion).toString(),
+ );
+ }
}
}
updatedPubspecs[package.dir] = pubspecEditor;
diff --git a/test/dependency_services/dependency_services_test.dart b/test/dependency_services/dependency_services_test.dart
index b670876..028e8a8 100644
--- a/test/dependency_services/dependency_services_test.dart
+++ b/test/dependency_services/dependency_services_test.dart
@@ -644,6 +644,8 @@
..serve('foo', '2.2.3', deps: {'transitive': '^1.0.0'})
..serve('bar', '1.2.3')
..serve('bar', '2.2.3')
+ ..serve('only_a', '1.0.0')
+ ..serve('only_a', '2.0.0')
..serve('dev', '1.0.0')
..serve('dev', '2.0.0')
..serve('transitive', '1.0.0')
@@ -667,7 +669,7 @@
libPubspec(
'a',
'1.1.1',
- deps: {'bar': '>=1.2.0 <1.5.0'},
+ deps: {'bar': '>=1.2.0 <1.5.0', 'only_a': '^1.0.0'},
devDeps: {
'foo': '^1.2.0',
'dev': '^1.0.0',
@@ -699,7 +701,15 @@
await _listReportApply(
context,
- [_PackageVersion('foo', '2.2.3'), _PackageVersion('transitive', '1.0.0')],
+ [
+ _PackageVersion('foo', '2.2.3'),
+ _PackageVersion('transitive', '1.0.0'),
+ _PackageVersion(
+ 'only_a',
+ '2.0.0',
+ constraint: VersionConstraint.parse('^2.0.0'),
+ ),
+ ],
workspace: ['.', p.join('pkgs', 'a')],
reportAssertions: (report) {
expect(
diff --git a/test/testdata/goldens/dependency_services/dependency_services_test/can upgrade workspaces.txt b/test/testdata/goldens/dependency_services/dependency_services_test/can upgrade workspaces.txt
index cf9e847..ce21586 100644
--- a/test/testdata/goldens/dependency_services/dependency_services_test/can upgrade workspaces.txt
+++ b/test/testdata/goldens/dependency_services/dependency_services_test/can upgrade workspaces.txt
@@ -3,7 +3,7 @@
$ cat pubspec.yaml
{"name":"myapp","version":"1.2.3","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"foo":"^1.0.0","bar":"^1.0.0"},"environment":{"sdk":"^3.5.0"},"workspace":["pkgs/a"]}
$ cat pkgs/a/pubspec.yaml
-{"name":"a","version":"1.1.1","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"bar":">=1.2.0 <1.5.0"},"dev_dependencies":{"foo":"^1.2.0","dev":"^1.0.0"},"environment":{"sdk":"^3.5.0-0"},"resolution":"workspace"}
+{"name":"a","version":"1.1.1","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"bar":">=1.2.0 <1.5.0","only_a":"^1.0.0"},"dev_dependencies":{"foo":"^1.2.0","dev":"^1.0.0"},"environment":{"sdk":"^3.5.0-0"},"resolution":"workspace"}
$ cat pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
@@ -32,6 +32,14 @@
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
+ only_a:
+ dependency: transitive
+ description:
+ name: only_a
+ sha256: $SHA256
+ url: "http://localhost:$PORT"
+ source: hosted
+ version: "1.0.0"
sdks:
dart: ">=3.5.0 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
@@ -81,6 +89,20 @@
"sha256": "b2b7fc405959806aa1f31ac7e68752534f66f66a11a280d9878ecb6cd835f01c"
}
}
+ },
+ {
+ "name": "only_a",
+ "version": "1.0.0",
+ "kind": "direct",
+ "constraint": "^1.0.0",
+ "source": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "8846e47bf6bbb3a868043fb35f476e5dad2fcb45ee35439e5d1722e05bf6466a"
+ }
+ }
}
]
}
@@ -344,6 +366,78 @@
"previousSource": null
}
]
+ },
+ {
+ "name": "only_a",
+ "version": "1.0.0",
+ "kind": "direct",
+ "source": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "8846e47bf6bbb3a868043fb35f476e5dad2fcb45ee35439e5d1722e05bf6466a"
+ }
+ },
+ "latest": "2.0.0",
+ "constraint": "^1.0.0",
+ "compatible": [],
+ "singleBreaking": [
+ {
+ "name": "only_a",
+ "version": "2.0.0",
+ "kind": "direct",
+ "source": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "5070353618421ae8ee236208fedb6940e6203a2147d808109acf36f5b33102af"
+ }
+ },
+ "constraintBumped": "^2.0.0",
+ "constraintWidened": ">=1.0.0 <3.0.0",
+ "constraintBumpedIfNeeded": "^2.0.0",
+ "previousVersion": "1.0.0",
+ "previousConstraint": "^1.0.0",
+ "previousSource": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "8846e47bf6bbb3a868043fb35f476e5dad2fcb45ee35439e5d1722e05bf6466a"
+ }
+ }
+ }
+ ],
+ "multiBreaking": [
+ {
+ "name": "only_a",
+ "version": "2.0.0",
+ "kind": "direct",
+ "source": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "5070353618421ae8ee236208fedb6940e6203a2147d808109acf36f5b33102af"
+ }
+ },
+ "constraintBumped": "^2.0.0",
+ "constraintWidened": ">=1.0.0 <3.0.0",
+ "constraintBumpedIfNeeded": "^2.0.0",
+ "previousVersion": "1.0.0",
+ "previousConstraint": "^1.0.0",
+ "previousSource": {
+ "type": "hosted",
+ "description": {
+ "name": "only_a",
+ "url": "http://localhost:$PORT",
+ "sha256": "8846e47bf6bbb3a868043fb35f476e5dad2fcb45ee35439e5d1722e05bf6466a"
+ }
+ }
+ }
+ ]
}
]
}
@@ -351,7 +445,7 @@
-------------------------------- END OF OUTPUT ---------------------------------
## Section apply
-$ echo '{"dependencyChanges":[{"name":"foo","version":"2.2.3"},{"name":"transitive","version":"1.0.0"}]}' | dependency_services apply
+$ echo '{"dependencyChanges":[{"name":"foo","version":"2.2.3"},{"name":"transitive","version":"1.0.0"},{"name":"only_a","version":"2.0.0","constraint":"^2.0.0"}]}' | dependency_services apply
{"dependencies":[]}
-------------------------------- END OF OUTPUT ---------------------------------
@@ -359,7 +453,7 @@
$ cat pubspec.yaml
{"name":"myapp","version":"1.2.3","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"foo":^2.2.3,"bar":"^1.0.0"},"environment":{"sdk":"^3.5.0"},"workspace":["pkgs/a"]}
$ cat pkgs/a/pubspec.yaml
-{"name":"a","version":"1.1.1","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"bar":">=1.2.0 <1.5.0"},"dev_dependencies":{"foo":^2.2.3,"dev":"^1.0.0"},"environment":{"sdk":"^3.5.0-0"},"resolution":"workspace"}
+{"name":"a","version":"1.1.1","homepage":"https://pub.dev","description":"A package, I guess.","dependencies":{"bar":">=1.2.0 <1.5.0","only_a":^2.0.0},"dev_dependencies":{"foo":^2.2.3,"dev":"^1.0.0"},"environment":{"sdk":"^3.5.0-0"},"resolution":"workspace"}
$ cat pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
@@ -388,6 +482,14 @@
url: "http://localhost:$PORT"
source: hosted
version: "2.2.3"
+ only_a:
+ dependency: transitive
+ description:
+ name: only_a
+ sha256: $SHA256
+ url: "http://localhost:$PORT"
+ source: hosted
+ version: "2.0.0"
transitive:
dependency: transitive
description: