fix support with the latest Go master version

It added packages which are only built with the boringcrypto build tag,
so trying to `go list` them will fail even though it doesn't matter.

While here, a few more minor cleanups:

1) Hide GarbleActionID and ToObfuscate from encoding/json, so that they
   can't possibly collide with the fields consumed from `go list -json`.

2) Add test cases for `garble build` with packages that fail to load.
   Note that this requires GOGARBLE=* to avoid its "does not match any
   package to be built" error.

3) Remove the last use of interface{}, in a testdata file.

Fixes #531.
pull/540/head
Daniel Martí 2 years ago committed by lu4p
parent 84ba444b7c
commit 7fb390e403

@ -71,8 +71,7 @@ jobs:
steps:
- name: Install Go
env:
# Note that newer tip versions from May break due to the boringtls merge.
GO_COMMIT: e6dfdbd11d5dcc9f918150552f50ca6cd524b89b # 2022-04-23
GO_COMMIT: 016d7552138077741a9c3fdadc73c0179f5d3ff7 # 2022-05-12
run: |
cd $HOME
mkdir $HOME/gotip

@ -139,9 +139,10 @@ type listedPackage struct {
ImportMap map[string]string
Standard bool
Dir string
GoFiles []string
Imports []string
Dir string
GoFiles []string
Imports []string
Incomplete bool
// The fields below are not part of 'go list', but are still reused
// between garble processes. Use "Garble" as a prefix to ensure no
@ -149,10 +150,10 @@ type listedPackage struct {
// TODO(mvdan): consider filling this iff ToObfuscate==true,
// which will help ensure we don't obfuscate any of their names otherwise.
GarbleActionID []byte
GarbleActionID []byte `json:"-"`
// ToObfuscate records whether the package should be obfuscated.
ToObfuscate bool
ToObfuscate bool `json:"-"`
}
func (p *listedPackage) obfuscatedImportPath() string {
@ -176,7 +177,7 @@ func appendListedPackages(packages []string, withDeps bool) error {
// TODO: perhaps include all top-level build flags set by garble,
// including -buildvcs=false.
// They shouldn't affect "go list" here, but might as well be consistent.
args := []string{"list", "-json", "-export", "-trimpath"}
args := []string{"list", "-json", "-export", "-trimpath", "-e"}
if withDeps {
args = append(args, "-deps")
}
@ -209,6 +210,13 @@ func appendListedPackages(packages []string, withDeps bool) error {
if err := dec.Decode(&pkg); err != nil {
return err
}
// Note that we use the `-e` flag above with `go list`.
// If a package fails to load, the Incomplete and Error fields will be set.
// We still record failed packages in the ListedPackages map,
// because some like crypto/internal/boring/fipstls simply fall under
// "build constraints exclude all Go files" and can be ignored.
// Real build errors will still be surfaced by `go build -toolexec` later.
if cache.ListedPackages[pkg.ImportPath] != nil {
return fmt.Errorf("duplicate package: %q", pkg.ImportPath)
}
@ -235,6 +243,9 @@ func appendListedPackages(packages []string, withDeps bool) error {
case cannotObfuscate[path], runtimeAndDeps[path]:
// We don't support obfuscating these yet.
case pkg.Incomplete:
// We can't obfuscate packages which weren't loaded.
case pkg.Name == "main" && strings.HasSuffix(path, ".test"),
path == "command-line-arguments",
strings.HasPrefix(path, "plugin/unnamed"),
@ -335,6 +346,8 @@ func listPackage(path string) (*listedPackage, error) {
startTime := time.Now()
// Obtained via scripts/runtime-linknamed-nodeps.sh as of Go 1.18beta1.
runtimeLinknamed := []string{
"crypto/internal/boring",
"crypto/internal/boring/fipstls",
"crypto/x509/internal/macos",
"internal/poll",
"internal/reflectlite",

@ -1,3 +1,5 @@
env GOGARBLE=*
! garble
stderr 'Garble obfuscates Go code'
stderr 'garble \[garble flags\] command'
@ -77,6 +79,14 @@ stderr 'must precede command, like: garble -seed=random build \./pkg'
[windows] ! garble C:\does\not\exist\compile
stderr 'run "garble \[command\]"'
! garble build badpackage
stderr 'package badpackage is not in GOROOT'
! stdout .
! garble build ./badpackage
stderr 'directory not found'
! stdout .
# Test the version command. Note that test binaries exclude VCS build info,
# and we reuse the test binary for garble itself, so that's missing.
# To avoid building another garble binary,

@ -229,7 +229,7 @@ type ReflectEmbeddedAlias = ReflectEmbeddingNamed
type ReflectEmbeddingNamed struct{}
func VariadicReflect(x interface{}, ys ...interface{}) int {
func VariadicReflect(x any, ys ...any) int {
_ = reflect.TypeOf(x)
_ = reflect.TypeOf(ys)

Loading…
Cancel
Save