]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go/internal/modcmd: do not copy test embed files when vendoring
authorQuan Tong <quantonganh@gmail.com>
Wed, 11 Oct 2023 01:55:24 +0000 (01:55 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 18 Oct 2023 19:30:00 +0000 (19:30 +0000)
Currently, go mod vendor copy all files matched by //go:embed,
even when it is in a _test.go file. According to the documentation,
it should not include test code for vendored packages.

Fixes #63473

Change-Id: I28c411724fc7b7aeb683857fc114e20b08635e01
Reviewed-on: https://go-review.googlesource.com/c/go/+/534376
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/testdata/script/mod_vendor_embed.txt

index 033d47b10468193a4e3795679004033de0573dd2..e95dfbfbb7d9582c5a5cd5587e06ddd3e4ea9185 100644 (file)
@@ -313,7 +313,15 @@ func vendorPkg(vdir, pkg string) {
                        base.Fatalf("internal error: failed to find embedded files of %s: %v\n", pkg, err)
                }
        }
-       embedPatterns := str.StringList(bp.EmbedPatterns, bp.TestEmbedPatterns, bp.XTestEmbedPatterns)
+       var embedPatterns []string
+       if gover.Compare(modload.MainModules.GoVersion(), "1.22") >= 0 {
+               embedPatterns = bp.EmbedPatterns
+       } else {
+               // Maintain the behavior of https://github.com/golang/go/issues/63473
+               // so that we continue to agree with older versions of the go command
+               // about the contents of vendor directories in existing modules
+               embedPatterns = str.StringList(bp.EmbedPatterns, bp.TestEmbedPatterns, bp.XTestEmbedPatterns)
+       }
        embeds, err := load.ResolveEmbed(bp.Dir, embedPatterns)
        if err != nil {
                base.Fatal(err)
index b14fd9915646f38ac0db6239f2740fb0f8b1cbb4..1a3b2fef26d3ec2a8d2d2edc4e1ba506dff4bda4 100644 (file)
@@ -12,6 +12,36 @@ cd ../broken_bad_pattern
 ! go mod vendor
 stderr 'go: pattern ../foo.txt: invalid pattern syntax'
 
+cd ../embed_go122
+go mod vendor
+cmp vendor/example.com/a/samedir_embed.txt ../a/samedir_embed.txt
+cmp vendor/example.com/a/subdir/embed.txt ../a/subdir/embed.txt
+! exists vendor/example.com/a/subdir/test/embed.txt
+! exists vendor/example.com/a/subdir/test/xtest/embed.txt
+-- embed_go122/go.mod --
+module example.com/foo
+go 1.22
+
+require (
+       example.com/a v0.1.0
+)
+
+replace (
+       example.com/a v0.1.0 => ../a
+)
+-- embed_go122/foo.go --
+package main
+
+import (
+       "fmt"
+
+       "example.com/a"
+)
+
+func main() {
+    fmt.Println(a.Str())
+}
+
 # matchPotentialSourceFile prunes out tests and unbuilt code.
 # Make sure that they are vendored if they are embedded files.
 cd ../embed_unbuilt