Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revive hangs on Windows if go.mod is not found #1123

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
func retrieveModFile(dir string) (string, error) {
const lookingForFile = "go.mod"
for {
if dir == "." || dir == "/" {
// filepath.Dir returns 'C:\' on Windows, and '/' on Unix
isRootDir := (dir == filepath.VolumeName(dir)+string(filepath.Separator))
if dir == "." || isRootDir {
return "", fmt.Errorf("did not found %q file", lookingForFile)
}

Expand Down
40 changes: 39 additions & 1 deletion lint/linter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
package lint

import "testing"
import (
"os"
"path/filepath"
"testing"
)

func TestRetrieveModFile(t *testing.T) {
t.Run("go.mod file exists", func(t *testing.T) {
nestedDir := filepath.Join(t.TempDir(), "nested", "dir", "structure")
err := os.MkdirAll(nestedDir, 0o755)
if err != nil {
t.Fatal(err)
}

modFilePath := filepath.Join(nestedDir, "go.mod")
err = os.WriteFile(modFilePath, []byte("module example.com/test"), 0o644)
if err != nil {
t.Fatal(err)
}
foundPath, err := retrieveModFile(nestedDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if foundPath != modFilePath {
t.Fatalf("expected %q, got %q", modFilePath, foundPath)
}
})

t.Run("go.mod file does not exist", func(t *testing.T) {
_, err := retrieveModFile(t.TempDir())
if err == nil {
t.Fatalf("expected error, got nil")
}
expectedErrMsg := `did not found "go.mod" file`
if err.Error() != expectedErrMsg {
t.Fatalf("expected error message %q, got %q", expectedErrMsg, err.Error())
}
})
}

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