@@ -199,8 +199,6 @@ class MakeDepsPathBasedCommand extends PackageCommand {
199
199
.where (
200
200
(String packageName) => localDependencies.containsKey (packageName))
201
201
.toList ();
202
- // Sort the combined list to avoid sort_pub_dependencies lint violations.
203
- packagesToOverride.sort ();
204
202
205
203
if (packagesToOverride.isEmpty) {
206
204
return false ;
@@ -219,34 +217,60 @@ class MakeDepsPathBasedCommand extends PackageCommand {
219
217
final YamlEditor editablePubspec = YamlEditor (pubspecContents);
220
218
final YamlNode root = editablePubspec.parseAt (< String > []);
221
219
const String dependencyOverridesKey = 'dependency_overrides' ;
222
- // Ensure that there's a `dependencyOverridesKey` entry to update.
223
- if ((root as YamlMap )[dependencyOverridesKey] == null ) {
224
- editablePubspec.update (< String > [dependencyOverridesKey], YamlMap ());
225
- }
226
- for (final String packageName in packagesToOverride) {
227
- // Find the relative path from the common base to the local package.
228
- final List <String > repoRelativePathComponents = path.split (path
229
- .relative (localDependencies[packageName]! .path, from: repoRootPath));
230
- editablePubspec.update (< String > [
231
- dependencyOverridesKey,
232
- packageName
233
- ], < String , String > {
234
- 'path' : p.posix.joinAll (< String > [
220
+
221
+ // Add in any existing overrides, then sort the combined list to avoid
222
+ // sort_pub_dependencies lint violations.
223
+ final YamlMap ? existingOverrides =
224
+ (root as YamlMap )[dependencyOverridesKey] as YamlMap ? ;
225
+ final List <String > allDependencyOverrides = < String > {
226
+ ...existingOverrides? .keys.cast <String >() ?? < String > [],
227
+ ...packagesToOverride,
228
+ }.toList ();
229
+ allDependencyOverrides.sort ();
230
+
231
+ // Build a fresh overrides section, to ensure that new entries are sorted
232
+ // with any existing entries. This does mean comments will be removed, but
233
+ // since this command's changes will always be reverted anyway, that
234
+ // shouldn't be an issue, whereas lint violations will cause analysis
235
+ // failures.
236
+ // This uses string concatenation rather than YamlMap because the latter
237
+ // doesn't guarantee any particular output order for its entries.
238
+ final List <String > newOverrideLines = < String > [];
239
+ for (final String packageName in allDependencyOverrides) {
240
+ final String overrideValue;
241
+ if (packagesToOverride.contains (packageName)) {
242
+ // Create a new override.
243
+
244
+ // Find the relative path from the common base to the local package.
245
+ final List <String > repoRelativePathComponents = path.split (
246
+ path.relative (localDependencies[packageName]! .path,
247
+ from: repoRootPath));
248
+ final String pathValue = p.posix.joinAll (< String > [
235
249
...relativeBasePathComponents,
236
250
...repoRelativePathComponents,
237
- ])
238
- });
251
+ ]);
252
+ overrideValue = '{path: $pathValue }' ;
253
+ } else {
254
+ // Re-use the pre-existing override.
255
+ overrideValue = existingOverrides! [packageName].toString ();
256
+ }
257
+ newOverrideLines.add (' $packageName : $overrideValue ' );
239
258
}
240
-
241
- // Add the warning if it's not already there.
259
+ // Create an empty section as a placeholder to replace.
260
+ editablePubspec. update ( < String > [dependencyOverridesKey], YamlMap ());
242
261
String newContent = editablePubspec.toString ();
243
- if (! newContent.contains (_dependencyOverrideWarningComment)) {
244
- newContent = newContent.replaceFirst ('$dependencyOverridesKey :' , '''
245
262
246
- $_dependencyOverrideWarningComment
247
- $dependencyOverridesKey :
263
+ // Add the warning if it's not already there.
264
+ final String warningComment =
265
+ newContent.contains (_dependencyOverrideWarningComment)
266
+ ? ''
267
+ : '$_dependencyOverrideWarningComment \n ' ;
268
+ // Replace the placeholder with the new section.
269
+ newContent =
270
+ newContent.replaceFirst (RegExp ('$dependencyOverridesKey :\\ s*{}\\ n' ), '''
271
+ $warningComment $dependencyOverridesKey :
272
+ ${newOverrideLines .join ('\n ' )}
248
273
''' );
249
- }
250
274
251
275
// Write the new pubspec.
252
276
package.pubspecFile.writeAsStringSync (newContent);
0 commit comments