| 1 | // Copyright 2014 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 buildutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/" |
| 11 | "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 13 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 14 | "io/ioutilscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/" |
| 15 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 16 | "pathscip-go gomod github.com/golang/go/src go1.25.0 path/" |
| 17 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 18 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 19 | ) |
| 20 | |
| 21 | // ParseFile behaves like parser.ParseFile, |
| 22 | // but uses the build context's file system interface, if any. |
| 23 | // |
| 24 | // If file is not absolute (as defined by IsAbsPath), the (dir, file) |
| 25 | // components are joined using JoinPath; dir must be absolute. |
| 26 | // |
| 27 | // The displayPath function, if provided, is used to transform the |
| 28 | // filename that will be attached to the ASTs. |
| 29 | // |
| 30 | // TODO(adonovan): call this from go/loader.parseFiles when the tree thaws. |
| 31 | func ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ParseFile().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, displayPath func(string) string, dir string, file string, mode parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.Modescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/Mode#) (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, error) { |
| 32 | if !IsAbsPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/IsAbsPath().(ctxt, file) { |
| 33 | file = JoinPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/JoinPath().(ctxt, dir, file) |
| 34 | } |
| 35 | rd, err := OpenFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/OpenFile().(ctxt, file) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | defer rd.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() // ignore error |
| 40 | if displayPath != nil { |
| 41 | file = displayPath(file) |
| 42 | } |
| 43 | return parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, file, rd, mode) |
| 44 | } |
| 45 | |
| 46 | // ContainingPackage returns the package containing filename. |
| 47 | // |
| 48 | // If filename is not absolute, it is interpreted relative to working directory dir. |
| 49 | // All I/O is via the build context's file system interface, if any. |
| 50 | // |
| 51 | // The '...Files []string' fields of the resulting build.Package are not |
| 52 | // populated (build.FindOnly mode). |
| 53 | func ContainingPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ContainingPackage().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, dir, filename string) (*buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Package#, error) { |
| 54 | if !IsAbsPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/IsAbsPath().(ctxt, filename) { |
| 55 | filename = JoinPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/JoinPath().(ctxt, dir, filename) |
| 56 | } |
| 57 | |
| 58 | // We must not assume the file tree uses |
| 59 | // "/" always, |
| 60 | // `\` always, |
| 61 | // or os.PathSeparator (which varies by platform), |
| 62 | // but to make any progress, we are forced to assume that |
| 63 | // paths will not use `\` unless the PathSeparator |
| 64 | // is also `\`, thus we can rely on filepath.ToSlash for some sanity. |
| 65 | |
| 66 | dirSlash := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Dirscip-go gomod github.com/golang/go/src go1.25.0 path/Dir().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(filename)) + "/" |
| 67 | |
| 68 | // We assume that no source root (GOPATH[i] or GOROOT) contains any other. |
| 69 | for _, srcdir := range ctxt.SrcDirsscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#SrcDirs().() { |
| 70 | srcdirSlash := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(srcdir) + "/" |
| 71 | if importPath, ok := HasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/HasSubdir().(ctxt, srcdirSlash, dirSlash); ok { |
| 72 | return ctxt.Importscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#Import().(importPath, dir, buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.FindOnlyscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/FindOnly.) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("can't find package containing %s", filename) |
| 77 | } |
| 78 | |
| 79 | // -- Effective methods of file system interface ------------------------- |
| 80 | |
| 81 | // (go/build.Context defines these as methods, but does not export them.) |
| 82 | |
| 83 | // HasSubdir calls ctxt.HasSubdir (if not nil) or else uses |
| 84 | // the local file system to answer the question. |
| 85 | func HasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/HasSubdir().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, root, dir string) (rel string, ok bool) { |
| 86 | if f := ctxt.HasSubdirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#HasSubdir.; f != nil { |
| 87 | return f(root, dir) |
| 88 | } |
| 89 | |
| 90 | // Try using paths we received. |
| 91 | if rel, ok = hasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/hasSubdir().(root, dir); ok { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | // Try expanding symlinks and comparing |
| 96 | // expanded against unexpanded and |
| 97 | // expanded against expanded. |
| 98 | rootSym, _ := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.EvalSymlinksscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/EvalSymlinks().(root) |
| 99 | dirSym, _ := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.EvalSymlinksscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/EvalSymlinks().(dir) |
| 100 | |
| 101 | if rel, ok = hasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/hasSubdir().(rootSym, dir); ok { |
| 102 | return |
| 103 | } |
| 104 | if rel, ok = hasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/hasSubdir().(root, dirSym); ok { |
| 105 | return |
| 106 | } |
| 107 | return hasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/hasSubdir().(rootSym, dirSym) |
| 108 | } |
| 109 | |
| 110 | func hasSubdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/hasSubdir().(root, dir string) (rel string, ok bool) { |
| 111 | const sep = string(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Separator.) |
| 112 | root = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(root) |
| 113 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(root, sep) { |
| 114 | root += sep |
| 115 | } |
| 116 | |
| 117 | dir = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(dir) |
| 118 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(dir, root) { |
| 119 | return "", false |
| 120 | } |
| 121 | |
| 122 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(dir[len(root):]), true |
| 123 | } |
| 124 | |
| 125 | // FileExists returns true if the specified file exists, |
| 126 | // using the build context's file system interface. |
| 127 | func FileExistsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/FileExists().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path string) bool { |
| 128 | if ctxt.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#OpenFile. != nil { |
| 129 | r, err := ctxt.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#OpenFile.(path) |
| 130 | if err != nil { |
| 131 | return false |
| 132 | } |
| 133 | r.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() // ignore error |
| 134 | return true |
| 135 | } |
| 136 | _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(path) |
| 137 | return err == nil |
| 138 | } |
| 139 | |
| 140 | // OpenFile behaves like os.Open, |
| 141 | // but uses the build context's file system interface, if any. |
| 142 | func OpenFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/OpenFile().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path string) (ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadCloser#, error) { |
| 143 | if ctxt.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#OpenFile. != nil { |
| 144 | return ctxt.OpenFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#OpenFile.(path) |
| 145 | } |
| 146 | return osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(path) |
| 147 | } |
| 148 | |
| 149 | // IsAbsPath behaves like filepath.IsAbs, |
| 150 | // but uses the build context's file system interface, if any. |
| 151 | func IsAbsPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/IsAbsPath().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path string) bool { |
| 152 | if ctxt.IsAbsPathscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#IsAbsPath. != nil { |
| 153 | return ctxt.IsAbsPathscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#IsAbsPath.(path) |
| 154 | } |
| 155 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/IsAbs().(path) |
| 156 | } |
| 157 | |
| 158 | // JoinPath behaves like filepath.Join, |
| 159 | // but uses the build context's file system interface, if any. |
| 160 | func JoinPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/JoinPath().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path ...string) string { |
| 161 | if ctxt.JoinPathscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#JoinPath. != nil { |
| 162 | return ctxt.JoinPathscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#JoinPath.(path...) |
| 163 | } |
| 164 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(path...) |
| 165 | } |
| 166 | |
| 167 | // IsDir behaves like os.Stat plus IsDir, |
| 168 | // but uses the build context's file system interface, if any. |
| 169 | func IsDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/IsDir().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path string) bool { |
| 170 | if ctxt.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#IsDir. != nil { |
| 171 | return ctxt.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#IsDir.(path) |
| 172 | } |
| 173 | fi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(path) |
| 174 | return err == nil && fi.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() |
| 175 | } |
| 176 | |
| 177 | // ReadDir behaves like ioutil.ReadDir, |
| 178 | // but uses the build context's file system interface, if any. |
| 179 | func ReadDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/ReadDir().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, path string) ([]osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileInfoscip-go gomod github.com/golang/go/src go1.25.0 os/FileInfo#, error) { |
| 180 | if ctxt.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#ReadDir. != nil { |
| 181 | return ctxt.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#ReadDir.(path) |
| 182 | } |
| 183 | return ioutilscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/ReadDir().(path) |
| 184 | } |
| 185 | |
| 186 | // SplitPathList behaves like filepath.SplitList, |
| 187 | // but uses the build context's file system interface, if any. |
| 188 | func SplitPathListscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/SplitPathList().(ctxt *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, s string) []string { |
| 189 | if ctxt.SplitPathListscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#SplitPathList. != nil { |
| 190 | return ctxt.SplitPathListscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#SplitPathList.(s) |
| 191 | } |
| 192 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.SplitListscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/SplitList().(s) |
| 193 | } |
| 194 | |
| 195 | // sameFile returns true if x and y have the same basename and denote |
| 196 | // the same file. |
| 197 | func sameFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/buildutil`/sameFile().(x, y string) bool { |
| 198 | if pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 path/Clean().(x) == pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 path/Clean().(y) { |
| 199 | return true |
| 200 | } |
| 201 | if filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(x) == filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(y) { // (optimisation) |
| 202 | if xi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(x); err == nil { |
| 203 | if yi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(y); err == nil { |
| 204 | return osscip-go gomod github.com/golang/go/src go1.25.0 os/.SameFilescip-go gomod github.com/golang/go/src go1.25.0 os/SameFile().(xi, yi) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | return false |
| 209 | } |