From 9bf174b4d3fab31065b140e4a69c6e6b17746806 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 12 Apr 2017 06:51:24 -0700 Subject: [PATCH] cmd/stringer: use source importer when available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means that running stringer should always have the intended effect, without having to go install the package first, which was a common source of confusion. The source importer is marginally slower, but stringer is run infrequently, and we're only typechecking one package (and fmt), not an entire tree, as vet does. Fixes golang/go#10249 Change-Id: Ib8cde29bd6cc596964dbe7348065932dd59075fc Reviewed-on: https://go-review.googlesource.com/40403 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Daniel Martí Reviewed-by: Robert Griesemer --- cmd/stringer/endtoend_test.go | 2 +- cmd/stringer/importer18.go | 16 ++++++++++++++++ cmd/stringer/importer19.go | 16 ++++++++++++++++ cmd/stringer/stringer.go | 3 +-- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 cmd/stringer/importer18.go create mode 100644 cmd/stringer/importer19.go diff --git a/cmd/stringer/endtoend_test.go b/cmd/stringer/endtoend_test.go index d71a6c13..a1db27ae 100644 --- a/cmd/stringer/endtoend_test.go +++ b/cmd/stringer/endtoend_test.go @@ -33,7 +33,7 @@ func TestEndToEnd(t *testing.T) { defer os.RemoveAll(dir) // Create stringer in temporary directory. stringer := filepath.Join(dir, "stringer.exe") - err = run("go", "build", "-o", stringer, "stringer.go") + err = run("go", "build", "-o", stringer) if err != nil { t.Fatalf("building stringer: %s", err) } diff --git a/cmd/stringer/importer18.go b/cmd/stringer/importer18.go new file mode 100644 index 00000000..fd21873c --- /dev/null +++ b/cmd/stringer/importer18.go @@ -0,0 +1,16 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !go1.9 + +package main + +import ( + "go/importer" + "go/types" +) + +func defaultImporter() types.Importer { + return importer.Default() +} diff --git a/cmd/stringer/importer19.go b/cmd/stringer/importer19.go new file mode 100644 index 00000000..deddadb1 --- /dev/null +++ b/cmd/stringer/importer19.go @@ -0,0 +1,16 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build go1.9 + +package main + +import ( + "go/importer" + "go/types" +) + +func defaultImporter() types.Importer { + return importer.For("source", nil) +} diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go index 78d7299b..f741d98c 100644 --- a/cmd/stringer/stringer.go +++ b/cmd/stringer/stringer.go @@ -66,7 +66,6 @@ import ( "go/build" exact "go/constant" "go/format" - "go/importer" "go/parser" "go/token" "go/types" @@ -258,7 +257,7 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac // check type-checks the package. The package must be OK to proceed. func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { pkg.defs = make(map[*ast.Ident]types.Object) - config := types.Config{Importer: importer.Default(), FakeImportC: true} + config := types.Config{Importer: defaultImporter(), FakeImportC: true} info := &types.Info{ Defs: pkg.defs, }