| 1 | // Copyright 2019 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 imports |
| 6 | |
| 7 | import ( |
| 8 | "archive/zip" |
| 9 | "context" |
| 10 | "fmt" |
| 11 | "log" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "reflect" |
| 15 | "regexp" |
| 16 | "sort" |
| 17 | "strings" |
| 18 | "sync" |
| 19 | "testing" |
| 20 | "time" |
| 21 | |
| 22 | "golang.org/x/mod/module" |
| 23 | "golang.org/x/tools/internal/gocommand" |
| 24 | "golang.org/x/tools/internal/gopathwalk" |
| 25 | "golang.org/x/tools/internal/proxydir" |
| 26 | "golang.org/x/tools/internal/testenv" |
| 27 | "golang.org/x/tools/txtar" |
| 28 | "maps" |
| 29 | "slices" |
| 30 | ) |
| 31 | |
| 32 | // Tests that we can find packages in the stdlib. |
| 33 | func TestScanStdlib(t *testing.T) { |
| 34 | mt := setup(t, nil, ` |
| 35 | -- go.mod -- |
| 36 | module x |
| 37 | `, "") |
| 38 | defer mt.cleanup() |
| 39 | |
| 40 | mt.assertScanFinds("fmt", "fmt") |
| 41 | } |
| 42 | |
| 43 | // Tests that we handle a nested module. This is different from other tests |
| 44 | // where the module is in scope -- here we have to figure out the import path |
| 45 | // without any help from go list. |
| 46 | func TestScanOutOfScopeNestedModule(t *testing.T) { |
| 47 | mt := setup(t, nil, ` |
| 48 | -- go.mod -- |
| 49 | module x |
| 50 | |
| 51 | -- x.go -- |
| 52 | package x |
| 53 | |
| 54 | -- v2/go.mod -- |
| 55 | module x |
| 56 | |
| 57 | -- v2/x.go -- |
| 58 | package x`, "") |
| 59 | defer mt.cleanup() |
| 60 | |
| 61 | pkg := mt.assertScanFinds("x/v2", "x") |
| 62 | if pkg != nil && !strings.HasSuffix(filepath.ToSlash(pkg.dir), "main/v2") { |
| 63 | t.Errorf("x/v2 was found in %v, wanted .../main/v2", pkg.dir) |
| 64 | } |
| 65 | // We can't load the package name from the import path, but that should |
| 66 | // be okay -- if we end up adding this result, we'll add it with a name |
| 67 | // if necessary. |
| 68 | } |
| 69 | |
| 70 | // Tests that we don't find a nested module contained in a local replace target. |
| 71 | // The code for this case is too annoying to write, so it's just ignored. |
| 72 | func TestScanNestedModuleInLocalReplace(t *testing.T) { |
| 73 | mt := setup(t, nil, ` |
| 74 | -- go.mod -- |
| 75 | module x |
| 76 | |
| 77 | require y v0.0.0 |
| 78 | replace y => ./y |
| 79 | |
| 80 | -- x.go -- |
| 81 | package x |
| 82 | |
| 83 | -- y/go.mod -- |
| 84 | module y |
| 85 | |
| 86 | -- y/y.go -- |
| 87 | package y |
| 88 | |
| 89 | -- y/z/go.mod -- |
| 90 | module y/z |
| 91 | |
| 92 | -- y/z/z.go -- |
| 93 | package z |
| 94 | `, "") |
| 95 | defer mt.cleanup() |
| 96 | |
| 97 | mt.assertFound("y", "y") |
| 98 | |
| 99 | scan, err := scanToSlice(mt.env.resolver, nil) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | for _, pkg := range scan { |
| 104 | if strings.HasSuffix(filepath.ToSlash(pkg.dir), "main/y/z") { |
| 105 | t.Errorf("scan found a package %v in dir main/y/z, wanted none", pkg.importPathShort) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Tests that path encoding is handled correctly. Adapted from mod_case.txt. |
| 111 | func TestModCase(t *testing.T) { |
| 112 | mt := setup(t, nil, ` |
| 113 | -- go.mod -- |
| 114 | module x |
| 115 | |
| 116 | require rsc.io/QUOTE v1.5.2 |
| 117 | |
| 118 | -- x.go -- |
| 119 | package x |
| 120 | |
| 121 | import _ "rsc.io/QUOTE/QUOTE" |
| 122 | `, "") |
| 123 | defer mt.cleanup() |
| 124 | mt.assertFound("rsc.io/QUOTE/QUOTE", "QUOTE") |
| 125 | } |
| 126 | |
| 127 | // Not obviously relevant to goimports. Adapted from mod_domain_root.txt anyway. |
| 128 | func TestModDomainRoot(t *testing.T) { |
| 129 | mt := setup(t, nil, ` |
| 130 | -- go.mod -- |
| 131 | module x |
| 132 | |
| 133 | require example.com v1.0.0 |
| 134 | |
| 135 | -- x.go -- |
| 136 | package x |
| 137 | import _ "example.com" |
| 138 | `, "") |
| 139 | defer mt.cleanup() |
| 140 | mt.assertFound("example.com", "x") |
| 141 | } |
| 142 | |
| 143 | // Tests that scanning the module cache > 1 time is able to find the same module. |
| 144 | func TestModMultipleScans(t *testing.T) { |
| 145 | mt := setup(t, nil, ` |
| 146 | -- go.mod -- |
| 147 | module x |
| 148 | |
| 149 | require example.com v1.0.0 |
| 150 | |
| 151 | -- x.go -- |
| 152 | package x |
| 153 | import _ "example.com" |
| 154 | `, "") |
| 155 | defer mt.cleanup() |
| 156 | |
| 157 | mt.assertScanFinds("example.com", "x") |
| 158 | mt.assertScanFinds("example.com", "x") |
| 159 | } |
| 160 | |
| 161 | // Tests that scanning the module cache > 1 time is able to find the same module |
| 162 | // in the module cache. |
| 163 | func TestModMultipleScansWithSubdirs(t *testing.T) { |
| 164 | mt := setup(t, nil, ` |
| 165 | -- go.mod -- |
| 166 | module x |
| 167 | |
| 168 | require rsc.io/quote v1.5.2 |
| 169 | |
| 170 | -- x.go -- |
| 171 | package x |
| 172 | import _ "rsc.io/quote" |
| 173 | `, "") |
| 174 | defer mt.cleanup() |
| 175 | |
| 176 | mt.assertScanFinds("rsc.io/quote", "quote") |
| 177 | mt.assertScanFinds("rsc.io/quote", "quote") |
| 178 | } |
| 179 | |
| 180 | // Tests that scanning the module cache > 1 after changing a package in module cache to make it unimportable |
| 181 | // is able to find the same module. |
| 182 | func TestModCacheEditModFile(t *testing.T) { |
| 183 | mt := setup(t, nil, ` |
| 184 | -- go.mod -- |
| 185 | module x |
| 186 | |
| 187 | require rsc.io/quote v1.5.2 |
| 188 | -- x.go -- |
| 189 | package x |
| 190 | import _ "rsc.io/quote" |
| 191 | `, "") |
| 192 | defer mt.cleanup() |
| 193 | found := mt.assertScanFinds("rsc.io/quote", "quote") |
| 194 | if found == nil { |
| 195 | t.Fatal("rsc.io/quote not found in initial scan.") |
| 196 | } |
| 197 | |
| 198 | // Update the go.mod file of example.com so that it changes its module path (not allowed). |
| 199 | if err := os.Chmod(filepath.Join(found.dir, "go.mod"), 0644); err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | if err := os.WriteFile(filepath.Join(found.dir, "go.mod"), []byte("module bad.com\n"), 0644); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | |
| 206 | // Test that with its cache of module packages it still finds the package. |
| 207 | mt.assertScanFinds("rsc.io/quote", "quote") |
| 208 | |
| 209 | // Rewrite the main package so that rsc.io/quote is not in scope. |
| 210 | if err := os.WriteFile(filepath.Join(mt.env.WorkingDir, "go.mod"), []byte("module x\n"), 0644); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | if err := os.WriteFile(filepath.Join(mt.env.WorkingDir, "x.go"), []byte("package x\n"), 0644); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | |
| 217 | // Uninitialize the go.mod dependent cached information and make sure it still finds the package. |
| 218 | mt.env.ClearModuleInfo() |
| 219 | mt.assertScanFinds("rsc.io/quote", "quote") |
| 220 | } |
| 221 | |
| 222 | // Tests that -mod=vendor works. Adapted from mod_vendor_build.txt. |
| 223 | func TestModVendorBuild(t *testing.T) { |
| 224 | mt := setup(t, nil, ` |
| 225 | -- go.mod -- |
| 226 | module m |
| 227 | go 1.12 |
| 228 | require rsc.io/sampler v1.3.1 |
| 229 | -- x.go -- |
| 230 | package x |
| 231 | import _ "rsc.io/sampler" |
| 232 | `, "") |
| 233 | defer mt.cleanup() |
| 234 | |
| 235 | // Sanity-check the setup. |
| 236 | mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `pkg.*mod.*/sampler@.*$`) |
| 237 | |
| 238 | // Populate vendor/ and clear out the mod cache so we can't cheat. |
| 239 | if _, err := mt.env.invokeGo(context.Background(), "mod", "vendor"); err != nil { |
| 240 | t.Fatal(err) |
| 241 | } |
| 242 | if _, err := mt.env.invokeGo(context.Background(), "clean", "-modcache"); err != nil { |
| 243 | t.Fatal(err) |
| 244 | } |
| 245 | |
| 246 | // Clear out the resolver's cache, since we've changed the environment. |
| 247 | mt.env.Env["GOFLAGS"] = "-mod=vendor" |
| 248 | mt.env.ClearModuleInfo() |
| 249 | mt.env.UpdateResolver(mt.env.resolver.ClearForNewScan()) |
| 250 | mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `/vendor/`) |
| 251 | } |
| 252 | |
| 253 | // Tests that -mod=vendor is auto-enabled only for go1.14 and higher. |
| 254 | // Vaguely inspired by mod_vendor_auto.txt. |
| 255 | func TestModVendorAuto(t *testing.T) { |
| 256 | mt := setup(t, nil, ` |
| 257 | -- go.mod -- |
| 258 | module m |
| 259 | go 1.14 |
| 260 | require rsc.io/sampler v1.3.1 |
| 261 | -- x.go -- |
| 262 | package x |
| 263 | import _ "rsc.io/sampler" |
| 264 | `, "") |
| 265 | defer mt.cleanup() |
| 266 | |
| 267 | // Populate vendor/. |
| 268 | if _, err := mt.env.invokeGo(context.Background(), "mod", "vendor"); err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | |
| 272 | wantDir := `/vendor/` |
| 273 | |
| 274 | // Clear out the resolver's module info, since we've changed the environment. |
| 275 | // (the presence of a /vendor directory affects `go list -m`). |
| 276 | mt.env.ClearModuleInfo() |
| 277 | mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", wantDir) |
| 278 | } |
| 279 | |
| 280 | // Tests that a module replace works. Adapted from mod_list.txt. We start with |
| 281 | // go.mod2; the first part of the test is irrelevant. |
| 282 | func TestModList(t *testing.T) { |
| 283 | mt := setup(t, nil, ` |
| 284 | -- go.mod -- |
| 285 | module x |
| 286 | require rsc.io/quote v1.5.1 |
| 287 | replace rsc.io/sampler v1.3.0 => rsc.io/sampler v1.3.1 |
| 288 | |
| 289 | -- x.go -- |
| 290 | package x |
| 291 | import _ "rsc.io/quote" |
| 292 | `, "") |
| 293 | defer mt.cleanup() |
| 294 | |
| 295 | mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `pkg.mod.*/[email protected]$`) |
| 296 | } |
| 297 | |
| 298 | // Tests that a local replace works. Adapted from mod_local_replace.txt. |
| 299 | func TestModLocalReplace(t *testing.T) { |
| 300 | mt := setup(t, nil, ` |
| 301 | -- x/y/go.mod -- |
| 302 | module x/y |
| 303 | require zz v1.0.0 |
| 304 | replace zz v1.0.0 => ../z |
| 305 | |
| 306 | -- x/y/y.go -- |
| 307 | package y |
| 308 | import _ "zz" |
| 309 | |
| 310 | -- x/z/go.mod -- |
| 311 | module x/z |
| 312 | |
| 313 | -- x/z/z.go -- |
| 314 | package z |
| 315 | `, "x/y") |
| 316 | defer mt.cleanup() |
| 317 | |
| 318 | mt.assertFound("zz", "z") |
| 319 | } |
| 320 | |
| 321 | // Tests that the package at the root of the main module can be found. |
| 322 | // Adapted from the first part of mod_multirepo.txt. |
| 323 | func TestModMultirepo1(t *testing.T) { |
| 324 | mt := setup(t, nil, ` |
| 325 | -- go.mod -- |
| 326 | module rsc.io/quote |
| 327 | |
| 328 | -- x.go -- |
| 329 | package quote |
| 330 | `, "") |
| 331 | defer mt.cleanup() |
| 332 | |
| 333 | mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`) |
| 334 | } |
| 335 | |
| 336 | // Tests that a simple module dependency is found. Adapted from the third part |
| 337 | // of mod_multirepo.txt (We skip the case where it doesn't have a go.mod |
| 338 | // entry -- we just don't work in that case.) |
| 339 | func TestModMultirepo3(t *testing.T) { |
| 340 | mt := setup(t, nil, ` |
| 341 | -- go.mod -- |
| 342 | module rsc.io/quote |
| 343 | |
| 344 | require rsc.io/quote/v2 v2.0.1 |
| 345 | -- x.go -- |
| 346 | package quote |
| 347 | |
| 348 | import _ "rsc.io/quote/v2" |
| 349 | `, "") |
| 350 | defer mt.cleanup() |
| 351 | |
| 352 | mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`) |
| 353 | mt.assertModuleFoundInDir("rsc.io/quote/v2", "quote", `pkg.mod.*/[email protected]$`) |
| 354 | } |
| 355 | |
| 356 | // Tests that a nested module is found in the module cache, even though |
| 357 | // it's checked out. Adapted from the fourth part of mod_multirepo.txt. |
| 358 | func TestModMultirepo4(t *testing.T) { |
| 359 | mt := setup(t, nil, ` |
| 360 | -- go.mod -- |
| 361 | module rsc.io/quote |
| 362 | require rsc.io/quote/v2 v2.0.1 |
| 363 | |
| 364 | -- x.go -- |
| 365 | package quote |
| 366 | import _ "rsc.io/quote/v2" |
| 367 | |
| 368 | -- v2/go.mod -- |
| 369 | package rsc.io/quote/v2 |
| 370 | |
| 371 | -- v2/x.go -- |
| 372 | package quote |
| 373 | import _ "rsc.io/quote/v2" |
| 374 | `, "") |
| 375 | defer mt.cleanup() |
| 376 | |
| 377 | mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`) |
| 378 | mt.assertModuleFoundInDir("rsc.io/quote/v2", "quote", `pkg.mod.*/[email protected]$`) |
| 379 | } |
| 380 | |
| 381 | // Tests a simple module dependency. Adapted from the first part of mod_replace.txt. |
| 382 | func TestModReplace1(t *testing.T) { |
| 383 | mt := setup(t, nil, ` |
| 384 | -- go.mod -- |
| 385 | module quoter |
| 386 | |
| 387 | require rsc.io/quote/v3 v3.0.0 |
| 388 | |
| 389 | -- main.go -- |
| 390 | |
| 391 | package main |
| 392 | `, "") |
| 393 | defer mt.cleanup() |
| 394 | mt.assertFound("rsc.io/quote/v3", "quote") |
| 395 | } |
| 396 | |
| 397 | // Tests a local replace. Adapted from the second part of mod_replace.txt. |
| 398 | func TestModReplace2(t *testing.T) { |
| 399 | mt := setup(t, nil, ` |
| 400 | -- go.mod -- |
| 401 | module quoter |
| 402 | |
| 403 | require rsc.io/quote/v3 v3.0.0 |
| 404 | replace rsc.io/quote/v3 => ./local/rsc.io/quote/v3 |
| 405 | -- main.go -- |
| 406 | package main |
| 407 | |
| 408 | -- local/rsc.io/quote/v3/go.mod -- |
| 409 | module rsc.io/quote/v3 |
| 410 | |
| 411 | require rsc.io/sampler v1.3.0 |
| 412 | |
| 413 | -- local/rsc.io/quote/v3/quote.go -- |
| 414 | package quote |
| 415 | |
| 416 | import "rsc.io/sampler" |
| 417 | `, "") |
| 418 | defer mt.cleanup() |
| 419 | mt.assertModuleFoundInDir("rsc.io/quote/v3", "quote", `/local/rsc.io/quote/v3`) |
| 420 | } |
| 421 | |
| 422 | // Tests that a module can be replaced by a different module path. Adapted |
| 423 | // from the third part of mod_replace.txt. |
| 424 | func TestModReplace3(t *testing.T) { |
| 425 | mt := setup(t, nil, ` |
| 426 | -- go.mod -- |
| 427 | module quoter |
| 428 | |
| 429 | require not-rsc.io/quote/v3 v3.1.0 |
| 430 | replace not-rsc.io/quote/v3 v3.1.0 => ./local/rsc.io/quote/v3 |
| 431 | |
| 432 | -- usenewmodule/main.go -- |
| 433 | package main |
| 434 | |
| 435 | -- local/rsc.io/quote/v3/go.mod -- |
| 436 | module rsc.io/quote/v3 |
| 437 | |
| 438 | require rsc.io/sampler v1.3.0 |
| 439 | |
| 440 | -- local/rsc.io/quote/v3/quote.go -- |
| 441 | package quote |
| 442 | |
| 443 | -- local/not-rsc.io/quote/v3/go.mod -- |
| 444 | module not-rsc.io/quote/v3 |
| 445 | |
| 446 | -- local/not-rsc.io/quote/v3/quote.go -- |
| 447 | package quote |
| 448 | `, "") |
| 449 | defer mt.cleanup() |
| 450 | mt.assertModuleFoundInDir("not-rsc.io/quote/v3", "quote", "local/rsc.io/quote/v3") |
| 451 | } |
| 452 | |
| 453 | // Tests more local replaces, notably the case where an outer module provides |
| 454 | // a package that could also be provided by an inner module. Adapted from |
| 455 | // mod_replace_import.txt, with example.com/v changed to /vv because Go 1.11 |
| 456 | // thinks /v is an invalid major version. |
| 457 | func TestModReplaceImport(t *testing.T) { |
| 458 | mt := setup(t, nil, ` |
| 459 | -- go.mod -- |
| 460 | module example.com/m |
| 461 | |
| 462 | replace ( |
| 463 | example.com/a => ./a |
| 464 | example.com/a/b => ./b |
| 465 | ) |
| 466 | |
| 467 | replace ( |
| 468 | example.com/x => ./x |
| 469 | example.com/x/v3 => ./v3 |
| 470 | ) |
| 471 | |
| 472 | replace ( |
| 473 | example.com/y/z/w => ./w |
| 474 | example.com/y => ./y |
| 475 | ) |
| 476 | |
| 477 | replace ( |
| 478 | example.com/vv v1.11.0 => ./v11 |
| 479 | example.com/vv v1.12.0 => ./v12 |
| 480 | example.com/vv => ./vv |
| 481 | ) |
| 482 | |
| 483 | require ( |
| 484 | example.com/a/b v0.0.0 |
| 485 | example.com/x/v3 v3.0.0 |
| 486 | example.com/y v0.0.0 |
| 487 | example.com/y/z/w v0.0.0 |
| 488 | example.com/vv v1.12.0 |
| 489 | ) |
| 490 | -- m.go -- |
| 491 | package main |
| 492 | import ( |
| 493 | _ "example.com/a/b" |
| 494 | _ "example.com/x/v3" |
| 495 | _ "example.com/y/z/w" |
| 496 | _ "example.com/vv" |
| 497 | ) |
| 498 | func main() {} |
| 499 | |
| 500 | -- a/go.mod -- |
| 501 | module a.localhost |
| 502 | -- a/a.go -- |
| 503 | package a |
| 504 | -- a/b/b.go-- |
| 505 | package b |
| 506 | |
| 507 | -- b/go.mod -- |
| 508 | module a.localhost/b |
| 509 | -- b/b.go -- |
| 510 | package b |
| 511 | |
| 512 | -- x/go.mod -- |
| 513 | module x.localhost |
| 514 | -- x/x.go -- |
| 515 | package x |
| 516 | -- x/v3.go -- |
| 517 | package v3 |
| 518 | import _ "x.localhost/v3" |
| 519 | |
| 520 | -- v3/go.mod -- |
| 521 | module x.localhost/v3 |
| 522 | -- v3/x.go -- |
| 523 | package x |
| 524 | |
| 525 | -- w/go.mod -- |
| 526 | module w.localhost |
| 527 | -- w/skip/skip.go -- |
| 528 | // Package skip is nested below nonexistent package w. |
| 529 | package skip |
| 530 | |
| 531 | -- y/go.mod -- |
| 532 | module y.localhost |
| 533 | -- y/z/w/w.go -- |
| 534 | package w |
| 535 | |
| 536 | -- v12/go.mod -- |
| 537 | module v.localhost |
| 538 | -- v12/v.go -- |
| 539 | package v |
| 540 | |
| 541 | -- v11/go.mod -- |
| 542 | module v.localhost |
| 543 | -- v11/v.go -- |
| 544 | package v |
| 545 | |
| 546 | -- vv/go.mod -- |
| 547 | module v.localhost |
| 548 | -- vv/v.go -- |
| 549 | package v |
| 550 | `, "") |
| 551 | defer mt.cleanup() |
| 552 | |
| 553 | mt.assertModuleFoundInDir("example.com/a/b", "b", `main/b$`) |
| 554 | mt.assertModuleFoundInDir("example.com/x/v3", "x", `main/v3$`) |
| 555 | mt.assertModuleFoundInDir("example.com/y/z/w", "w", `main/y/z/w$`) |
| 556 | mt.assertModuleFoundInDir("example.com/vv", "v", `main/v12$`) |
| 557 | } |
| 558 | |
| 559 | // Tests that go.work files are respected. |
| 560 | func TestModWorkspace(t *testing.T) { |
| 561 | mt := setup(t, nil, ` |
| 562 | -- go.work -- |
| 563 | go 1.18 |
| 564 | |
| 565 | use ( |
| 566 | ./a |
| 567 | ./b |
| 568 | ) |
| 569 | -- a/go.mod -- |
| 570 | module example.com/a |
| 571 | |
| 572 | go 1.18 |
| 573 | -- a/a.go -- |
| 574 | package a |
| 575 | -- b/go.mod -- |
| 576 | module example.com/b |
| 577 | |
| 578 | go 1.18 |
| 579 | -- b/b.go -- |
| 580 | package b |
| 581 | `, "") |
| 582 | defer mt.cleanup() |
| 583 | |
| 584 | mt.assertModuleFoundInDir("example.com/a", "a", `main/a$`) |
| 585 | mt.assertModuleFoundInDir("example.com/b", "b", `main/b$`) |
| 586 | mt.assertScanFinds("example.com/a", "a") |
| 587 | mt.assertScanFinds("example.com/b", "b") |
| 588 | } |
| 589 | |
| 590 | // Tests replaces in workspaces. Uses the directory layout in the cmd/go |
| 591 | // work_replace test. It tests both that replaces in go.work files are |
| 592 | // respected and that a wildcard replace in go.work overrides a versioned replace |
| 593 | // in go.mod. |
| 594 | func TestModWorkspaceReplace(t *testing.T) { |
| 595 | mt := setup(t, nil, ` |
| 596 | -- go.work -- |
| 597 | use m |
| 598 | |
| 599 | replace example.com/dep => ./dep |
| 600 | replace example.com/other => ./other2 |
| 601 | |
| 602 | -- m/go.mod -- |
| 603 | module example.com/m |
| 604 | |
| 605 | require example.com/dep v1.0.0 |
| 606 | require example.com/other v1.0.0 |
| 607 | |
| 608 | replace example.com/other v1.0.0 => ./other |
| 609 | -- m/m.go -- |
| 610 | package m |
| 611 | |
| 612 | import "example.com/dep" |
| 613 | import "example.com/other" |
| 614 | |
| 615 | func F() { |
| 616 | dep.G() |
| 617 | other.H() |
| 618 | } |
| 619 | -- dep/go.mod -- |
| 620 | module example.com/dep |
| 621 | -- dep/dep.go -- |
| 622 | package dep |
| 623 | |
| 624 | func G() { |
| 625 | } |
| 626 | -- other/go.mod -- |
| 627 | module example.com/other |
| 628 | -- other/dep.go -- |
| 629 | package other |
| 630 | |
| 631 | func G() { |
| 632 | } |
| 633 | -- other2/go.mod -- |
| 634 | module example.com/other |
| 635 | -- other2/dep.go -- |
| 636 | package other2 |
| 637 | |
| 638 | func G() { |
| 639 | } |
| 640 | `, "") |
| 641 | defer mt.cleanup() |
| 642 | |
| 643 | mt.assertScanFinds("example.com/m", "m") |
| 644 | mt.assertScanFinds("example.com/dep", "dep") |
| 645 | mt.assertModuleFoundInDir("example.com/other", "other2", "main/other2$") |
| 646 | mt.assertScanFinds("example.com/other", "other2") |
| 647 | } |
| 648 | |
| 649 | // Tests a case where conflicting replaces are overridden by a replace |
| 650 | // in the go.work file. |
| 651 | func TestModWorkspaceReplaceOverride(t *testing.T) { |
| 652 | mt := setup(t, nil, `-- go.work -- |
| 653 | use m |
| 654 | use n |
| 655 | replace example.com/dep => ./dep3 |
| 656 | -- m/go.mod -- |
| 657 | module example.com/m |
| 658 | |
| 659 | require example.com/dep v1.0.0 |
| 660 | replace example.com/dep => ./dep1 |
| 661 | -- m/m.go -- |
| 662 | package m |
| 663 | |
| 664 | import "example.com/dep" |
| 665 | |
| 666 | func F() { |
| 667 | dep.G() |
| 668 | } |
| 669 | -- n/go.mod -- |
| 670 | module example.com/n |
| 671 | |
| 672 | require example.com/dep v1.0.0 |
| 673 | replace example.com/dep => ./dep2 |
| 674 | -- n/n.go -- |
| 675 | package n |
| 676 | |
| 677 | import "example.com/dep" |
| 678 | |
| 679 | func F() { |
| 680 | dep.G() |
| 681 | } |
| 682 | -- dep1/go.mod -- |
| 683 | module example.com/dep |
| 684 | -- dep1/dep.go -- |
| 685 | package dep |
| 686 | |
| 687 | func G() { |
| 688 | } |
| 689 | -- dep2/go.mod -- |
| 690 | module example.com/dep |
| 691 | -- dep2/dep.go -- |
| 692 | package dep |
| 693 | |
| 694 | func G() { |
| 695 | } |
| 696 | -- dep3/go.mod -- |
| 697 | module example.com/dep |
| 698 | -- dep3/dep.go -- |
| 699 | package dep |
| 700 | |
| 701 | func G() { |
| 702 | } |
| 703 | `, "") |
| 704 | |
| 705 | mt.assertScanFinds("example.com/m", "m") |
| 706 | mt.assertScanFinds("example.com/n", "n") |
| 707 | mt.assertScanFinds("example.com/dep", "dep") |
| 708 | mt.assertModuleFoundInDir("example.com/dep", "dep", "main/dep3$") |
| 709 | } |
| 710 | |
| 711 | // Tests that the correct versions of modules are found in |
| 712 | // workspaces with module pruning. This is based on the |
| 713 | // cmd/go mod_prune_all script test. |
| 714 | func TestModWorkspacePrune(t *testing.T) { |
| 715 | mt := setup(t, nil, ` |
| 716 | -- go.work -- |
| 717 | go 1.18 |
| 718 | |
| 719 | use ( |
| 720 | ./a |
| 721 | ./p |
| 722 | ) |
| 723 | |
| 724 | replace example.com/b v1.0.0 => ./b |
| 725 | replace example.com/q v1.0.0 => ./q1_0_0 |
| 726 | replace example.com/q v1.0.5 => ./q1_0_5 |
| 727 | replace example.com/q v1.1.0 => ./q1_1_0 |
| 728 | replace example.com/r v1.0.0 => ./r |
| 729 | replace example.com/w v1.0.0 => ./w |
| 730 | replace example.com/x v1.0.0 => ./x |
| 731 | replace example.com/y v1.0.0 => ./y |
| 732 | replace example.com/z v1.0.0 => ./z1_0_0 |
| 733 | replace example.com/z v1.1.0 => ./z1_1_0 |
| 734 | |
| 735 | -- a/go.mod -- |
| 736 | module example.com/a |
| 737 | |
| 738 | go 1.18 |
| 739 | |
| 740 | require example.com/b v1.0.0 |
| 741 | require example.com/z v1.0.0 |
| 742 | -- a/foo.go -- |
| 743 | package main |
| 744 | |
| 745 | import "example.com/b" |
| 746 | |
| 747 | func main() { |
| 748 | b.B() |
| 749 | } |
| 750 | -- b/go.mod -- |
| 751 | module example.com/b |
| 752 | |
| 753 | go 1.18 |
| 754 | |
| 755 | require example.com/q v1.1.0 |
| 756 | -- b/b.go -- |
| 757 | package b |
| 758 | |
| 759 | func B() { |
| 760 | } |
| 761 | -- p/go.mod -- |
| 762 | module example.com/p |
| 763 | |
| 764 | go 1.18 |
| 765 | |
| 766 | require example.com/q v1.0.0 |
| 767 | |
| 768 | replace example.com/q v1.0.0 => ../q1_0_0 |
| 769 | replace example.com/q v1.1.0 => ../q1_1_0 |
| 770 | -- p/main.go -- |
| 771 | package main |
| 772 | |
| 773 | import "example.com/q" |
| 774 | |
| 775 | func main() { |
| 776 | q.PrintVersion() |
| 777 | } |
| 778 | -- q1_0_0/go.mod -- |
| 779 | module example.com/q |
| 780 | |
| 781 | go 1.18 |
| 782 | -- q1_0_0/q.go -- |
| 783 | package q |
| 784 | |
| 785 | import "fmt" |
| 786 | |
| 787 | func PrintVersion() { |
| 788 | fmt.Println("version 1.0.0") |
| 789 | } |
| 790 | -- q1_0_5/go.mod -- |
| 791 | module example.com/q |
| 792 | |
| 793 | go 1.18 |
| 794 | |
| 795 | require example.com/r v1.0.0 |
| 796 | -- q1_0_5/q.go -- |
| 797 | package q |
| 798 | |
| 799 | import _ "example.com/r" |
| 800 | -- q1_1_0/go.mod -- |
| 801 | module example.com/q |
| 802 | |
| 803 | require example.com/w v1.0.0 |
| 804 | require example.com/z v1.1.0 |
| 805 | |
| 806 | go 1.18 |
| 807 | -- q1_1_0/q.go -- |
| 808 | package q |
| 809 | |
| 810 | import _ "example.com/w" |
| 811 | import _ "example.com/z" |
| 812 | |
| 813 | import "fmt" |
| 814 | |
| 815 | func PrintVersion() { |
| 816 | fmt.Println("version 1.1.0") |
| 817 | } |
| 818 | -- r/go.mod -- |
| 819 | module example.com/r |
| 820 | |
| 821 | go 1.18 |
| 822 | |
| 823 | require example.com/r v1.0.0 |
| 824 | -- r/r.go -- |
| 825 | package r |
| 826 | -- w/go.mod -- |
| 827 | module example.com/w |
| 828 | |
| 829 | go 1.18 |
| 830 | |
| 831 | require example.com/x v1.0.0 |
| 832 | -- w/w.go -- |
| 833 | package w |
| 834 | -- w/w_test.go -- |
| 835 | package w |
| 836 | |
| 837 | import _ "example.com/x" |
| 838 | -- x/go.mod -- |
| 839 | module example.com/x |
| 840 | |
| 841 | go 1.18 |
| 842 | -- x/x.go -- |
| 843 | package x |
| 844 | -- x/x_test.go -- |
| 845 | package x |
| 846 | import _ "example.com/y" |
| 847 | -- y/go.mod -- |
| 848 | module example.com/y |
| 849 | |
| 850 | go 1.18 |
| 851 | -- y/y.go -- |
| 852 | package y |
| 853 | -- z1_0_0/go.mod -- |
| 854 | module example.com/z |
| 855 | |
| 856 | go 1.18 |
| 857 | |
| 858 | require example.com/q v1.0.5 |
| 859 | -- z1_0_0/z.go -- |
| 860 | package z |
| 861 | |
| 862 | import _ "example.com/q" |
| 863 | -- z1_1_0/go.mod -- |
| 864 | module example.com/z |
| 865 | |
| 866 | go 1.18 |
| 867 | -- z1_1_0/z.go -- |
| 868 | package z |
| 869 | `, "") |
| 870 | |
| 871 | mt.assertScanFinds("example.com/w", "w") |
| 872 | mt.assertScanFinds("example.com/q", "q") |
| 873 | mt.assertScanFinds("example.com/x", "x") |
| 874 | mt.assertScanFinds("example.com/z", "z") |
| 875 | mt.assertModuleFoundInDir("example.com/w", "w", "main/w$") |
| 876 | mt.assertModuleFoundInDir("example.com/q", "q", "main/q1_1_0$") |
| 877 | mt.assertModuleFoundInDir("example.com/x", "x", "main/x$") |
| 878 | mt.assertModuleFoundInDir("example.com/z", "z", "main/z1_1_0$") |
| 879 | } |
| 880 | |
| 881 | // Tests that we handle GO111MODULE=on with no go.mod file. See #30855. |
| 882 | func TestNoMainModule(t *testing.T) { |
| 883 | mt := setup(t, map[string]string{"GO111MODULE": "on"}, ` |
| 884 | -- x.go -- |
| 885 | package x |
| 886 | `, "") |
| 887 | defer mt.cleanup() |
| 888 | if _, err := mt.env.invokeGo(context.Background(), "mod", "download", "rsc.io/[email protected]"); err != nil { |
| 889 | t.Fatal(err) |
| 890 | } |
| 891 | |
| 892 | mt.assertScanFinds("rsc.io/quote", "quote") |
| 893 | } |
| 894 | |
| 895 | // assertFound asserts that the package at importPath is found to have pkgName, |
| 896 | // and that scanning for pkgName finds it at importPath. |
| 897 | func (t *modTest) assertFound(importPath, pkgName string) (string, *pkg) { |
| 898 | t.Helper() |
| 899 | |
| 900 | names, err := t.env.resolver.loadPackageNames([]string{importPath}, t.env.WorkingDir) |
| 901 | if err != nil { |
| 902 | t.Errorf("loading package name for %v: %v", importPath, err) |
| 903 | } |
| 904 | if names[importPath] != pkgName { |
| 905 | t.Errorf("package name for %v = %v, want %v", importPath, names[importPath], pkgName) |
| 906 | } |
| 907 | pkg := t.assertScanFinds(importPath, pkgName) |
| 908 | |
| 909 | _, foundDir := t.env.resolver.(*ModuleResolver).findPackage(importPath) |
| 910 | return foundDir, pkg |
| 911 | } |
| 912 | |
| 913 | func (t *modTest) assertScanFinds(importPath, pkgName string) *pkg { |
| 914 | t.Helper() |
| 915 | scan, err := scanToSlice(t.env.resolver, nil) |
| 916 | if err != nil { |
| 917 | t.Errorf("scan failed: %v", err) |
| 918 | } |
| 919 | for _, pkg := range scan { |
| 920 | if pkg.importPathShort == importPath { |
| 921 | return pkg |
| 922 | } |
| 923 | } |
| 924 | t.Errorf("scanning for %v did not find %v", pkgName, importPath) |
| 925 | return nil |
| 926 | } |
| 927 | |
| 928 | func scanToSlice(resolver Resolver, exclude []gopathwalk.RootType) ([]*pkg, error) { |
| 929 | var mu sync.Mutex |
| 930 | var result []*pkg |
| 931 | filter := &scanCallback{ |
| 932 | rootFound: func(root gopathwalk.Root) bool { |
| 933 | return !slices.Contains(exclude, root.Type) |
| 934 | }, |
| 935 | dirFound: func(pkg *pkg) bool { |
| 936 | return true |
| 937 | }, |
| 938 | packageNameLoaded: func(pkg *pkg) bool { |
| 939 | mu.Lock() |
| 940 | defer mu.Unlock() |
| 941 | result = append(result, pkg) |
| 942 | return false |
| 943 | }, |
| 944 | } |
| 945 | err := resolver.scan(context.Background(), filter) |
| 946 | return result, err |
| 947 | } |
| 948 | |
| 949 | // assertModuleFoundInDir is the same as assertFound, but also checks that the |
| 950 | // package was found in an active module whose Dir matches dirRE. |
| 951 | func (t *modTest) assertModuleFoundInDir(importPath, pkgName, dirRE string) { |
| 952 | t.Helper() |
| 953 | dir, pkg := t.assertFound(importPath, pkgName) |
| 954 | re, err := regexp.Compile(dirRE) |
| 955 | if err != nil { |
| 956 | t.Fatal(err) |
| 957 | } |
| 958 | |
| 959 | if dir == "" { |
| 960 | t.Errorf("import path %v not found in active modules", importPath) |
| 961 | } else { |
| 962 | if !re.MatchString(filepath.ToSlash(dir)) { |
| 963 | t.Errorf("finding dir for %s: dir = %q did not match regex %q", importPath, dir, dirRE) |
| 964 | } |
| 965 | } |
| 966 | if pkg != nil { |
| 967 | if !re.MatchString(filepath.ToSlash(pkg.dir)) { |
| 968 | t.Errorf("scanning for %s: dir = %q did not match regex %q", pkgName, pkg.dir, dirRE) |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | var proxyOnce sync.Once |
| 974 | var proxyDir string |
| 975 | |
| 976 | type modTest struct { |
| 977 | *testing.T |
| 978 | env *ProcessEnv |
| 979 | gopath string |
| 980 | cleanup func() |
| 981 | } |
| 982 | |
| 983 | // setup builds a test environment from a txtar and supporting modules |
| 984 | // in testdata/mod, along the lines of TestScript in cmd/go. |
| 985 | // |
| 986 | // extraEnv is applied on top of the default test env. |
| 987 | func setup(t *testing.T, extraEnv map[string]string, main, wd string) *modTest { |
| 988 | t.Helper() |
| 989 | testenv.NeedsTool(t, "go") |
| 990 | |
| 991 | proxyOnce.Do(func() { |
| 992 | var err error |
| 993 | proxyDir, err = os.MkdirTemp("", "proxy-") |
| 994 | if err != nil { |
| 995 | t.Fatal(err) |
| 996 | } |
| 997 | if err := writeProxy(proxyDir, "testdata/mod"); err != nil { |
| 998 | t.Fatal(err) |
| 999 | } |
| 1000 | }) |
| 1001 | |
| 1002 | dir, err := os.MkdirTemp("", t.Name()) |
| 1003 | if err != nil { |
| 1004 | t.Fatal(err) |
| 1005 | } |
| 1006 | |
| 1007 | mainDir := filepath.Join(dir, "main") |
| 1008 | if err := writeModule(mainDir, main); err != nil { |
| 1009 | t.Fatal(err) |
| 1010 | } |
| 1011 | |
| 1012 | env := &ProcessEnv{ |
| 1013 | Env: map[string]string{ |
| 1014 | "GOPATH": filepath.Join(dir, "gopath"), |
| 1015 | "GOMODCACHE": "", |
| 1016 | "GO111MODULE": "auto", |
| 1017 | "GOSUMDB": "off", |
| 1018 | "GOPROXY": proxydir.ToURL(proxyDir), |
| 1019 | }, |
| 1020 | WorkingDir: filepath.Join(mainDir, wd), |
| 1021 | GocmdRunner: &gocommand.Runner{}, |
| 1022 | } |
| 1023 | maps.Copy(env.Env, extraEnv) |
| 1024 | if *testDebug { |
| 1025 | env.Logf = log.Printf |
| 1026 | } |
| 1027 | // go mod download gets mad if we don't have a go.mod, so make sure we do. |
| 1028 | _, err = os.Stat(filepath.Join(mainDir, "go.mod")) |
| 1029 | if err != nil && !os.IsNotExist(err) { |
| 1030 | t.Fatalf("checking if go.mod exists: %v", err) |
| 1031 | } |
| 1032 | if err == nil { |
| 1033 | if _, err := env.invokeGo(context.Background(), "mod", "download", "all"); err != nil { |
| 1034 | t.Fatal(err) |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | // Ensure the resolver is set for tests that (unsafely) access env.resolver |
| 1039 | // directly. |
| 1040 | // |
| 1041 | // TODO(rfindley): fix this after addressing the TODO in the ProcessEnv |
| 1042 | // docstring. |
| 1043 | if _, err := env.GetResolver(); err != nil { |
| 1044 | t.Fatal(err) |
| 1045 | } |
| 1046 | |
| 1047 | return &modTest{ |
| 1048 | T: t, |
| 1049 | gopath: env.Env["GOPATH"], |
| 1050 | env: env, |
| 1051 | cleanup: func() { removeDir(dir) }, |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | // writeModule writes the module in the ar, a txtar, to dir. |
| 1056 | func writeModule(dir, ar string) error { |
| 1057 | a := txtar.Parse([]byte(ar)) |
| 1058 | |
| 1059 | for _, f := range a.Files { |
| 1060 | fpath := filepath.Join(dir, f.Name) |
| 1061 | if err := os.MkdirAll(filepath.Dir(fpath), 0755); err != nil { |
| 1062 | return err |
| 1063 | } |
| 1064 | |
| 1065 | if err := os.WriteFile(fpath, f.Data, 0644); err != nil { |
| 1066 | return err |
| 1067 | } |
| 1068 | } |
| 1069 | return nil |
| 1070 | } |
| 1071 | |
| 1072 | // writeProxy writes all the txtar-formatted modules in arDir to a proxy |
| 1073 | // directory in dir. |
| 1074 | func writeProxy(dir, arDir string) error { |
| 1075 | files, err := os.ReadDir(arDir) |
| 1076 | if err != nil { |
| 1077 | return err |
| 1078 | } |
| 1079 | |
| 1080 | for _, fi := range files { |
| 1081 | if err := writeProxyModule(dir, filepath.Join(arDir, fi.Name())); err != nil { |
| 1082 | return err |
| 1083 | } |
| 1084 | } |
| 1085 | return nil |
| 1086 | } |
| 1087 | |
| 1088 | // writeProxyModule writes a txtar-formatted module at arPath to the module |
| 1089 | // proxy in base. |
| 1090 | func writeProxyModule(base, arPath string) error { |
| 1091 | arName := filepath.Base(arPath) |
| 1092 | i := strings.LastIndex(arName, "_v") |
| 1093 | ver := strings.TrimSuffix(arName[i+1:], ".txt") |
| 1094 | modDir := strings.ReplaceAll(arName[:i], "_", "/") |
| 1095 | modPath, err := module.UnescapePath(modDir) |
| 1096 | if err != nil { |
| 1097 | return err |
| 1098 | } |
| 1099 | |
| 1100 | dir := filepath.Join(base, modDir, "@v") |
| 1101 | a, err := txtar.ParseFile(arPath) |
| 1102 | |
| 1103 | if err != nil { |
| 1104 | return err |
| 1105 | } |
| 1106 | |
| 1107 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 1108 | return err |
| 1109 | } |
| 1110 | |
| 1111 | f, err := os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644) |
| 1112 | if err != nil { |
| 1113 | return err |
| 1114 | } |
| 1115 | z := zip.NewWriter(f) |
| 1116 | for _, f := range a.Files { |
| 1117 | if f.Name[0] == '.' { |
| 1118 | if err := os.WriteFile(filepath.Join(dir, ver+f.Name), f.Data, 0644); err != nil { |
| 1119 | return err |
| 1120 | } |
| 1121 | } else { |
| 1122 | zf, err := z.Create(modPath + "@" + ver + "/" + f.Name) |
| 1123 | if err != nil { |
| 1124 | return err |
| 1125 | } |
| 1126 | if _, err := zf.Write(f.Data); err != nil { |
| 1127 | return err |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | if err := z.Close(); err != nil { |
| 1132 | return err |
| 1133 | } |
| 1134 | if err := f.Close(); err != nil { |
| 1135 | return err |
| 1136 | } |
| 1137 | |
| 1138 | list, err := os.OpenFile(filepath.Join(dir, "list"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
| 1139 | if err != nil { |
| 1140 | return err |
| 1141 | } |
| 1142 | if _, err := fmt.Fprintf(list, "%s\n", ver); err != nil { |
| 1143 | return err |
| 1144 | } |
| 1145 | if err := list.Close(); err != nil { |
| 1146 | return err |
| 1147 | } |
| 1148 | return nil |
| 1149 | } |
| 1150 | |
| 1151 | func removeDir(dir string) { |
| 1152 | _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 1153 | if err != nil { |
| 1154 | return nil |
| 1155 | } |
| 1156 | if info.IsDir() { |
| 1157 | _ = os.Chmod(path, 0777) |
| 1158 | } |
| 1159 | return nil |
| 1160 | }) |
| 1161 | _ = os.RemoveAll(dir) // ignore errors |
| 1162 | } |
| 1163 | |
| 1164 | // Tests that findModFile can find the mod files from a path in the module cache. |
| 1165 | func TestFindModFileModCache(t *testing.T) { |
| 1166 | mt := setup(t, nil, ` |
| 1167 | -- go.mod -- |
| 1168 | module x |
| 1169 | |
| 1170 | require rsc.io/quote v1.5.2 |
| 1171 | -- x.go -- |
| 1172 | package x |
| 1173 | import _ "rsc.io/quote" |
| 1174 | `, "") |
| 1175 | defer mt.cleanup() |
| 1176 | want := filepath.Join(mt.gopath, "pkg/mod", "rsc.io/[email protected]") |
| 1177 | |
| 1178 | found := mt.assertScanFinds("rsc.io/quote", "quote") |
| 1179 | modDir, _ := mt.env.resolver.(*ModuleResolver).modInfo(found.dir) |
| 1180 | if modDir != want { |
| 1181 | t.Errorf("expected: %s, got: %s", want, modDir) |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | // Tests that crud in the module cache is ignored. |
| 1186 | func TestInvalidModCache(t *testing.T) { |
| 1187 | testenv.NeedsTool(t, "go") |
| 1188 | |
| 1189 | dir, err := os.MkdirTemp("", t.Name()) |
| 1190 | if err != nil { |
| 1191 | t.Fatal(err) |
| 1192 | } |
| 1193 | defer removeDir(dir) |
| 1194 | |
| 1195 | // This doesn't have module@version like it should. |
| 1196 | if err := os.MkdirAll(filepath.Join(dir, "gopath/pkg/mod/sabotage"), 0777); err != nil { |
| 1197 | t.Fatal(err) |
| 1198 | } |
| 1199 | if err := os.WriteFile(filepath.Join(dir, "gopath/pkg/mod/sabotage/x.go"), []byte("package foo\n"), 0777); err != nil { |
| 1200 | t.Fatal(err) |
| 1201 | } |
| 1202 | env := &ProcessEnv{ |
| 1203 | Env: map[string]string{ |
| 1204 | "GOPATH": filepath.Join(dir, "gopath"), |
| 1205 | "GO111MODULE": "on", |
| 1206 | "GOSUMDB": "off", |
| 1207 | }, |
| 1208 | GocmdRunner: &gocommand.Runner{}, |
| 1209 | WorkingDir: dir, |
| 1210 | } |
| 1211 | resolver, err := env.GetResolver() |
| 1212 | if err != nil { |
| 1213 | t.Fatal(err) |
| 1214 | } |
| 1215 | scanToSlice(resolver, nil) |
| 1216 | } |
| 1217 | |
| 1218 | func TestGetCandidatesRanking(t *testing.T) { |
| 1219 | mt := setup(t, nil, ` |
| 1220 | -- go.mod -- |
| 1221 | module example.com |
| 1222 | |
| 1223 | require rsc.io/quote v1.5.1 |
| 1224 | require rsc.io/quote/v3 v3.0.0 |
| 1225 | |
| 1226 | -- rpackage/x.go -- |
| 1227 | package rpackage |
| 1228 | import ( |
| 1229 | _ "rsc.io/quote" |
| 1230 | _ "rsc.io/quote/v3" |
| 1231 | ) |
| 1232 | `, "") |
| 1233 | defer mt.cleanup() |
| 1234 | |
| 1235 | if _, err := mt.env.invokeGo(context.Background(), "mod", "download", "rsc.io/quote/[email protected]"); err != nil { |
| 1236 | t.Fatal(err) |
| 1237 | } |
| 1238 | |
| 1239 | type res struct { |
| 1240 | relevance float64 |
| 1241 | name, path string |
| 1242 | } |
| 1243 | want := []res{ |
| 1244 | // Stdlib |
| 1245 | {7, "bytes", "bytes"}, |
| 1246 | {7, "http", "net/http"}, |
| 1247 | // Main module |
| 1248 | {6, "rpackage", "example.com/rpackage"}, |
| 1249 | // Direct module deps with v2+ major version |
| 1250 | {5.003, "quote", "rsc.io/quote/v3"}, |
| 1251 | // Direct module deps |
| 1252 | {5, "quote", "rsc.io/quote"}, |
| 1253 | // Indirect deps |
| 1254 | {4, "language", "golang.org/x/text/language"}, |
| 1255 | // Out of scope modules |
| 1256 | {3, "quote", "rsc.io/quote/v2"}, |
| 1257 | } |
| 1258 | var mu sync.Mutex |
| 1259 | var got []res |
| 1260 | add := func(c ImportFix) { |
| 1261 | mu.Lock() |
| 1262 | defer mu.Unlock() |
| 1263 | for _, w := range want { |
| 1264 | if c.StmtInfo.ImportPath == w.path { |
| 1265 | got = append(got, res{c.Relevance, c.IdentName, c.StmtInfo.ImportPath}) |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | if err := GetAllCandidates(context.Background(), add, "", "foo.go", "foo", mt.env); err != nil { |
| 1270 | t.Fatalf("getAllCandidates() = %v", err) |
| 1271 | } |
| 1272 | sort.Slice(got, func(i, j int) bool { |
| 1273 | ri, rj := got[i], got[j] |
| 1274 | if ri.relevance != rj.relevance { |
| 1275 | return ri.relevance > rj.relevance // Highest first. |
| 1276 | } |
| 1277 | return ri.name < rj.name |
| 1278 | }) |
| 1279 | if !reflect.DeepEqual(want, got) { |
| 1280 | t.Errorf("wanted candidates in order %v, got %v", want, got) |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | func BenchmarkModuleResolver_RescanModCache(b *testing.B) { |
| 1285 | env := &ProcessEnv{ |
| 1286 | GocmdRunner: &gocommand.Runner{}, |
| 1287 | // Uncomment for verbose logging (too verbose to enable by default). |
| 1288 | // Logf: b.Logf, |
| 1289 | } |
| 1290 | exclude := []gopathwalk.RootType{gopathwalk.RootGOROOT} |
| 1291 | resolver, err := env.GetResolver() |
| 1292 | if err != nil { |
| 1293 | b.Fatal(err) |
| 1294 | } |
| 1295 | start := time.Now() |
| 1296 | scanToSlice(resolver, exclude) |
| 1297 | b.Logf("warming the mod cache took %v", time.Since(start)) |
| 1298 | |
| 1299 | for b.Loop() { |
| 1300 | scanToSlice(resolver, exclude) |
| 1301 | resolver = resolver.ClearForNewScan() |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | func BenchmarkModuleResolver_InitialScan(b *testing.B) { |
| 1306 | for b.Loop() { |
| 1307 | env := &ProcessEnv{ |
| 1308 | GocmdRunner: &gocommand.Runner{}, |
| 1309 | } |
| 1310 | exclude := []gopathwalk.RootType{gopathwalk.RootGOROOT} |
| 1311 | resolver, err := env.GetResolver() |
| 1312 | if err != nil { |
| 1313 | b.Fatal(err) |
| 1314 | } |
| 1315 | scanToSlice(resolver, exclude) |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | // Tests that go.work files and vendor directory are respected. |
| 1320 | func TestModWorkspaceVendoring(t *testing.T) { |
| 1321 | mt := setup(t, nil, ` |
| 1322 | -- go.work -- |
| 1323 | go 1.22 |
| 1324 | |
| 1325 | use ( |
| 1326 | ./a |
| 1327 | ./b |
| 1328 | ) |
| 1329 | -- a/go.mod -- |
| 1330 | module example.com/a |
| 1331 | |
| 1332 | go 1.22 |
| 1333 | |
| 1334 | require rsc.io/sampler v1.3.1 |
| 1335 | -- a/a.go -- |
| 1336 | package a |
| 1337 | |
| 1338 | import _ "rsc.io/sampler" |
| 1339 | -- b/go.mod -- |
| 1340 | module example.com/b |
| 1341 | |
| 1342 | go 1.22 |
| 1343 | -- b/b.go -- |
| 1344 | package b |
| 1345 | `, "") |
| 1346 | defer mt.cleanup() |
| 1347 | |
| 1348 | // generate vendor directory |
| 1349 | if _, err := mt.env.invokeGo(context.Background(), "work", "vendor"); err != nil { |
| 1350 | t.Fatal(err) |
| 1351 | } |
| 1352 | |
| 1353 | // update module resolver |
| 1354 | mt.env.ClearModuleInfo() |
| 1355 | mt.env.UpdateResolver(mt.env.resolver.ClearForNewScan()) |
| 1356 | |
| 1357 | mt.assertModuleFoundInDir("example.com/a", "a", `main/a$`) |
| 1358 | mt.assertScanFinds("example.com/a", "a") |
| 1359 | mt.assertModuleFoundInDir("example.com/b", "b", `main/b$`) |
| 1360 | mt.assertScanFinds("example.com/b", "b") |
| 1361 | mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `/vendor/`) |
| 1362 | } |