]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/modload/search.go
cmd: ignore the directory named go.mod
[gostls13.git] / src / cmd / go / internal / modload / search.go
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package modload
6
7 import (
8         "fmt"
9         "os"
10         "path/filepath"
11         "strings"
12
13         "cmd/go/internal/base"
14         "cmd/go/internal/cfg"
15         "cmd/go/internal/imports"
16         "cmd/go/internal/module"
17         "cmd/go/internal/search"
18 )
19
20 // matchPackages returns a list of packages in the list of modules
21 // matching the pattern. Package loading assumes the given set of tags.
22 func matchPackages(pattern string, tags map[string]bool, useStd bool, modules []module.Version) []string {
23         match := func(string) bool { return true }
24         treeCanMatch := func(string) bool { return true }
25         if !search.IsMetaPackage(pattern) {
26                 match = search.MatchPattern(pattern)
27                 treeCanMatch = search.TreeCanMatchPattern(pattern)
28         }
29
30         have := map[string]bool{
31                 "builtin": true, // ignore pseudo-package that exists only for documentation
32         }
33         if !cfg.BuildContext.CgoEnabled {
34                 have["runtime/cgo"] = true // ignore during walk
35         }
36         var pkgs []string
37
38         walkPkgs := func(root, importPathRoot string, includeVendor bool) {
39                 root = filepath.Clean(root)
40                 filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
41                         if err != nil {
42                                 return nil
43                         }
44
45                         // Don't use GOROOT/src but do walk down into it.
46                         if path == root && importPathRoot == "" {
47                                 return nil
48                         }
49
50                         want := true
51                         // Avoid .foo, _foo, and testdata directory trees.
52                         _, elem := filepath.Split(path)
53                         if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
54                                 want = false
55                         }
56
57                         name := importPathRoot + filepath.ToSlash(path[len(root):])
58                         if importPathRoot == "" {
59                                 name = name[1:] // cut leading slash
60                         }
61                         if !treeCanMatch(name) {
62                                 want = false
63                         }
64
65                         if !fi.IsDir() {
66                                 if fi.Mode()&os.ModeSymlink != 0 && want {
67                                         if target, err := os.Stat(path); err == nil && target.IsDir() {
68                                                 fmt.Fprintf(os.Stderr, "warning: ignoring symlink %s\n", path)
69                                         }
70                                 }
71                                 return nil
72                         }
73
74                         if !want {
75                                 return filepath.SkipDir
76                         }
77                         // Stop at module boundaries.
78                         if path != root {
79                                 if fi, err := os.Stat(filepath.Join(path, "go.mod")); err == nil && !fi.IsDir() {
80                                         return filepath.SkipDir
81                                 }
82                         }
83
84                         if !have[name] {
85                                 have[name] = true
86                                 if match(name) {
87                                         if _, _, err := scanDir(path, tags); err != imports.ErrNoGo {
88                                                 pkgs = append(pkgs, name)
89                                         }
90                                 }
91                         }
92
93                         if elem == "vendor" && !includeVendor {
94                                 return filepath.SkipDir
95                         }
96                         return nil
97                 })
98         }
99
100         if useStd {
101                 walkPkgs(cfg.GOROOTsrc, "", true)
102                 if treeCanMatch("cmd") {
103                         walkPkgs(filepath.Join(cfg.GOROOTsrc, "cmd"), "cmd", true)
104                 }
105         }
106
107         if cfg.BuildMod == "vendor" {
108                 if HasModRoot() {
109                         walkPkgs(ModRoot(), targetPrefix, false)
110                         walkPkgs(filepath.Join(ModRoot(), "vendor"), "", false)
111                 }
112                 return pkgs
113         }
114
115         for _, mod := range modules {
116                 if !treeCanMatch(mod.Path) {
117                         continue
118                 }
119                 var root, modPrefix string
120                 if mod == Target {
121                         if !HasModRoot() {
122                                 continue // If there is no main module, we can't search in it.
123                         }
124                         root = ModRoot()
125                         modPrefix = targetPrefix
126                 } else {
127                         var err error
128                         root, _, err = fetch(mod)
129                         if err != nil {
130                                 base.Errorf("go: %v", err)
131                                 continue
132                         }
133                         modPrefix = mod.Path
134                 }
135                 walkPkgs(root, modPrefix, false)
136         }
137
138         return pkgs
139 }