From 1812bdb9bd6a44c6e8556ca7e780053010a0af03 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 3 Sep 2020 22:08:05 +1000 Subject: [PATCH] Look for the wuffs root dir in CWD and ancestors Updates #35 --- lang/wuffsroot/wuffsroot.go | 38 ++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/lang/wuffsroot/wuffsroot.go b/lang/wuffsroot/wuffsroot.go index 2f64f96d..d1a24f07 100644 --- a/lang/wuffsroot/wuffsroot.go +++ b/lang/wuffsroot/wuffsroot.go @@ -22,11 +22,25 @@ import ( "sync" ) +var initialWorkingDirectory = "" + +func init() { + initialWorkingDirectory, _ = os.Getwd() +} + var cache struct { mu sync.Mutex value string } +func setValue(value string) (string, error) { + cache.mu.Lock() + cache.value = value + cache.mu.Unlock() + + return value, nil +} + func Value() (string, error) { cache.mu.Lock() value := cache.value @@ -36,17 +50,23 @@ func Value() (string, error) { return value, nil } - // TODO: look for a WUFFSROOT environment variable? Also check that - // wuffs-root-directory.txt exists? - for _, p := range filepath.SplitList(build.Default.GOPATH) { - p = filepath.Join(p, "src", "github.com", "google", "wuffs") - if o, err := os.Stat(p); err == nil && o.IsDir() { - cache.mu.Lock() - cache.value = p - cache.mu.Unlock() + const wrdTxt = "wuffs-root-directory.txt" - return p, nil + // Look for "w-r-d.txt" in the working directory or its ancestors. + for p, q := initialWorkingDirectory, ""; p != q; p, q = filepath.Dir(p), p { + if _, err := os.Stat(filepath.Join(p, wrdTxt)); err == nil { + return setValue(p) } } + + // Look for "github.com/google/wuffs/w-r-d.txt" in the Go source + // directories. + for _, p := range build.Default.SrcDirs() { + p = filepath.Join(p, "github.com", "google", "wuffs") + if _, err := os.Stat(filepath.Join(p, wrdTxt)); err == nil { + return setValue(p) + } + } + return "", errors.New("could not find Wuffs root directory") }