garble icon indicating copy to clipboard operation
garble copied to clipboard

"garble test" doesn't work with reflection

Open huibeu opened this issue 4 months ago • 3 comments

Output of garble version:

mvdan.cc/garble v0.14.2

Build settings:
      -buildmode exe
       -compiler gc
  DefaultGODEBUG gotestjsonbuildtext=1,multipathtcp=0,randseednop=0,rsa1024min=0,tlsmlkem=0,x509rsacrt=0,x509usepolicies=0
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1753113114=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.5'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

go test gives errors, even when running tests on the std packages.

# This works
go test reflect
go test encoding/xml
go test encoding/json

# This gives failing tests and build errors
garble test reflect
garble test encoding/xml
garble test encoding/json

I did not see any issues when running code with garble run or garble build.

What did you see happen?

garble test reflect

# reflect.test
list flag: not found
FAIL	reflect [build failed]
FAIL
exit status 1

garble test encoding/xml

--- FAIL: TestMarshal (0.00s)
    --- FAIL: TestMarshal/2 (0.00s)
        ozLy15Wlr.go:1: marshal(&bvuKzfAhE4.Jp9cjKXs{MmON2eeJ:true}):
            have `<Jp9cjKXs><MmON2eeJ>true</MmON2eeJ></Jp9cjKXs>`
            want `<Plain><V>true</V></Plain>`
[...]
FAIL
FAIL	encoding/xml	3.234s
FAIL
exit status 1

garble test encoding/json

# encoding/json.test
list internal/fuzz: not found
FAIL	encoding/json [build failed]
FAIL
exit status 1

What did you expect to see?

I expected the output to look similar to running the tests with go test:

$ go test reflect
ok  	reflect	0.279s
$ go test encoding/xml
ok  	encoding/xml	3.360s
$ go test encoding/json
ok  	encoding/json	0.378s

huibeu avatar Aug 01 '25 09:08 huibeu

Being able to garble test the standard library is not an objective of this project. But patches are welcome if you want to work towards that.

mvdan avatar Aug 01 '25 09:08 mvdan

I used the tests of the standard library because I thought it would be easier to reproduce. I'm running into the same problem with my own tests. For example:

main.go:

package main

import (
	"encoding/xml"
	"fmt"
)

type Book struct {
	XMLName struct{} `xml:"book"`
	Title   string   `xml:",chardata"`
}

func getXML() string {
	book := &Book{Title: "My book"}
	data, _ := xml.Marshal(book)
	return string(data)
}

func main() {
	fmt.Println(getXML())
}

main_test.go:

package main

import "testing"

func TestGetXml(t *testing.T) {
	gotXml := getXML()
	wantXml := "<book>My book</book>"

	if gotXml != wantXml {
		t.Errorf("got  %v\n want %v", gotXml, wantXml)
	}
}

Output:

$ go run .  
<book>My book</book>

$ go test .
ok      garble-demo     0.001s

$ garble run .
<book>My book</book>

$ garble test .
--- FAIL: TestGetXml (0.00s)
    A8iPCAyf3f0_.go:1: got  <OReE6JEkJ1Rk><book></book>My book</OReE6JEkJ1Rk>
         want <book>My book</book>
FAIL
FAIL    garble-demo     0.002s
FAIL
exit status 1

Would you like me to create a new ticket? I did have a quick look at the garble source code but I couldn't find out why this would behave differently for tests.

huibeu avatar Aug 01 '25 10:08 huibeu

Very odd that garble run works, but garble test does not.

Thanks for clarifying this is not about testing the standard library.

mvdan avatar Aug 01 '25 22:08 mvdan