diff --git a/here/dir.go b/here/dir.go index 1fb0771..b2d3b49 100644 --- a/here/dir.go +++ b/here/dir.go @@ -2,6 +2,7 @@ package here import ( "encoding/json" + "fmt" "os" "path/filepath" ) @@ -9,7 +10,6 @@ import ( // Dir attempts to gather info for the requested directory. func Dir(p string) (Info, error) { i, err := Cache(p, func(p string) (Info, error) { - var i Info fi, err := os.Stat(p) @@ -35,7 +35,10 @@ func Dir(p string) (Info, error) { // build .: cannot find module for path . // no Go files in if err != nil { - return fromNonGoDir(p) + if nonGoDirRx.MatchString(err.Error()) { + return fromNonGoDir(p) + } + return i, fmt.Errorf("%w %s", err, p) } if err := json.Unmarshal(b, &i); err != nil { @@ -55,42 +58,6 @@ func Dir(p string) (Info, error) { } -func dir(p string) (Info, error) { - var i Info - - fi, err := os.Stat(p) - if err != nil { - return i, err - } - - if !fi.IsDir() { - p = filepath.Dir(p) - } - - pwd, err := os.Getwd() - if err != nil { - return i, err - } - - defer os.Chdir(pwd) - - os.Chdir(p) - - b, err := run("go", "list", "-json") - // go: cannot find main module; see 'go help modules' - // build .: cannot find module for path . - // no Go files in - if err != nil { - return fromNonGoDir(p) - } - - if err := json.Unmarshal(b, &i); err != nil { - return i, err - } - - return i, nil -} - func fromNonGoDir(dir string) (Info, error) { i := Info{ Dir: dir, diff --git a/here/dir_test.go b/here/dir_test.go index f81d0ce..421d0be 100644 --- a/here/dir_test.go +++ b/here/dir_test.go @@ -47,6 +47,7 @@ func Test_Dir(t *testing.T) { } for _, tt := range table { t.Run(tt.in, func(st *testing.T) { + here.ClearCache() r := require.New(st) info, err := here.Dir(tt.in)