add test case for ImportMap support (#186)

We also update the "original types importer" to support ImportMap.

The test now gets further along, no longer getting stuck on "path not
found in listed packages". Instead, we get stuck on:

	error parsing importcfg: <...>/importcfg:2: unknown directive "importmap"

This bug has been filed at https://github.com/Binject/debug/issues/17.
Until it's fixed, we can't really proceed on #146, so the net import in
the test file (which triggers this case) is commented out for now.

Updates #146.
pull/191/head
Daniel Martí 5 years ago committed by GitHub
parent 1336711c9c
commit 3e7416ee9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -90,16 +90,18 @@ var (
b64 = base64.NewEncoding(nameCharset) b64 = base64.NewEncoding(nameCharset)
printConfig = printer.Config{Mode: printer.RawFormat} printConfig = printer.Config{Mode: printer.RawFormat}
// origTypesConfig configures a go/types typechecker which uses the // origImporter configures a go/types importer which uses the original
// original versions of packages, without any obfuscation. This is // versions of packages, without any obfuscation. This is helpful to
// helpful to make decisions on how to obfuscate our input code. // make decisions on how to obfuscate our input code.
origTypesConfig = types.Config{Importer: importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) { origImporter = func(fromPkg string) types.Importer {
pkg, err := listPackage(path) return importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) {
if err != nil { pkg, err := listPackage(fromPkg, path)
return nil, err if err != nil {
} return nil, err
return os.Open(pkg.Export) }
})} return os.Open(pkg.Export)
})
}
buildInfo = struct { buildInfo = struct {
actionID []byte // from -buildid actionID []byte // from -buildid
@ -183,12 +185,13 @@ type listedPackage struct {
ImportPath string ImportPath string
Export string Export string
Deps []string Deps []string
ImportMap map[string]string
// TODO(mvdan): reuse this field once TOOLEXEC_IMPORTPATH is used // TODO(mvdan): reuse this field once TOOLEXEC_IMPORTPATH is used
private bool private bool
} }
func listPackage(path string) (*listedPackage, error) { func listPackage(fromPath, path string) (*listedPackage, error) {
if listedPackages == nil { if listedPackages == nil {
f, err := os.Open(envGarbleListPkgs) f, err := os.Open(envGarbleListPkgs)
if err != nil { if err != nil {
@ -201,6 +204,11 @@ func listPackage(path string) (*listedPackage, error) {
} }
pkg, ok := listedPackages[path] pkg, ok := listedPackages[path]
if !ok { if !ok {
if fromPkg, ok := listedPackages[fromPath]; ok {
if path2 := fromPkg.ImportMap[path]; path2 != "" {
return listPackage(fromPath, path2)
}
}
return nil, fmt.Errorf("path not found in listed packages: %s", path) return nil, fmt.Errorf("path not found in listed packages: %s", path)
} }
return pkg, nil return pkg, nil
@ -636,6 +644,9 @@ func transformCompile(args []string) ([]string, error) {
}, },
} }
origTypesConfig := types.Config{
Importer: origImporter(pkgPath),
}
tf.pkg, err = origTypesConfig.Check(pkgPath, fset, files, tf.info) tf.pkg, err = origTypesConfig.Check(pkgPath, fset, files, tf.info)
if err != nil { if err != nil {
return nil, fmt.Errorf("typecheck error: %v", err) return nil, fmt.Errorf("typecheck error: %v", err)

@ -8,6 +8,9 @@ stderr '^public package "test/main/importer" can''t depend on obfuscated package
[short] stop [short] stop
# Try garbling all of std.
# This used to fail since the "net" import causes 'go list -json' to output
# ImportMap, since "net" imports packages vendored in std.
env GOPRIVATE='*' env GOPRIVATE='*'
garble build -o=out ./standalone garble build -o=out ./standalone
@ -18,6 +21,9 @@ go 1.15
-- standalone/main.go -- -- standalone/main.go --
package main package main
// Blocked until https://github.com/Binject/debug/issues/17 is fixed.
// import _ "net"
func main() {} func main() {}
-- importer/importer.go -- -- importer/importer.go --
package importer package importer

Loading…
Cancel
Save