diff --git a/internal/code/imports.go b/internal/code/imports.go index 75c30fe1ed5..5e5d73a341d 100644 --- a/internal/code/imports.go +++ b/internal/code/imports.go @@ -62,21 +62,22 @@ func ImportPathForDir(dir string) (res string) { modDir := dir assumedPart := "" for { - f, err := ioutil.ReadFile(filepath.Join(modDir, "/", "go.mod")) + f, err := ioutil.ReadFile(filepath.Join(modDir, "go.mod")) if err == nil { // found it, stop searching return string(modregex.FindSubmatch(f)[1]) + assumedPart } assumedPart = "/" + filepath.Base(modDir) + assumedPart - modDir, err = filepath.Abs(filepath.Join(modDir, "..")) + parentDir, err := filepath.Abs(filepath.Join(modDir, "..")) if err != nil { panic(err) } - // Walked all the way to the root and didnt find anything :'( - if modDir == "/" { + if parentDir == modDir { break + } else { + modDir = parentDir } } diff --git a/internal/code/imports_test.go b/internal/code/imports_test.go index 5e8a0a214b8..e3bc9474f99 100644 --- a/internal/code/imports_test.go +++ b/internal/code/imports_test.go @@ -3,6 +3,7 @@ package code import ( "os" "path/filepath" + "runtime" "testing" "github.com/stretchr/testify/assert" @@ -21,6 +22,12 @@ func TestImportPathForDir(t *testing.T) { // directory does not exist assert.Equal(t, "github.com/99designs/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos"))) + + if runtime.GOOS == "windows" { + assert.Equal(t, "", ImportPathForDir("C:/doesnotexist")) + } else { + assert.Equal(t, "", ImportPathForDir("/doesnotexist")) + } } func TestNameForPackage(t *testing.T) {