| 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 gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/ |
| 6 | |
| 7 | import ( |
| 8 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 9 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 10 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 11 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 12 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 15 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 16 | ) |
| 17 | |
| 18 | func TestSymlinkTraversalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/TestSymlinkTraversal().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 19 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 20 | |
| 21 | gopath := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().() |
| 22 | |
| 23 | if err := mapToDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/mapToDir().(gopath, map[string]string{ |
| 24 | "a/b/c": "LINK:../../a/d", |
| 25 | "a/b/pkg/pkg.go": "package pkg", |
| 26 | "a/d/e": "LINK:../../a/b", |
| 27 | "a/d/pkg/pkg.go": "package pkg", |
| 28 | "a/f/loop": "LINK:../f", |
| 29 | "a/f/pkg/pkg.go": "package pkg", |
| 30 | "a/g/pkg/pkg.go": "LINK:../../f/pkg/pkg.go", |
| 31 | "a/self": "LINK:.", |
| 32 | }); err != nil { |
| 33 | switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. { |
| 34 | case "windows", "plan9": |
| 35 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("skipping symlink-requiring test on %s", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS.) |
| 36 | } |
| 37 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 38 | } |
| 39 | |
| 40 | pkgc := make(chan []string, 1) |
| 41 | pkgc <- nil |
| 42 | add := func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) { |
| 43 | rel, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(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().(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path., "src"), dir) |
| 44 | if err != nil { |
| 45 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err) |
| 46 | } |
| 47 | pkgc <- append(<-pkgc, 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().(rel)) |
| 48 | } |
| 49 | |
| 50 | Walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Walk().([]Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{{Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.: gopath, Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type.: RootGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOPATH.}}, add, Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#{Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Logf.: t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().}) |
| 51 | |
| 52 | pkgs := <-pkgc |
| 53 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(pkgs) |
| 54 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Found packages:\n\t%s", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(pkgs, "\n\t")) |
| 55 | |
| 56 | got := make(map[string]bool, len(pkgs)) |
| 57 | for _, pkg := range pkgs { |
| 58 | got[pkg] = true |
| 59 | } |
| 60 | tests := []struct { |
| 61 | path string |
| 62 | want bool |
| 63 | why string |
| 64 | }{ |
| 65 | { |
| 66 | path: "a/b/pkg", |
| 67 | want: true, |
| 68 | why: "found via regular directories", |
| 69 | }, |
| 70 | { |
| 71 | path: "a/b/c/pkg", |
| 72 | want: true, |
| 73 | why: "found via non-cyclic dir link", |
| 74 | }, |
| 75 | { |
| 76 | path: "a/b/c/e/pkg", |
| 77 | want: true, |
| 78 | why: "found via two non-cyclic dir links", |
| 79 | }, |
| 80 | { |
| 81 | path: "a/d/e/c/pkg", |
| 82 | want: true, |
| 83 | why: "found via two non-cyclic dir links", |
| 84 | }, |
| 85 | { |
| 86 | path: "a/f/loop/pkg", |
| 87 | want: true, |
| 88 | why: "found via a single parent-dir link", |
| 89 | }, |
| 90 | { |
| 91 | path: "a/f/loop/loop/pkg", |
| 92 | want: false, |
| 93 | why: "would follow loop symlink twice", |
| 94 | }, |
| 95 | { |
| 96 | path: "a/self/b/pkg", |
| 97 | want: true, |
| 98 | why: "follows self-link once", |
| 99 | }, |
| 100 | { |
| 101 | path: "a/self/self/b/pkg", |
| 102 | want: false, |
| 103 | why: "would follow self-link twice", |
| 104 | }, |
| 105 | } |
| 106 | for _, tc := range tests { |
| 107 | if got[tc.path] != tc.want { |
| 108 | if tc.want { |
| 109 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("MISSING: %s (%s)", tc.path, tc.why) |
| 110 | } else { |
| 111 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("UNEXPECTED: %s (%s)", tc.path, tc.why) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // TestSkip tests that various goimports rules are followed in non-modules mode. |
| 118 | func TestSkipscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/TestSkip().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 119 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 120 | |
| 121 | dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().() |
| 122 | |
| 123 | if err := mapToDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/mapToDir().(dir, map[string]string{ |
| 124 | "ignoreme/f.go": "package ignoreme", // ignored by .goimportsignore |
| 125 | "node_modules/f.go": "package nodemodules;", // ignored by hardcoded node_modules filter |
| 126 | "v/f.go": "package v;", // ignored by hardcoded vgo cache rule |
| 127 | "mod/f.go": "package mod;", // ignored by hardcoded vgo cache rule |
| 128 | "shouldfind/f.go": "package shouldfind;", // not ignored |
| 129 | |
| 130 | ".goimportsignore": "ignoreme\n", |
| 131 | }); err != nil { |
| 132 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 133 | } |
| 134 | |
| 135 | var found []string |
| 136 | var mu syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 137 | walkDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/walkDir().(Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{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().(dir, "src"), RootGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOPATH.}, |
| 138 | func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) { |
| 139 | mu.Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 140 | defer mu.Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 141 | found = append(found, dir[len(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.)+1:]) |
| 142 | }, func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) bool { |
| 143 | return false |
| 144 | }, Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#{ |
| 145 | ModulesEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#ModulesEnabled.: false, |
| 146 | Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Logf.: t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf()., |
| 147 | }) |
| 148 | if want := []string{"shouldfind"}; !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(found, want) { |
| 149 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected to find only %v, got %v", want, found) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // TestSkipFunction tests that scan successfully skips directories from user callback. |
| 154 | func TestSkipFunctionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/TestSkipFunction().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 155 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 156 | |
| 157 | dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().() |
| 158 | |
| 159 | if err := mapToDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/mapToDir().(dir, map[string]string{ |
| 160 | "ignoreme/f.go": "package ignoreme", // ignored by skip |
| 161 | "ignoreme/subignore/f.go": "package subignore", // also ignored by skip |
| 162 | "shouldfind/f.go": "package shouldfind;", // not ignored |
| 163 | }); err != nil { |
| 164 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 165 | } |
| 166 | |
| 167 | var found []string |
| 168 | var mu syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 169 | walkDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/walkDir().(Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{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().(dir, "src"), RootGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOPATH.}, |
| 170 | func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) { |
| 171 | mu.Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 172 | defer mu.Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 173 | found = append(found, dir[len(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.)+1:]) |
| 174 | }, func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) bool { |
| 175 | return 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().(dir, "ignoreme") |
| 176 | }, |
| 177 | Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#{ |
| 178 | ModulesEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#ModulesEnabled.: false, |
| 179 | Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Logf.: t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf()., |
| 180 | }) |
| 181 | if want := []string{"shouldfind"}; !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(found, want) { |
| 182 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected to find only %v, got %v", want, found) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // TestWalkSymlinkConcurrentDeletion is a regression test for the panic reported |
| 187 | // in https://go.dev/issue/58054#issuecomment-1791513726. |
| 188 | func TestWalkSymlinkConcurrentDeletionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/TestWalkSymlinkConcurrentDeletion().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 189 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 190 | |
| 191 | src := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().() |
| 192 | |
| 193 | m := map[string]string{ |
| 194 | "dir/readme.txt": "dir is not a go package", |
| 195 | "dirlink": "LINK:dir", |
| 196 | } |
| 197 | if err := mapToDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/mapToDir().(src, m); err != nil { |
| 198 | switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. { |
| 199 | case "windows", "plan9": |
| 200 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("skipping symlink-requiring test on %s", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS.) |
| 201 | } |
| 202 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 203 | } |
| 204 | |
| 205 | done := make(chan struct{}) |
| 206 | go func() { |
| 207 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(src); err != nil { |
| 208 | t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(err) |
| 209 | } |
| 210 | close(done) |
| 211 | }() |
| 212 | defer func() { |
| 213 | <-done |
| 214 | }() |
| 215 | |
| 216 | add := func(root Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) { |
| 217 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected call to add(%q, %q)", root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path., dir) |
| 218 | } |
| 219 | Walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Walk().([]Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{{Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.: src, Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type.: RootGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOPATH.}}, add, Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#{Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Logf.: t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().}) |
| 220 | } |
| 221 | |
| 222 | func mapToDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/mapToDir().(destDir string, files map[string]string) error { |
| 223 | var symlinkPaths []string |
| 224 | for path, contents := range files { |
| 225 | file := 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().(destDir, "src", path) |
| 226 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(file), 0755); err != nil { |
| 227 | return err |
| 228 | } |
| 229 | var err error |
| 230 | 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().(contents, "LINK:") { |
| 231 | // To work around https://go.dev/issue/39183, wait to create symlinks |
| 232 | // until we have created all non-symlink paths. |
| 233 | symlinkPaths = append(symlinkPaths, path) |
| 234 | } else { |
| 235 | err = osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(file, []byte(contents), 0644) |
| 236 | } |
| 237 | if err != nil { |
| 238 | return err |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | for _, path := range symlinkPaths { |
| 243 | file := 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().(destDir, "src", path) |
| 244 | target := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/FromSlash().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(files[path], "LINK:")) |
| 245 | err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Symlinkscip-go gomod github.com/golang/go/src go1.25.0 os/Symlink().(target, file) |
| 246 | if err != nil { |
| 247 | return err |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return nil |
| 252 | } |