From 7d3bb4af5db10c6f5fed27d2ec9b025f668f94dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 8 Nov 2020 12:35:35 +0000 Subject: [PATCH] add test case for ImportMap support 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. --- main.go | 33 ++++++++++++++++++++++----------- testdata/scripts/goprivate.txt | 6 ++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index df9cc153..596f34c1 100644 --- a/main.go +++ b/main.go @@ -90,16 +90,18 @@ var ( b64 = base64.NewEncoding(nameCharset) printConfig = printer.Config{Mode: printer.RawFormat} - // origTypesConfig configures a go/types typechecker which uses the - // original versions of packages, without any obfuscation. This is - // helpful to make decisions on how to obfuscate our input code. - origTypesConfig = types.Config{Importer: importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) { - pkg, err := listPackage(path) - if err != nil { - return nil, err - } - return os.Open(pkg.Export) - })} + // origImporter configures a go/types importer which uses the original + // versions of packages, without any obfuscation. This is helpful to + // make decisions on how to obfuscate our input code. + origImporter = func(fromPkg string) types.Importer { + return importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) { + pkg, err := listPackage(fromPkg, path) + if err != nil { + return nil, err + } + return os.Open(pkg.Export) + }) + } buildInfo = struct { actionID []byte // from -buildid @@ -183,12 +185,13 @@ type listedPackage struct { ImportPath string Export string Deps []string + ImportMap map[string]string // TODO(mvdan): reuse this field once TOOLEXEC_IMPORTPATH is used private bool } -func listPackage(path string) (*listedPackage, error) { +func listPackage(fromPath, path string) (*listedPackage, error) { if listedPackages == nil { f, err := os.Open(envGarbleListPkgs) if err != nil { @@ -201,6 +204,11 @@ func listPackage(path string) (*listedPackage, error) { } pkg, ok := listedPackages[path] 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 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) if err != nil { return nil, fmt.Errorf("typecheck error: %v", err) diff --git a/testdata/scripts/goprivate.txt b/testdata/scripts/goprivate.txt index 6f41eaf3..4b79e1af 100644 --- a/testdata/scripts/goprivate.txt +++ b/testdata/scripts/goprivate.txt @@ -8,6 +8,9 @@ stderr '^public package "test/main/importer" can''t depend on obfuscated package [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='*' garble build -o=out ./standalone @@ -18,6 +21,9 @@ go 1.15 -- standalone/main.go -- package main +// Blocked until https://github.com/Binject/debug/issues/17 is fixed. +// import _ "net" + func main() {} -- importer/importer.go -- package importer