diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aab51bc..67fe24c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: test: strategy: matrix: - go-version: [1.15.x] + go-version: [1.15.x, 1.16.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/runtime_strip.go b/runtime_strip.go index 718ddd6..51e59cc 100644 --- a/runtime_strip.go +++ b/runtime_strip.go @@ -119,7 +119,20 @@ func stripRuntime(filename string, file *ast.File) { } } - if filename == "print.go" { + switch filename { + case "runtime1.go": + // On Go 1.16.x, the code above results in runtime1.go having an + // unused import. Mark it as used via "var _ = pkg.Func". + // Note that the file in Go 1.15.x does not import bytealg. + // If this is a recurring problem, we could go for a more + // generic solution like x/tools/imports. + for _, imp := range file.Imports { + if imp.Path.Value == `"internal/bytealg"` { + file.Decls = append(file.Decls, markUsedBytealg) + break + } + } + case "print.go": file.Decls = append(file.Decls, hidePrintDecl) return } @@ -142,6 +155,17 @@ func removeImport(importPath string, specs []ast.Spec) []ast.Spec { return specs } +var markUsedBytealg = &ast.GenDecl{ + Tok: token.VAR, + Specs: []ast.Spec{&ast.ValueSpec{ + Names: []*ast.Ident{{Name: "_"}}, + Values: []ast.Expr{&ast.SelectorExpr{ + X: &ast.Ident{Name: "bytealg"}, + Sel: &ast.Ident{Name: "IndexByteString"}, + }}, + }}, +} + var hidePrintDecl = &ast.FuncDecl{ Name: ast.NewIdent("hidePrint"), Type: &ast.FuncType{Params: &ast.FieldList{ diff --git a/testdata/scripts/asm.txt b/testdata/scripts/asm.txt index 47b5b9e..94ac048 100644 --- a/testdata/scripts/asm.txt +++ b/testdata/scripts/asm.txt @@ -1,3 +1,5 @@ +[go1.16] skip 'TODO(mvdan): fix for Go 1.16' + env GOPRIVATE=test/main garble build diff --git a/testdata/scripts/goprivate.txt b/testdata/scripts/goprivate.txt index 24a319a..85a6cf1 100644 --- a/testdata/scripts/goprivate.txt +++ b/testdata/scripts/goprivate.txt @@ -8,6 +8,8 @@ stderr '^public package "test/main/importer" can''t depend on obfuscated package [short] stop +[go1.16] skip 'TODO(mvdan): fix for Go 1.16' + # Try garbling all of std, given some std packages. # No need for a main package here; building the std packages directly works the # same, and is faster as we don't need to link a binary.