Skip to content

Commit 74e2417

Browse files
alexandearchavacava
authored andcommitted
fix: revive hangs on Windows if go.mod is not found
1 parent 34e9d78 commit 74e2417

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lint/linter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
184184
func retrieveModFile(dir string) (string, error) {
185185
const lookingForFile = "go.mod"
186186
for {
187-
if dir == "." || dir == "/" {
187+
// filepath.Dir returns 'C:\' on Windows, and '/' on Unix
188+
isRootDir := (dir == filepath.VolumeName(dir)+string(filepath.Separator))
189+
if dir == "." || isRootDir {
188190
return "", fmt.Errorf("did not found %q file", lookingForFile)
189191
}
190192

lint/linter_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
package lint
22

3-
import "testing"
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func TestRetrieveModFile(t *testing.T) {
10+
t.Run("go.mod file exists", func(t *testing.T) {
11+
nestedDir := filepath.Join(t.TempDir(), "nested", "dir", "structure")
12+
err := os.MkdirAll(nestedDir, 0o755)
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
17+
modFilePath := filepath.Join(nestedDir, "go.mod")
18+
err = os.WriteFile(modFilePath, []byte("module example.com/test"), 0o644)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
foundPath, err := retrieveModFile(nestedDir)
23+
if err != nil {
24+
t.Fatalf("unexpected error: %v", err)
25+
}
26+
if foundPath != modFilePath {
27+
t.Fatalf("expected %q, got %q", modFilePath, foundPath)
28+
}
29+
})
30+
31+
t.Run("go.mod file does not exist", func(t *testing.T) {
32+
_, err := retrieveModFile(t.TempDir())
33+
if err == nil {
34+
t.Fatalf("expected error, got nil")
35+
}
36+
expectedErrMsg := `did not found "go.mod" file`
37+
if err.Error() != expectedErrMsg {
38+
t.Fatalf("expected error message %q, got %q", expectedErrMsg, err.Error())
39+
}
40+
})
41+
}
442

543
// TestIsGenerated tests isGenerated function.
644
func TestIsGenerated(t *testing.T) { //revive:disable-line:exported

0 commit comments

Comments
 (0)