@@ -492,9 +492,59 @@ func (b *typeCheckBatch) importPackage(ctx context.Context, mp *metadata.Package
492
492
return bug .Errorf ("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904) (using GOPACKAGESDRIVER)" ,
493
493
pkg .Name (), item .Name , id , item .Path )
494
494
} else {
495
- return bug .Errorf ("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904)" ,
496
- pkg .Name (), item .Name , id , item .Path )
497
-
495
+ // There's a package in the export data with the same path as the
496
+ // imported package, but a different name.
497
+ //
498
+ // This is observed to occur (very frequently!) in telemetry, yet
499
+ // we don't yet have a plausible explanation: any self import or
500
+ // circular import should have resulted in a broken import, which
501
+ // can't be referenced by export data. (Any type qualified by the
502
+ // broken import name will be invalid.)
503
+ //
504
+ // However, there are some mechanisms that could potentially be
505
+ // involved:
506
+ // 1. go/types will synthesize package names based on the import
507
+ // path for fake packages (but as mentioned above, I don't think
508
+ // these can be referenced by export data.)
509
+ // 2. Test variants have the same path as non-test variant. Could
510
+ // that somehow be involved? (I don't see how, particularly using
511
+ // the go list driver, but nevertheless it's worth considering.)
512
+ // 3. Command-line arguments and main packages may have special
513
+ // handling that we don't fully understand.
514
+ // Try to sort these potential causes into unique stacks, as well
515
+ // as a few other pathological scenarios.
516
+ report := func () error {
517
+ return bug .Errorf ("internal error: package name is %q, want %q (id=%q, path=%q) (see issue #60904)" ,
518
+ pkg .Name (), item .Name , id , item .Path )
519
+ }
520
+ impliedName := ""
521
+ if i := strings .LastIndex (item .Path , "/" ); i >= 0 {
522
+ impliedName = item .Path [i + 1 :]
523
+ }
524
+ switch {
525
+ case pkg .Name () == "" :
526
+ return report ()
527
+ case item .Name == "" :
528
+ return report ()
529
+ case metadata .IsCommandLineArguments (mp .ID ):
530
+ return report ()
531
+ case mp .ForTest != "" :
532
+ return report ()
533
+ case len (mp .CompiledGoFiles ) == 0 :
534
+ return report ()
535
+ case len (mp .Errors ) > 0 :
536
+ return report ()
537
+ case impliedName != "" && impliedName != string (mp .Name ):
538
+ return report ()
539
+ case len (mp .CompiledGoFiles ) != len (mp .GoFiles ):
540
+ return report ()
541
+ case mp .Module == nil :
542
+ return report ()
543
+ case mp .Name == "main" :
544
+ return report ()
545
+ default :
546
+ return report ()
547
+ }
498
548
}
499
549
}
500
550
} else {
0 commit comments