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

xgo failed to run tests on Windows #103

Closed
Zxilly opened this issue May 10, 2024 · 14 comments
Closed

xgo failed to run tests on Windows #103

Zxilly opened this issue May 10, 2024 · 14 comments
Assignees
Labels
bug Something isn't working done mock windows
Milestone

Comments

@Zxilly
Copy link

Zxilly commented May 10, 2024

I created a test with xgo on https://github.com/Zxilly/go-size-analyzer/blob/master/internal/printer/json_xgo_test.go. But when I run the test with

xgo test -v -race -covermode=atomic -cover ./... -tags xgo,embed

It tolds me

open C:\Users\zxilly\.xgo\go-instrument\go1.22.3_C_Us_12_go_pk_mo_go_to_7f0da9bd\go1.22.3\src\runtime\proc.go: The system cannot find the path specified.

The dir structure of C:\Users\zxilly\.xgo\go-instrument\go1.22.3_C_Us_12_go_pk_mo_go_to_7f0da9bd\ is

    Directory: C:\Users\zxilly\.xgo\go-instrument\go1.22.3_C_Us_12_go_pk_mo_go_to_7f0da9bd

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2024/5/10    11:36                pkgdata

nothing in the pkgdata directory.

I'm running go 1.22.3 and xgo 1.0.31, build with go install.

@Zxilly
Copy link
Author

Zxilly commented May 10, 2024

The project using go toolchain, is this leading to the error?

@xhd2015
Copy link
Owner

xhd2015 commented May 10, 2024

Thanks for raising the issue, I will take a deep look later.
This project requires $GOROOT/src exists because it needs to instrument the go runtime and compiler. Can you confirm if your GOROOT contains a src directory?

Besides can you try to run this within WSL to see if it works?

Thanks.

@xhd2015 xhd2015 added bug Something isn't working windows labels May 10, 2024
@Zxilly
Copy link
Author

Zxilly commented May 10, 2024

Here's go env

go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\zxilly\AppData\Local\go-build
set GOENV=C:\Users\zxilly\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\zxilly\go\pkg\mod
set GONOPROXY=1
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\zxilly\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:/Users/zxilly/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\zxilly\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.22.3.windows-amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=E:\Project\CS_Project\gsv\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\zxilly\AppData\Local\Temp\go-build2969563362=/tmp/go-build -gno-record-gcc-switches

C:\Users\zxilly\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.22.3.windows-amd64\src exists and include the required file.

@xhd2015
Copy link
Owner

xhd2015 commented May 10, 2024

Seems like go-toolchain related. I'll try later

@Zxilly
Copy link
Author

Zxilly commented May 10, 2024

On linux it works but mock failed.

--- FAIL: TestJson (0.00s)
panic: failed to setup mock for: encoding/json.Marshal [recovered]
        panic: failed to setup mock for: encoding/json.Marshal

goroutine 18 [running]:
testing.tRunner.func1.2({0xe52500, 0xc00045f8c0})
        /home/zxilly/.xgo/go-instrument/go1.22.3_ho_zx_go_pk_mo_go_to_9d7c113a/go1.22.3/src/testing/testing.go:1642 +0x3f7
testing.tRunner.func1()
        /home/zxilly/.xgo/go-instrument/go1.22.3_ho_zx_go_pk_mo_go_to_9d7c113a/go1.22.3/src/testing/testing.go:1645 +0x6b6
panic({0xe52500?, 0xc00045f8c0?})
        /home/zxilly/.xgo/go-instrument/go1.22.3_ho_zx_go_pk_mo_go_to_9d7c113a/go1.22.3/src/runtime/panic.go:770 +0x132
github.com/xhd2015/xgo/runtime/mock.getFunc({0xe49820, 0xf92468})
        /home/zxilly/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.31/mock/mock.go:54 +0x12a
github.com/xhd2015/xgo/runtime/mock.Patch({0xe49820, 0xf92468}, {0xe49820, 0xf924a0})
        /home/zxilly/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.31/mock/patch.go:51 +0x49d
github.com/Zxilly/go-size-analyzer/internal/printer.TestJson(0xc00018f380)
        /mnt/e/Project/CS_Project/gsv/internal/printer/json_xgo_test.go:15 +0x177
testing.tRunner(0xc00018f380, 0xf922a8)
        /home/zxilly/.xgo/go-instrument/go1.22.3_ho_zx_go_pk_mo_go_to_9d7c113a/go1.22.3/src/testing/testing.go:1709 +0x47c
