@@ -29,10 +29,14 @@ import (
2929)
3030
3131type ImportMissingError struct {
32- Path string
33- Module module.Version
34- QueryErr error
35-
32+ Path string
33+ Module module.Version
34+ QueryErr error
35+ modContainingCWD module.Version
36+
37+ // modRoot is dependent on the value of ImportingMainModule and should be
38+ // kept in sync.
39+ modRoot string
3640 ImportingMainModule module.Version
3741
3842 // isStd indicates whether we would expect to find the package in the standard
@@ -82,8 +86,8 @@ func (e *ImportMissingError) Error() string {
8286 if e .QueryErr != nil {
8387 return fmt .Sprintf ("%s: %v" , message , e .QueryErr )
8488 }
85- if e .ImportingMainModule .Path != "" && e .ImportingMainModule != LoaderState . MainModules . ModContainingCWD () {
86- return fmt .Sprintf ("%s; to add it:\n \t cd %s\n \t go get %s" , message , LoaderState . MainModules . ModRoot ( e . ImportingMainModule ) , e .Path )
89+ if e .ImportingMainModule .Path != "" && e .ImportingMainModule != e . modContainingCWD {
90+ return fmt .Sprintf ("%s; to add it:\n \t cd %s\n \t go get %s" , message , e . modRoot , e .Path )
8791 }
8892 return fmt .Sprintf ("%s; to add it:\n \t go get %s" , message , e .Path )
8993 }
@@ -368,7 +372,10 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
368372 }
369373
370374 if len (mods ) == 0 {
371- return module.Version {}, "" , "" , nil , & ImportMissingError {Path : path }
375+ return module.Version {}, "" , "" , nil , & ImportMissingError {
376+ Path : path ,
377+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
378+ }
372379 }
373380
374381 return mods [0 ], roots [0 ], dirs [0 ], nil , nil
@@ -486,7 +493,12 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
486493 if ! HasModRoot (loaderstate ) {
487494 queryErr = NewNoMainModulesError (loaderstate )
488495 }
489- return module.Version {}, "" , "" , nil , & ImportMissingError {Path : path , QueryErr : queryErr , isStd : pathIsStd }
496+ return module.Version {}, "" , "" , nil , & ImportMissingError {
497+ Path : path ,
498+ QueryErr : queryErr ,
499+ isStd : pathIsStd ,
500+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
501+ }
490502 }
491503
492504 // So far we've checked the root dependencies.
@@ -558,7 +570,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
558570 return m , err
559571 } else if ok {
560572 if cfg .BuildMod == "readonly" {
561- return module.Version {}, & ImportMissingError {Path : path , replaced : m }
573+ return module.Version {}, & ImportMissingError {
574+ Path : path ,
575+ replaced : m ,
576+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
577+ }
562578 }
563579 return m , nil
564580 }
@@ -584,7 +600,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
584600 // QueryPattern cannot possibly find a module containing this package.
585601 //
586602 // Instead of trying QueryPattern, report an ImportMissingError immediately.
587- return module.Version {}, & ImportMissingError {Path : path , isStd : true }
603+ return module.Version {}, & ImportMissingError {
604+ Path : path ,
605+ isStd : true ,
606+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
607+ }
588608 }
589609
590610 if (cfg .BuildMod == "readonly" || cfg .BuildMod == "vendor" ) && ! allowMissingModuleImports {
@@ -599,7 +619,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
599619 } else if cfg .BuildModReason != "" {
600620 queryErr = fmt .Errorf ("import lookup disabled by -mod=%s\n \t (%s)" , cfg .BuildMod , cfg .BuildModReason )
601621 }
602- return module.Version {}, & ImportMissingError {Path : path , QueryErr : queryErr }
622+ return module.Version {}, & ImportMissingError {
623+ Path : path ,
624+ QueryErr : queryErr ,
625+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
626+ }
603627 }
604628
605629 // Look up module containing the package, for addition to the build list.
@@ -617,7 +641,11 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
617641 if errors .Is (err , fs .ErrNotExist ) {
618642 // Return "cannot find module providing package […]" instead of whatever
619643 // low-level error QueryPattern produced.
620- return module.Version {}, & ImportMissingError {Path : path , QueryErr : err }
644+ return module.Version {}, & ImportMissingError {
645+ Path : path ,
646+ QueryErr : err ,
647+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
648+ }
621649 } else {
622650 return module.Version {}, err
623651 }
@@ -645,6 +673,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
645673 Path : path ,
646674 Module : candidates [0 ].Mod ,
647675 newMissingVersion : candidate0MissingVersion ,
676+ modContainingCWD : loaderstate .MainModules .ModContainingCWD (),
648677 }
649678}
650679
0 commit comments