github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/pkg/gnomod/utils.go (about) 1 package gnomod 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 ) 8 9 // ErrGnoModNotFound is returned by [FindRootDir] when, even after traversing 10 // up to the root directory, a gno.mod file could not be found. 11 var ErrGnoModNotFound = errors.New("gno.mod file not found in current or any parent directory") 12 13 // FindRootDir determines the root directory of the project which contains the 14 // gno.mod file. If no gno.mod file is found, [ErrGnoModNotFound] is returned. 15 // The given path must be absolute. 16 func FindRootDir(absPath string) (string, error) { 17 if !filepath.IsAbs(absPath) { 18 return "", errors.New("requires absolute path") 19 } 20 21 root := filepath.VolumeName(absPath) + string(filepath.Separator) 22 for absPath != root { 23 modPath := filepath.Join(absPath, "gno.mod") 24 _, err := os.Stat(modPath) 25 if errors.Is(err, os.ErrNotExist) { 26 absPath = filepath.Dir(absPath) 27 continue 28 } 29 if err != nil { 30 return "", err 31 } 32 return absPath, nil 33 } 34 35 return "", ErrGnoModNotFound 36 }