package path import ( "errors" "os" "path/filepath" ) // PathProjectRoot searches upwards from the current directory to find the project root, // identified by the presence of a "go.mod" file. func PathProjectRoot() (string, error) { dir, err := os.Getwd() if err != nil { return "", err } for { if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { return dir, nil } if _, err := os.Stat(filepath.Join(dir, "go.work")); err == nil { return dir, nil } if fi, err := os.Stat(filepath.Join(dir, ".git")); err == nil && fi.IsDir() { return dir, nil } parent := filepath.Dir(dir) if parent == dir { return "", errors.New("go.mod not found in any parent directory") } dir = parent } }