@@ -19,6 +19,7 @@ package main
19
19
import (
20
20
"archive/zip"
21
21
"bytes"
22
+ "context"
22
23
"encoding/json"
23
24
_ "expvar" // to serve /debug/vars
24
25
"flag"
@@ -44,6 +45,7 @@ import (
44
45
"golang.org/x/tools/godoc/vfs/gatefs"
45
46
"golang.org/x/tools/godoc/vfs/mapfs"
46
47
"golang.org/x/tools/godoc/vfs/zipfs"
48
+ "golang.org/x/tools/internal/gocommand"
47
49
"golang.org/x/xerrors"
48
50
)
49
51
@@ -210,28 +212,48 @@ func main() {
210
212
usage ()
211
213
}
212
214
213
- // Try to download dependencies that are not in the module cache in order to
214
- // to show their documentation.
215
- // This may fail if module downloading is disallowed (GOPROXY=off) or due to
216
- // limited connectivity, in which case we print errors to stderr and show
217
- // documentation only for packages that are available.
218
- fillModuleCache (os .Stderr , goModFile )
219
-
220
- // Determine modules in the build list.
221
- mods , err := buildList (goModFile )
215
+ // Detect whether to use vendor mode or not.
216
+ mainMod , vendorEnabled , err := gocommand .VendorEnabled (context .Background (), gocommand.Invocation {}, & gocommand.Runner {})
222
217
if err != nil {
223
- fmt .Fprintf (os .Stderr , "failed to determine the build list of the main module : %v" , err )
218
+ fmt .Fprintf (os .Stderr , "failed to determine if vendoring is enabled : %v" , err )
224
219
os .Exit (1 )
225
220
}
221
+ if vendorEnabled {
222
+ // Bind the root directory of the main module.
223
+ fs .Bind (path .Join ("/src" , mainMod .Path ), gatefs .New (vfs .OS (mainMod .Dir ), fsGate ), "/" , vfs .BindAfter )
224
+
225
+ // Bind the vendor directory.
226
+ //
227
+ // Note that in module mode, vendor directories in locations
228
+ // other than the main module's root directory are ignored.
229
+ // See https://golang.org/ref/mod#vendoring.
230
+ vendorDir := filepath .Join (mainMod .Dir , "vendor" )
231
+ fs .Bind ("/src" , gatefs .New (vfs .OS (vendorDir ), fsGate ), "/" , vfs .BindAfter )
232
+
233
+ } else {
234
+ // Try to download dependencies that are not in the module cache in order to
235
+ // to show their documentation.
236
+ // This may fail if module downloading is disallowed (GOPROXY=off) or due to
237
+ // limited connectivity, in which case we print errors to stderr and show
238
+ // documentation only for packages that are available.
239
+ fillModuleCache (os .Stderr , goModFile )
240
+
241
+ // Determine modules in the build list.
242
+ mods , err := buildList (goModFile )
243
+ if err != nil {
244
+ fmt .Fprintf (os .Stderr , "failed to determine the build list of the main module: %v" , err )
245
+ os .Exit (1 )
246
+ }
226
247
227
- // Bind module trees into Go root.
228
- for _ , m := range mods {
229
- if m .Dir == "" {
230
- // Module is not available in the module cache, skip it.
231
- continue
248
+ // Bind module trees into Go root.
249
+ for _ , m := range mods {
250
+ if m .Dir == "" {
251
+ // Module is not available in the module cache, skip it.
252
+ continue
253
+ }
254
+ dst := path .Join ("/src" , m .Path )
255
+ fs .Bind (dst , gatefs .New (vfs .OS (m .Dir ), fsGate ), "/" , vfs .BindAfter )
232
256
}
233
- dst := path .Join ("/src" , m .Path )
234
- fs .Bind (dst , gatefs .New (vfs .OS (m .Dir ), fsGate ), "/" , vfs .BindAfter )
235
257
}
236
258
} else {
237
259
fmt .Println ("using GOPATH mode" )
@@ -395,7 +417,7 @@ func goMod() (string, error) {
395
417
// with all dependencies of the main module in the current directory
396
418
// by invoking the go command. Module download logs are streamed to w.
397
419
// If there are any problems encountered, they are also written to w.
398
- // It should only be used when operating in module mode.
420
+ // It should only be used in module mode, when vendor mode isn't on .
399
421
//
400
422
// See https://golang.org/cmd/go/#hdr-Download_modules_to_local_cache.
401
423
func fillModuleCache (w io.Writer , goMod string ) {
@@ -436,9 +458,14 @@ func fillModuleCache(w io.Writer, goMod string) {
436
458
}
437
459
}
438
460
461
+ type mod struct {
462
+ Path string // Module path.
463
+ Dir string // Directory holding files for this module, if any.
464
+ }
465
+
439
466
// buildList determines the build list in the current directory
440
- // by invoking the go command. It should only be used when operating
441
- // in module mode.
467
+ // by invoking the go command. It should only be used in module mode,
468
+ // when vendor mode isn't on .
442
469
//
443
470
// See https://golang.org/cmd/go/#hdr-The_main_module_and_the_build_list.
444
471
func buildList (goMod string ) ([]mod , error ) {
@@ -467,11 +494,6 @@ func buildList(goMod string) ([]mod, error) {
467
494
return mods , nil
468
495
}
469
496
470
- type mod struct {
471
- Path string // Module path.
472
- Dir string // Directory holding files for this module, if any.
473
- }
474
-
475
497
// moduleFS is a vfs.FileSystem wrapper used when godoc is running
476
498
// in module mode. It's needed so that packages inside modules are
477
499
// considered to be third party.
0 commit comments