Skip to content

Commit 3799e65

Browse files
Add new goroot extraction fallback
This commit adds a fallback to extract the GOROOT from the standard library package path.
1 parent 0736217 commit 3799e65

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

goroot.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package gore
1919

2020
import (
2121
"bytes"
22+
"fmt"
2223
"reflect"
24+
"strings"
2325
"unicode/utf8"
2426

2527
"golang.org/x/arch/x86/x86asm"
@@ -327,11 +329,36 @@ func findGoRootPath(f *GoFile) (string, error) {
327329
// There is no GOROOT function may be inlined (after go1.16)
328330
// at this time GOROOT is obtained through time_init function
329331
goroot, err := tryFromGOROOT(f)
332+
if goroot != "" {
333+
return goroot, nil
334+
}
335+
if err != nil && err != ErrNoGoRootFound {
336+
return "", err
337+
}
338+
339+
goroot, err = tryFromTimeInit(f)
340+
if goroot != "" {
341+
return goroot, nil
342+
}
343+
if err != nil && err != ErrNoGoRootFound {
344+
return "", err
345+
}
346+
347+
// Try determine from std lib package paths.
348+
pkg, err := f.GetSTDLib()
330349
if err != nil {
331-
if err == ErrNoGoRootFound {
332-
return tryFromTimeInit(f)
350+
return "", fmt.Errorf("error when getting standard library packages: %w", err)
351+
}
352+
if len(pkg) == 0 {
353+
return "", fmt.Errorf("no standard library packages found")
354+
}
355+
356+
for _, v := range pkg {
357+
subpath := fmt.Sprintf("/src/%s", v.Name)
358+
if strings.HasSuffix(v.Filepath, subpath) {
359+
return strings.TrimSuffix(v.Filepath, subpath), nil
333360
}
334-
return "", err
335361
}
336-
return goroot, nil
362+
363+
return "", ErrNoGoRootFound
337364
}

goroot_test.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,8 @@ func TestExtractGoRoot(t *testing.T) {
4848
r.NoError(err)
4949
defer f.Close()
5050
goroot, err := findGoRootPath(f)
51-
// Windows version 1.5 to 1.9 did not find a goroot string that can be searched, so we ruled it out
52-
switch test {
53-
case "gold-windows-386-1.5.0":
54-
r.Equal(ErrNoGoRootFound, err)
55-
case "gold-windows-386-1.6.0":
56-
r.Equal(ErrNoGoRootFound, err)
57-
case "gold-windows-386-1.7.0":
58-
r.Equal(ErrNoGoRootFound, err)
59-
case "gold-windows-386-1.7beta1":
60-
r.Equal(ErrNoGoRootFound, err)
61-
case "gold-windows-386-1.8.0":
62-
r.Equal(ErrNoGoRootFound, err)
63-
case "gold-windows-386-1.9.0":
64-
r.Equal(ErrNoGoRootFound, err)
65-
case "gold-windows-amd64-1.5.0":
66-
r.Equal(ErrNoGoRootFound, err)
67-
case "gold-windows-amd64-1.6.0":
68-
r.Equal(ErrNoGoRootFound, err)
69-
case "gold-windows-amd64-1.7.0":
70-
r.Equal(ErrNoGoRootFound, err)
71-
case "gold-windows-amd64-1.7beta1":
72-
r.Equal(ErrNoGoRootFound, err)
73-
case "gold-windows-amd64-1.8.0":
74-
r.Equal(ErrNoGoRootFound, err)
75-
case "gold-windows-amd64-1.9.0":
76-
r.Equal(ErrNoGoRootFound, err)
77-
default:
78-
r.NoError(err)
79-
r.Equal(expectGoRoot, goroot)
80-
}
51+
r.NoError(err)
52+
r.Equal(expectGoRoot, goroot)
8153
})
8254
}
8355
}

0 commit comments

Comments
 (0)