Skip to content

Commit

Permalink
cmd/stringer: use source importer when available
Browse files Browse the repository at this point in the history
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 <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
josharian committed Apr 12, 2017
1 parent 851a2d8 commit b8b9109
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions importer18.go
Original file line number Diff line number Diff line change
@@ -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()
}
16 changes: 16 additions & 0 deletions importer19.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import (
"go/build"
exact "go/constant"
"go/format"
"go/importer"
"go/parser"
"go/token"
"go/types"
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit b8b9109

Please sign in to comment.