]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: allow 'go generate' even if imports do not resolve
authorRuss Cox <rsc@golang.org>
Fri, 21 Oct 2016 15:37:54 +0000 (11:37 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 24 Oct 2016 15:17:32 +0000 (15:17 +0000)
Maybe the go generate is generating the imports,
or maybe there's some other good reason the code
is incomplete.

The help text already says:

Note that go generate does not parse the file, so lines that look
like directives in comments or multiline strings will be treated
as directives.

We'll still reject Go source files that don't begin with a package statement
or have a syntax error in the import block, but those are I think more
defensible rejections.

Fixes #16307.

Change-Id: I4f8496c02fdff993f038adfed2df4db7f067dc06
Reviewed-on: https://go-review.googlesource.com/31659
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/go/generate.go
src/cmd/go/go_test.go
src/cmd/go/pkg.go
src/cmd/go/testdata/src/gencycle/gencycle.go [new file with mode: 0644]

index d5b955af9215df8dc8384368e049491614fb6b21..2d92a0c100ca169f21afeb9a91bef16e579832eb 100644 (file)
@@ -136,6 +136,8 @@ func init() {
 }
 
 func runGenerate(cmd *Command, args []string) {
+       ignoreImports = true
+
        if generateRunFlag != "" {
                var err error
                generateRunRE, err = regexp.Compile(generateRunFlag)
index f445aef5bd05320d1c718e47faaefe195ad6ac9c..2f883c94df69c7e464dfebff6491d577aa7c981f 100644 (file)
@@ -2349,6 +2349,20 @@ func TestGoGenerateEnv(t *testing.T) {
        }
 }
 
+func TestGoGenerateBadImports(t *testing.T) {
+       if runtime.GOOS == "windows" {
+               t.Skip("skipping because windows has no echo command")
+       }
+
+       // This package has an invalid import causing an import cycle,
+       // but go generate is supposed to still run.
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
+       tg.run("generate", "gencycle")
+       tg.grepStdout("hello world", "go generate gencycle did not run generator")
+}
+
 func TestGoGetCustomDomainWildcard(t *testing.T) {
        testenv.MustHaveExternalNetwork(t)
 
index a3018bce453de48c2c494a57f633530ab0c5969e..2f5e90faf42a770a49e44ae1e8c5907aa1ff5968 100644 (file)
@@ -24,6 +24,8 @@ import (
        "unicode"
 )
 
+var ignoreImports bool // control whether we ignore imports in packages
+
 // A Package describes a single package found in a directory.
 type Package struct {
        // Note: These fields are part of the go command's public API.
@@ -181,6 +183,11 @@ func (p *Package) copyBuild(pp *build.Package) {
        p.TestImports = pp.TestImports
        p.XTestGoFiles = pp.XTestGoFiles
        p.XTestImports = pp.XTestImports
+       if ignoreImports {
+               p.Imports = nil
+               p.TestImports = nil
+               p.XTestImports = nil
+       }
 }
 
 // isStandardImportPath reports whether $GOROOT/src/path should be considered
diff --git a/src/cmd/go/testdata/src/gencycle/gencycle.go b/src/cmd/go/testdata/src/gencycle/gencycle.go
new file mode 100644 (file)
index 0000000..600afd9
--- /dev/null
@@ -0,0 +1,5 @@
+//go:generate echo hello world
+
+package gencycle
+
+import _ "gencycle"