Skip to content

Commit ba8c33d

Browse files
committed
cmd/stringer: use source importer when available
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 3425bcc commit ba8c33d

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

endtoend_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestEndToEnd(t *testing.T) {
3333
defer os.RemoveAll(dir)
3434
// Create stringer in temporary directory.
3535
stringer := filepath.Join(dir, "stringer.exe")
36-
err = run("go", "build", "-o", stringer, "stringer.go")
36+
err = run("go", "build", "-o", stringer)
3737
if err != nil {
3838
t.Fatalf("building stringer: %s", err)
3939
}

importer18.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 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+
// +build !go1.9
6+
7+
package main
8+
9+
import (
10+
"go/importer"
11+
"go/types"
12+
)
13+
14+
func defaultImporter() types.Importer {
15+
return importer.Default()
16+
}

importer19.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 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+
// +build go1.9
6+
7+
package main
8+
9+
import (
10+
"go/importer"
11+
"go/types"
12+
)
13+
14+
func defaultImporter() types.Importer {
15+
return importer.For("source", nil)
16+
}

stringer.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import (
6666
"go/build"
6767
exact "go/constant"
6868
"go/format"
69-
"go/importer"
7069
"go/parser"
7170
"go/token"
7271
"go/types"
@@ -258,7 +257,7 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac
258257
// check type-checks the package. The package must be OK to proceed.
259258
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
260259
pkg.defs = make(map[*ast.Ident]types.Object)
261-
config := types.Config{Importer: importer.Default(), FakeImportC: true}
260+
config := types.Config{Importer: defaultImporter(), FakeImportC: true}
262261
info := &types.Info{
263262
Defs: pkg.defs,
264263
}

0 commit comments

Comments
 (0)