use real package paths when typechecking

'go tool compile' receives the package path via the -p flag. This is
better than making up one.

We have to be careful with "-p main" though, as that's not part of the
standard library.
pull/22/head
Daniel Martí 5 years ago
parent b6d889cdcf
commit c23f529830

@ -222,7 +222,8 @@ func transformCompile(args []string) ([]string, error) {
Defs: make(map[*ast.Ident]types.Object), Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object), Uses: make(map[*ast.Ident]types.Object),
} }
if _, err := typesConfig.Check("current.pkg/path", fset, files, info); err != nil { pkgPath := flagValue(flags, "-p")
if _, err := typesConfig.Check(pkgPath, fset, files, info); err != nil {
return nil, fmt.Errorf("typecheck error: %v", err) return nil, fmt.Errorf("typecheck error: %v", err)
} }
@ -379,7 +380,7 @@ func transformGo(node ast.Node, info *types.Info) ast.Node {
return true // universe scope return true // universe scope
} }
path := pkg.Path() path := pkg.Path()
if !strings.Contains(path, ".") { if isStandardLibrary(path) {
return true // std isn't transformed return true // std isn't transformed
} }
if id := buildInfo.imports[path].buildID; id != "" { if id := buildInfo.imports[path].buildID; id != "" {
@ -393,6 +394,16 @@ func transformGo(node ast.Node, info *types.Info) ast.Node {
return astutil.Apply(node, pre, nil) return astutil.Apply(node, pre, nil)
} }
func isStandardLibrary(path string) bool {
switch path {
case "main":
// Main packages may not have fully qualified import paths, but
// they're not part of the standard library
return false
}
return !strings.Contains(path, ".")
}
func transformLink(args []string) ([]string, error) { func transformLink(args []string) ([]string, error) {
flags, paths := splitFlagsFromFiles(args, ".a") flags, paths := splitFlagsFromFiles(args, ".a")
if len(paths) == 0 { if len(paths) == 0 {

Loading…
Cancel
Save