Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test case for ImportMap support #186

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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