env: warning and fallback if LLGO_ROOT is invalid

This commit is contained in:
Li Jie
2025-01-18 15:38:51 +08:00
parent 93d36d40d8
commit 977db86700
2 changed files with 35 additions and 8 deletions

View File

@@ -41,9 +41,13 @@ func LLGoRuntimeDir() string {
} }
func LLGoROOT() string { func LLGoROOT() string {
if root, ok := isLLGoRoot(os.Getenv("LLGO_ROOT")); ok { llgoRootEnv := os.Getenv("LLGO_ROOT")
if llgoRootEnv != "" {
if root, ok := isLLGoRoot(llgoRootEnv); ok {
return root return root
} }
fmt.Fprintf(os.Stderr, "WARNING: LLGO_ROOT is not a valid LLGO root: %s\n", llgoRootEnv)
}
// Get executable path // Get executable path
exe, err := os.Executable() exe, err := os.Executable()
if err != nil { if err != nil {

View File

@@ -58,10 +58,28 @@ func TestLLGoRuntimeDir(t *testing.T) {
defer os.Setenv("LLGO_ROOT", origLLGoRoot) defer os.Setenv("LLGO_ROOT", origLLGoRoot)
os.Setenv("LLGO_ROOT", "/nonexistent/path") os.Setenv("LLGO_ROOT", "/nonexistent/path")
wd, _ := os.Getwd() wd, err := os.Getwd()
expected, _ := filepath.Abs(filepath.Join(wd, "../../../runtime")) if err != nil {
if got := LLGoRuntimeDir(); got != expected { t.Fatal(err)
t.Errorf("LLGoRuntimeDir() = %v, want %v", got, expected) }
runtimeDir := filepath.Join(wd, "../../../runtime")
if got := LLGoRuntimeDir(); got != runtimeDir {
t.Errorf("LLGoRuntimeDir() = %v, want %v", got, runtimeDir)
}
})
t.Run("devel runtime dir", func(t *testing.T) {
origLLGoRoot := os.Getenv("LLGO_ROOT")
defer os.Setenv("LLGO_ROOT", origLLGoRoot)
os.Setenv("LLGO_ROOT", "")
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
runtimeDir := filepath.Join(wd, "../../../runtime")
if got := LLGoRuntimeDir(); got != runtimeDir {
t.Errorf("LLGoRuntimeDir() = %v, want %v", got, runtimeDir)
} }
}) })
} }
@@ -92,8 +110,13 @@ func TestLLGoROOT(t *testing.T) {
defer os.Setenv("LLGO_ROOT", origLLGoRoot) defer os.Setenv("LLGO_ROOT", origLLGoRoot)
os.Setenv("LLGO_ROOT", "/nonexistent/path") os.Setenv("LLGO_ROOT", "/nonexistent/path")
if got := LLGoROOT(); got != "" { wd, err := os.Getwd()
t.Errorf("LLGoROOT() = %v, want empty string", got) if err != nil {
t.Fatal(err)
}
rootDir := filepath.Join(wd, "../../..")
if got := LLGoROOT(); got != rootDir {
t.Errorf("LLGoROOT() = %v, want %v", got, rootDir)
} }
}) })