Skip to content

Commit ded5f1e

Browse files
committed
Cache base path for util.RelPath() on first use.
It isn't supposed to change (it would be a problem if it did). More to the point, flaky filesystems can make that fail with a panic, which is tolerable on startup but not half-way through a pipeline.
1 parent 23c05ff commit ded5f1e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

martian/util/util.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,31 @@ import (
2727
"github.com/martian-lang/docopt.go"
2828
)
2929

30-
func RelPath(p string) string {
31-
base := os.Getenv("MARTIAN_BASE")
32-
if base != "" {
33-
return path.Join(base, p)
30+
func getExeBasePath() string {
31+
if base := os.Getenv("MARTIAN_BASE"); base != "" {
32+
return base
3433
} else {
3534
if exe, err := os.Executable(); err != nil {
3635
panic(err)
3736
} else {
3837
if exe, err := filepath.EvalSymlinks(exe); err != nil {
3938
panic(err)
4039
} else {
41-
return path.Join(path.Dir(exe), p)
40+
return path.Dir(exe)
4241
}
4342
}
4443
}
4544
}
4645

46+
var exeBasePath string
47+
48+
func RelPath(p string) string {
49+
if exeBasePath == "" {
50+
exeBasePath = getExeBasePath()
51+
}
52+
return path.Join(exeBasePath, p)
53+
}
54+
4755
func Mkdir(p string) error {
4856
if err := os.Mkdir(p, 0777); err != nil {
4957
if !os.IsExist(err) {

0 commit comments

Comments
 (0)