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

Get interface file fail when using ApplyInterface #954

Open
sunyakun opened this issue Aug 19, 2023 · 0 comments
Open

Get interface file fail when using ApplyInterface #954

sunyakun opened this issue Aug 19, 2023 · 0 comments

Comments

@sunyakun
Copy link

GORM Playground Link

make sure your GOROOT is not /usr/local/go before running the playground

go-gorm/playground#642

Description

The gorm/gen use go/build:Default.Import to collect the interface file path when using ApplyInterface, and the Default.Import will running go list command which find the go executable by runtime.GOROOT(), but the runtime.GOROOT() return value will invalid in some scenarios such as my playground code. See more about the runtime.GOROOT() here.

Maybe golang.org/x/tools/go/packages is a better alternatives.

import (
        "golang.org/x/tools/go/packages"
        "fmt"
	"log"
	"reflect"
)

arg := reflect.TypeOf(query.CustomQuery{})
pkgs, err := packages.Load(nil, arg.PkgPath())
if err != nil {
        log.Printf("x/tools/go/packages: load fail: %s", err.Error())
} else {
        for _, pkg := range pkgs {
	        for _, file := range pkg.CompiledGoFiles {
		        fmt.Printf("%s\n", file)
	        }
        }
}
@sunyakun sunyakun changed the title go/build import error when using ApplyInterface Get interface file fail when using ApplyInterface Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant