From 7f992c627d3c76b941870c524f210c4d256ff0ab Mon Sep 17 00:00:00 2001 From: Carl Sverre Date: Tue, 28 Nov 2017 15:31:38 -0800 Subject: [PATCH 1/2] import from source --- stringer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringer.go b/stringer.go index ccfb304..a9273e9 100644 --- a/stringer.go +++ b/stringer.go @@ -273,7 +273,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: importer.For("source", nil), FakeImportC: true} info := &types.Info{ Defs: pkg.defs, } From e5b99fedb3a60c7d2b14cfd7080b5aaff30f6807 Mon Sep 17 00:00:00 2001 From: Carl Sverre Date: Tue, 28 Nov 2017 15:42:24 -0800 Subject: [PATCH 2/2] Maintain go 1.8 compat --- endtoend_test.go | 2 +- importer18.go | 16 ++++++++++++++++ importer19.go | 16 ++++++++++++++++ stringer.go | 3 +-- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 importer18.go create mode 100644 importer19.go diff --git a/endtoend_test.go b/endtoend_test.go index 40c91f7..3397c06 100644 --- a/endtoend_test.go +++ b/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, "enumer.go", "sql.go", "stringer.go", "transformer.go") + err = run("go", "build", "-o", stringer) if err != nil { t.Fatalf("building stringer: %s", err) } diff --git a/importer18.go b/importer18.go new file mode 100644 index 0000000..fd21873 --- /dev/null +++ b/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/importer19.go b/importer19.go new file mode 100644 index 0000000..deddadb --- /dev/null +++ b/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/stringer.go b/stringer.go index a9273e9..f44a21c 100644 --- a/stringer.go +++ b/stringer.go @@ -68,7 +68,6 @@ import ( "go/build" exact "go/constant" "go/format" - "go/importer" "go/parser" "go/token" "go/types" @@ -273,7 +272,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.For("source", nil), FakeImportC: true} + config := types.Config{Importer: defaultImporter(), FakeImportC: true} info := &types.Info{ Defs: pkg.defs, }