-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: overcoming srcimporter issues (#288)
See go-critic/go-critic#1126 We're using the host-specific `go env` info to avoid incorrect GOROOT and other Go build context variables.
- Loading branch information
Showing
9 changed files
with
165 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"go/build" | ||
"log" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/quasilyte/go-ruleguard/analyzer" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
func main() { | ||
// If we don't do this, release binaries will have GOROOT set | ||
// to the `go env GOROOT` of the machine that built them. | ||
// | ||
// Usually, it doesn't matter, but since we're using "source" | ||
// importers, it *will* use build.Default.GOROOT to locate packages. | ||
// | ||
// Example: release binary was built with GOROOT="/foo/bar/go", | ||
// user has GOROOT at "/usr/local/go"; if we don't adjust GOROOT | ||
// field here, it'll be "/foo/bar/go". | ||
build.Default.GOROOT = hostGOROOT() | ||
|
||
singlechecker.Main(analyzer.Analyzer) | ||
} | ||
|
||
func hostGOROOT() string { | ||
// `go env GOROOT` should return the correct value even | ||
// if it was overwritten by explicit GOROOT env var. | ||
out, err := exec.Command("go", "env", "GOROOT").CombinedOutput() | ||
if err != nil { | ||
log.Fatalf("infer GOROOT: %v: %s", err, out) | ||
} | ||
return strings.TrimSpace(string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package xsrcimporter | ||
|
||
import ( | ||
"go/build" | ||
"go/importer" | ||
"go/token" | ||
"go/types" | ||
"unsafe" | ||
) | ||
|
||
func New(ctxt *build.Context, fset *token.FileSet) types.Importer { | ||
imp := importer.ForCompiler(fset, "source", nil) | ||
ifaceVal := *(*iface)(unsafe.Pointer(&imp)) | ||
srcImp := (*srcImporter)(ifaceVal.data) | ||
srcImp.ctxt = ctxt | ||
return imp | ||
} | ||
|
||
type iface struct { | ||
_ *byte | ||
data unsafe.Pointer | ||
} | ||
|
||
type srcImporter struct { | ||
ctxt *build.Context | ||
_ *token.FileSet | ||
_ types.Sizes | ||
_ map[string]*types.Package | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package xsrcimporter | ||
|
||
import ( | ||
"go/build" | ||
"go/importer" | ||
"go/token" | ||
"reflect" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
func TestSize(t *testing.T) { | ||
fset := token.NewFileSet() | ||
imp := importer.ForCompiler(fset, "source", nil) | ||
have := unsafe.Sizeof(srcImporter{}) | ||
want := reflect.ValueOf(imp).Elem().Type().Size() | ||
if have != want { | ||
t.Errorf("sizes mismatch: have %d want %d", have, want) | ||
} | ||
} | ||
|
||
func TestImport(t *testing.T) { | ||
fset := token.NewFileSet() | ||
imp := New(&build.Default, fset) | ||
|
||
packages := []string{ | ||
"errors", | ||
"fmt", | ||
"encoding/json", | ||
} | ||
|
||
for _, pkgPath := range packages { | ||
pkg, err := imp.Import(pkgPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if pkg.Path() != pkgPath { | ||
t.Fatalf("%s: pkg path mismatch (got %s)", pkgPath, pkg.Path()) | ||
} | ||
if !pkg.Complete() { | ||
t.Fatalf("%s is incomplete", pkgPath) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.