gruntwork-io / gruntwork-io/pre-commit

`gofmt` check fails if `go.mod` and `go.sum` are not in the root directory

Open
#67 0 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
Shell
Stars
547
Forks
168
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
The `gofmt` check fails if Go code is stored in a subdirectory of the repository:

```
gofmt....................................................................Failed
- hook id: gofmt
- exit code: 1

go: cannot find main module, but found .git/config in /Users/gchappel/code/sample-directory
to create a module there, run:
go mod init
```

**To Reproduce**
* Create an empty directory
* `git init .`
* Configure `.pre-commit-config.yaml` with the Gruntwork pre-commit repo and enable the `gofmt` hook
* Create a subdirectory
* In this subdirectory run `go mod init test`
* In the subdirectory write a deliberately-malformed Go file, for example using spaces instead of tabs:
```go
package main

import (
"fmt" // this line starts with two spaces
)

func main() {
fmt.Println("test") // this line starts with two spaces
}
```
* `git add .` to add the Go code and the `.pre-commit-config.yaml` to the staging area
* run `pre-commit run gofmt`
* see error

**Expected behavior**
The malformed Go file has its spaces replaced with tabs

**Nice to have**
- [ ] Terminal output
- [ ] Screenshots

**Additional context**
Add any other context about the problem here.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.