@@ -15,7 +15,6 @@ import (
15
15
"path/filepath"
16
16
"sort"
17
17
"strings"
18
- "time"
19
18
20
19
"cmd/go/internal/cfg"
21
20
"cmd/go/internal/fsys"
@@ -42,6 +41,10 @@ type ImportMissingError struct {
42
41
// modules.
43
42
isStd bool
44
43
44
+ // replaced the highest replaced version of the module where the replacement
45
+ // contains the package. replaced is only set if the replacement is unused.
46
+ replaced module.Version
47
+
45
48
// newMissingVersion is set to a newer version of Module if one is present
46
49
// in the build list. When set, we can't automatically upgrade.
47
50
newMissingVersion string
@@ -59,6 +62,14 @@ func (e *ImportMissingError) Error() string {
59
62
return "cannot find module providing package " + e .Path
60
63
}
61
64
65
+ if e .replaced .Path != "" {
66
+ suggestArg := e .replaced .Path
67
+ if ! modfetch .IsZeroPseudoVersion (e .replaced .Version ) {
68
+ suggestArg = e .replaced .String ()
69
+ }
70
+ return fmt .Sprintf ("module %s provides package %s and is replaced but not required; try 'go get -d %s' to add it" , e .replaced .Path , e .Path , suggestArg )
71
+ }
72
+
62
73
suggestion := ""
63
74
if ! HasModRoot () {
64
75
suggestion = ": working directory is not part of a module"
@@ -284,37 +295,6 @@ func importFromBuildList(ctx context.Context, path string) (m module.Version, di
284
295
// Unlike QueryPattern, queryImport prefers to add a replaced version of a
285
296
// module *before* checking the proxies for a version to add.
286
297
func queryImport (ctx context.Context , path string ) (module.Version , error ) {
287
- pathIsStd := search .IsStandardImportPath (path )
288
-
289
- if cfg .BuildMod == "readonly" {
290
- if pathIsStd {
291
- // If the package would be in the standard library and none of the
292
- // available replacement modules could concievably provide it, report it
293
- // as a missing standard-library package instead of complaining that
294
- // module lookups are disabled.
295
- maybeReplaced := false
296
- if index != nil {
297
- for p := range index .highestReplaced {
298
- if maybeInModule (path , p ) {
299
- maybeReplaced = true
300
- break
301
- }
302
- }
303
- }
304
- if ! maybeReplaced {
305
- return module.Version {}, & ImportMissingError {Path : path , isStd : true }
306
- }
307
- }
308
-
309
- var queryErr error
310
- if cfg .BuildModExplicit {
311
- queryErr = fmt .Errorf ("import lookup disabled by -mod=%s" , cfg .BuildMod )
312
- } else if cfg .BuildModReason != "" {
313
- queryErr = fmt .Errorf ("import lookup disabled by -mod=%s\n \t (%s)" , cfg .BuildMod , cfg .BuildModReason )
314
- }
315
- return module.Version {}, & ImportMissingError {Path : path , QueryErr : queryErr }
316
- }
317
-
318
298
// To avoid spurious remote fetches, try the latest replacement for each
319
299
// module (golang.org/issue/26241).
320
300
if index != nil {
@@ -330,9 +310,9 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
330
310
// used from within some other module, the user will be able to upgrade
331
311
// the requirement to any real version they choose.
332
312
if _ , pathMajor , ok := module .SplitPathVersion (mp ); ok && len (pathMajor ) > 0 {
333
- mv = modfetch .PseudoVersion (pathMajor [1 :], "" , time. Time {}, "000000000000" )
313
+ mv = modfetch .ZeroPseudoVersion (pathMajor [1 :])
334
314
} else {
335
- mv = modfetch .PseudoVersion ("v0" , "" , time. Time {}, "000000000000 " )
315
+ mv = modfetch .ZeroPseudoVersion ("v0" )
336
316
}
337
317
}
338
318
mods = append (mods , module.Version {Path : mp , Version : mv })
@@ -347,18 +327,23 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
347
327
needSum := true
348
328
root , isLocal , err := fetch (ctx , m , needSum )
349
329
if err != nil {
350
- // Report fetch error as above.
330
+ if sumErr := (* sumMissingError )(nil ); errors .As (err , & sumErr ) {
331
+ return module.Version {}, & ImportMissingSumError {importPath : path }
332
+ }
351
333
return module.Version {}, err
352
334
}
353
335
if _ , ok , err := dirInModule (path , m .Path , root , isLocal ); err != nil {
354
336
return m , err
355
337
} else if ok {
338
+ if cfg .BuildMod == "readonly" {
339
+ return module.Version {}, & ImportMissingError {Path : path , replaced : m }
340
+ }
356
341
return m , nil
357
342
}
358
343
}
359
344
if len (mods ) > 0 && module .CheckPath (path ) != nil {
360
345
// The package path is not valid to fetch remotely,
361
- // so it can only exist if in a replaced module,
346
+ // so it can only exist in a replaced module,
362
347
// and we know from the above loop that it is not.
363
348
return module.Version {}, & PackageNotInModuleError {
364
349
Mod : mods [0 ],
@@ -369,7 +354,7 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
369
354
}
370
355
}
371
356
372
- if pathIsStd {
357
+ if search . IsStandardImportPath ( path ) {
373
358
// This package isn't in the standard library, isn't in any module already
374
359
// in the build list, and isn't in any other module that the user has
375
360
// shimmed in via a "replace" directive.
@@ -380,6 +365,19 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
380
365
return module.Version {}, & ImportMissingError {Path : path , isStd : true }
381
366
}
382
367
368
+ if cfg .BuildMod == "readonly" {
369
+ // In readonly mode, we can't write go.mod, so we shouldn't try to look up
370
+ // the module. If readonly mode was enabled explicitly, include that in
371
+ // the error message.
372
+ var queryErr error
373
+ if cfg .BuildModExplicit {
374
+ queryErr = fmt .Errorf ("import lookup disabled by -mod=%s" , cfg .BuildMod )
375
+ } else if cfg .BuildModReason != "" {
376
+ queryErr = fmt .Errorf ("import lookup disabled by -mod=%s\n \t (%s)" , cfg .BuildMod , cfg .BuildModReason )
377
+ }
378
+ return module.Version {}, & ImportMissingError {Path : path , QueryErr : queryErr }
379
+ }
380
+
383
381
// Look up module containing the package, for addition to the build list.
384
382
// Goal is to determine the module, download it to dir,
385
383
// and return m, dir, ImpportMissingError.
0 commit comments