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

go-vet does not work if Go code is not in the root folder #30

Open
talha131 opened this issue May 30, 2019 · 8 comments
Open

go-vet does not work if Go code is not in the root folder #30

talha131 opened this issue May 30, 2019 · 8 comments

Comments

@talha131
Copy link

go-vet script, first runs go list in the root directory. It fails if it does not find go files.

In my case, I have multiple related projects in the same repo. Something like this

.
├── LICENSE
├── README.md
├── backend
└── frontend

I need go-vet to only run the backend folder. But the way script works, it is not possible.

One way is to infer the directory from the staged files. Or add option to pass dir in the argument to the hook.

@davidpfarrell
Copy link
Contributor

I'm experiencing a similar issue with both the go-vet and go-build scripts.

At least the vet script tries to get a list. The build script currently does not.

What would you think to :

  1. Modifying the go-vet script to do go list ./... instead of just go list
  2. Add the same functionality to the go-build script

?

I could create a separate issue for the go-build mod to keep changes clean.

BTW: I currently get around this issue by having a doc.go file in my root folder. This allows vet and build to succeed and continue into sub-folders. Not sure if there are any negative side effects to having it and in fact it gives me a chance to give a quick description of the repo, so I kinda like it (although I would prefer the scripts work without it)

@treuherz
Copy link

treuherz commented Mar 2, 2020

Is there a reason running go vet ./... wouldn't work? If that command doesn't fail fast on finding an error then I'd agree with @davidpfarrell's solution of looping over the output of go list ./...

@davidpfarrell
Copy link
Contributor

Hi @treuherz ,

I discovered the issue overall to be more nuanced, what with gopath vs modules and single-package-repos vs multi-package repos.

Inspired by @dnephin 's work, I ended up developing a new set of hooks that tries to offer more support for various project setups:

If you had some time to try it, I'd love to hear if it worked any better for your project's setup?

@treuherz
Copy link

treuherz commented Mar 2, 2020

That's excellent, @davidpfarrell, it solves the problem perfectly. The hooks that only run against the modules of staged files are great to see as well, a very helpful improvement.

@rupamkhaitan
Copy link

rupamkhaitan commented Jun 23, 2020

@davidpfarrell what is your suggestion for doc.go? How to make go build work? What do you write inside doc.go? Is it a blank file?

i raised another issue for go build not working #50

@rupamkhaitan
Copy link

@davidpfarrell your new repo is very useful, i am also moving to this one https://github.com/TekWizely/pre-commit-golang/blob/master/sample-config.yaml

@davidpfarrell
Copy link
Contributor

@davidpfarrell what is your suggestion for doc.go? How to make go build work? What do you write inside doc.go? Is it a blank file?

i raised another issue for go build not working #50

Hi @rupamkhaitan ,

Here's the doc.go referenced in my comment:

I think at the time it just contained the package declaration and that's all it needed in order to work with these hooks.

your new repo is very useful, i am also moving to this one
Thanks for the note. Looking at your code sample in #50 you'll likely want to use the *-mod hooks, which will apply the hooks to the module root folder of changed files.

Good luck!

@rupamkhaitan
Copy link

rupamkhaitan commented Jun 24, 2020

@davidpfarrell yes i am using your repo and running my build by passing custom tags.. and everything works.. I dont really understand the diff between go-build-mod & go-build-repo-mod (and -pkg ones is for old strcuture??)

 - id: go-build-mod
        always_run: true # Run even if no matching files staged
        args: ["-tags", "e2e"]

wwade added a commit to wwade/pre-commit-golang that referenced this issue Feb 1, 2022
Some packages don't have any go files in the root directory, for
example, those following https://github.com/golang-standards/project-layout

In the case of a GO111MODULE package with go.mod in place, we can just
use "go list -m" and this will return the package name even without
any go files in the project root.

I've also updated the script to report all files that fail "go vet" in
a single run instead of exiting after finding the first error.

This is a fix for issue dnephin#30.
wwade added a commit to wwade/pre-commit-golang that referenced this issue Feb 1, 2022
Some packages don't have any go files in the root directory, for
example, those following https://github.com/golang-standards/project-layout

In the case of a GO111MODULE package with go.mod in place, we can just
use "go list -m" and this will return the package name even without
any go files in the project root.

I've also updated the script to report all files that fail "go vet" in
a single run instead of exiting after finding the first error.

This is a fix for issue dnephin#30.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants