From 95c4456ccc44976fa476e94d7ba9d041ca501502 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 8 Jan 2025 21:29:02 +0800 Subject: [PATCH] env: get GOROOT from env variable or go env GOROOT --- compiler/internal/build/build.go | 2 +- compiler/internal/env/env.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/internal/build/build.go b/compiler/internal/build/build.go index c7609bbf..debf0e2a 100644 --- a/compiler/internal/build/build.go +++ b/compiler/internal/build/build.go @@ -135,7 +135,7 @@ func Do(args []string, conf *Config) ([]Package, error) { cfg.Overlay = make(map[string][]byte) for file, src := range overlayFiles { overlay := unsafe.Slice(unsafe.StringData(src), len(src)) - cfg.Overlay[filepath.Join(runtime.GOROOT(), "src", file)] = overlay + cfg.Overlay[filepath.Join(env.GOROOT(), "src", file)] = overlay } } diff --git a/compiler/internal/env/env.go b/compiler/internal/env/env.go index e587e9e5..6eef99be 100644 --- a/compiler/internal/env/env.go +++ b/compiler/internal/env/env.go @@ -1,7 +1,9 @@ package env import ( + "bytes" "os" + "os/exec" "path/filepath" "strings" ) @@ -12,6 +14,21 @@ const ( LLGoRuntimePkg = LLGoCompilerPkg + "/" + LLGoRuntimePkgName ) +func GOROOT() string { + root := os.Getenv("GOROOT") + if root != "" { + return root + } + cmd := exec.Command("go", "env", "GOROOT") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err == nil { + return strings.TrimSpace(out.String()) + } + panic("cannot get GOROOT: " + err.Error()) +} + func LLGoRuntimeDir() string { root := LLGoROOT() if root != "" {