created by testing.(*T).Run in goroutine 1
        /home/zxilly/.xgo/go-instrument/go1.22.3_ho_zx_go_pk_mo_go_to_9d7c113a/go1.22.3/src/testing/testing.go:1762 +0x7e6
FAIL    github.com/Zxilly/go-size-analyzer/internal/printer     0.037s

But I guess it's a limitation of xgo itself that the build was successful

@xhd2015
Copy link
Owner

xhd2015 commented May 10, 2024

On linux it works but mock failed.

panic: failed to setup mock for: encoding/json.Marshal [recovered]
        panic: failed to setup mock for: encoding/json.Marshal

But I guess it's a limitation of xgo itself that the build was successful

Yes because only a small part of stdlib are injected. See https://github.com/xhd2015/xgo/blob/master/runtime/mock/stdlib.md.
But if that is necessary, I can add support for them.

@xhd2015
Copy link
Owner

xhd2015 commented May 14, 2024

The failure caused this issue on windows has been fixed in #109 .

To upgrade, run:

# update xgo
go install github.com/xhd2015/xgo/cmd/xgo@v1.0.34

# update dependency
go get github.com/xhd2015/xgo/runtime@v1.0.34

@xhd2015 xhd2015 added the done label May 14, 2024
@Zxilly
Copy link
Author

Zxilly commented May 17, 2024

I still think hook stdlib is quite important, because in other cases we always have the opportunity to get around the need for hooks by writing test-friendly code, but modifications to the standard library prevent the code from compiling on other machines, which makes a monkey patch inevitable.

@xhd2015
Copy link
Owner

xhd2015 commented May 17, 2024

I still think hook stdlib is quite important, because in other cases we always have the opportunity to get around the need for hooks by writing test-friendly code, but modifications to the standard library prevent the code from compiling on other machines, which makes a monkey patch inevitable.

@Zxilly OK I got your point. I also feel it would be more feasible if xgo allows user to specify what stdlib funcs they want to mock. I will take a look on this more deeply.

By the way, do you have any case that would require mocking specific stdlib functions? You can post an example here or raise another issue so that we have a better understanding of the problem.

Thanks.

@Zxilly
Copy link
Author

Zxilly commented May 17, 2024

func download() (string, error) {
	slog.Info("Downloading template")
	resp, err := http.Get(BaseURL)
	if err != nil {
		return "", err
	}
	defer func(body io.ReadCloser) {
		_ = body.Close()
	}(resp.Body)

	// check status code
	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("failed to download template: %s", resp.Status)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(body), nil
}
func getCacheFilePath() (string, error) {
	dir, err := os.UserCacheDir()
	if err != nil {
		return "", err
	}

	cacheDir := filepath.Join(dir, "go-size-analyzer")

	err = os.MkdirAll(cacheDir, 0755)
	if err != nil {
		return "", err
	}

	file := filepath.Join(cacheDir, fmt.Sprintf("webui-v%s.html", gsa.StaticVersion))

	return file, nil
}

In fact, both of these examples can be tested by replacing the corresponding calls with variables, but this amounts to adding runtime complexity for the sake of the test

@xhd2015 xhd2015 added this to the v1.1.0 milestone May 17, 2024
@xhd2015 xhd2015 self-assigned this May 17, 2024
@xhd2015
Copy link
Owner

xhd2015 commented May 18, 2024

@Zxilly This is being resolved in this PR: #117 . Will be available in the next version of xgo at 2024-05-20.

I tried to support as more stdlib packages as possible, however due the nature of the implementations, these packages cannot be mocked:

runtime, syscall, reflect, sync,  sync/atomic, testing, unsafe

User needs to add --trap-stdlib to explicitly enable mocking stdlib functions:

xgo test --trap-stdlib ./

@xhd2015
Copy link
Owner

xhd2015 commented May 20, 2024

@Zxilly
Copy link
Author

Zxilly commented May 20, 2024

Is that possible to automatic set a build tag for trap-stdlib?

@xhd2015
Copy link
Owner

xhd2015 commented May 20, 2024

It is possible to automatically add --trap-stdlib when xgo test is used. But since xgo v1.0.36 is the first release introducing this flag, I think I need to test this flag against some large projects like kubernetes to see if it has any impact.

If all tests go well, I'll make this flag default in the future version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working done mock windows
Projects
Status: Done
Development

No branches or pull requests

2 participants