Skip to content

Commit

Permalink
add test case for ImportMap support (#186)
Browse files Browse the repository at this point in the history
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 Binject/debug#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.
  • Loading branch information
mvdan authored Nov 8, 2020
1 parent 1336711 commit 3e7416e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
33 changes: 22 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions testdata/scripts/goprivate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 3e7416e

Please sign in to comment.