@@ -414,9 +414,16 @@ func (p *pass) fix() ([]*ImportFix, bool) {
414
414
})
415
415
}
416
416
}
417
-
417
+ // Collecting fixes involved map iteration, so sort for stability. See
418
+ // golang/go#59976.
419
+ sortFixes (fixes )
420
+
421
+ // collect selected fixes in a separate slice, so that it can be sorted
422
+ // separately. Note that these fixes must occur after fixes to existing
423
+ // imports. TODO(rfindley): figure out why.
424
+ var selectedFixes []* ImportFix
418
425
for _ , imp := range selected {
419
- fixes = append (fixes , & ImportFix {
426
+ selectedFixes = append (selectedFixes , & ImportFix {
420
427
StmtInfo : ImportInfo {
421
428
Name : p .importSpecName (imp ),
422
429
ImportPath : imp .ImportPath ,
@@ -425,8 +432,25 @@ func (p *pass) fix() ([]*ImportFix, bool) {
425
432
FixType : AddImport ,
426
433
})
427
434
}
435
+ sortFixes (selectedFixes )
436
+
437
+ return append (fixes , selectedFixes ... ), true
438
+ }
428
439
429
- return fixes , true
440
+ func sortFixes (fixes []* ImportFix ) {
441
+ sort .Slice (fixes , func (i , j int ) bool {
442
+ fi , fj := fixes [i ], fixes [j ]
443
+ if fi .StmtInfo .ImportPath != fj .StmtInfo .ImportPath {
444
+ return fi .StmtInfo .ImportPath < fj .StmtInfo .ImportPath
445
+ }
446
+ if fi .StmtInfo .Name != fj .StmtInfo .Name {
447
+ return fi .StmtInfo .Name < fj .StmtInfo .Name
448
+ }
449
+ if fi .IdentName != fj .IdentName {
450
+ return fi .IdentName < fj .IdentName
451
+ }
452
+ return fi .FixType < fj .FixType
453
+ })
430
454
}
431
455
432
456
// importSpecName gets the import name of imp in the import spec.
0 commit comments