Giter VIP home page Giter VIP logo

Comments (10)

TekWizely avatar TekWizely commented on August 24, 2024

Hi @Xumeiquer

Are you sure? Can you give an example of an existing hook that you want to use build tags with but can't?

from pre-commit-golang.

Xumeiquer avatar Xumeiquer commented on August 24, 2024

Sure... I'll be focusing on go-vet just for a matter of brevity.

Running go vet through the command line:

# Without build tags
$ go vet ./...
# github.com/xxxx/yyyy/internal/app
internal/app/app.go:50:17: undefined: NewDatabase
internal/app/app.go:55:2: undefined: CloseDBConnection

# With build tags
$ go vet -tags mongo ./...

Now running go vet using this pre-commit project.

First run without args.

[...]
- repo: https://github.com/tekwizely/pre-commit-golang
    rev: v1.0.0-rc.1
    hooks:
      - id: go-vet
[...]

The output is:

pre-commit run --all-files
[INFO] Initializing environment for https://github.com/tekwizely/pre-commit-golang.
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
check for added large files..............................................Passed
check for merge conflicts................................................Passed
detect private key.......................................................Passed
Checkov..............................................(no files to check)Skipped
Lint Dockerfiles.....................................(no files to check)Skipped
go-vet...................................................................Failed
- hook id: go-vet
- exit code: 1

# github.com/xxxx/yyyy/internal/app
internal/app/app.go:50:17: undefined: NewDatabase
internal/app/app.go:55:2: undefined: CloseDBConnection
# command-line-arguments
vet: internal/app/app.go:50:17: undefined: NewDatabase

Now, pre-commit with args:

[...]
- repo: https://github.com/tekwizely/pre-commit-golang
    rev: v1.0.0-rc.1
    hooks:
      - id: go-vet
        args: ["-tags", "mongo"]
[...]

The output:

pre-commit run --all-files
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
check for added large files..............................................Passed
check for merge conflicts................................................Passed
detect private key.......................................................Passed
Checkov..............................................(no files to check)Skipped
Lint Dockerfiles.....................................(no files to check)Skipped
go-vet...................................................................Failed
- hook id: go-vet
- exit code: 1

# command-line-arguments
vet: internal/app/app.go:50:17: undefined: NewDatabase

from pre-commit-golang.

TekWizely avatar TekWizely commented on August 24, 2024

Thanks for taking the time add your notes. The first thing I want to focus on is that it looks like you may be using the wrong hook:

Your cli examples used:

go vet ./...

but the default go-vet hook doesn't use ./.... Per the docs

Hook ID Description
go-vet Run 'go vet [$ARGS] $FILE' for each staged .go file
go-vet-mod Run 'cd $(mod_root $FILE); go vet [$ARGS] ./...' for each staged .go file
go-vet-pkg Run 'go vet [$ARGS] ./$(dirname $FILE)' for each staged .go file
go-vet-repo-mod Run 'cd $(mod_root); go vet [$ARGS] ./...' for each module in the repo
go-vet-repo-pkg Run 'go vet [$ARGS] ./...' in repo root folder

To get the equivalent command, you'll want one of:

  • go-vet-mod - You use modules and only want to run against modules of changed files
  • go-vet-repo-mod - You use modules and want to run against all modules every time
  • go-vet-repo-pkg - You use the old pre-module package structure and want to run against the package root folder every time

Depending on your repository structure.

If you could, please try using one of the mentioned hook-ids and report back your findings.

-TW

from pre-commit-golang.

Xumeiquer avatar Xumeiquer commented on August 24, 2024

It works now. Thank you.
I use the go-vet-repo-mod version and it and pre-commit execution was succeesfull.

pre-commit run --all-files
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
check for added large files..............................................Passed
check for merge conflicts................................................Passed
detect private key.......................................................Passed
Checkov..............................................(no files to check)Skipped
Lint Dockerfiles.....................................(no files to check)Skipped
go-fmt...................................................................Passed
go-fmt-repo..............................................................Passed
go-vet-repo-mod..........................................................Passed
go-sec-repo-mod..........................................................Passed

The args I used is: args: [-tags=mongo]. If I set the args to args: [-tags mongo] or args: ["-tags", "mongo"], it does not work and I guess it is because how bash treats the spaces and arrays.

from pre-commit-golang.

TekWizely avatar TekWizely commented on August 24, 2024

Glad it worked! BUT args: ["-tags", "mongo"] should have worked, too.

Q: Do you happen to have a file named mongo in your repo ?

from pre-commit-golang.

TekWizely avatar TekWizely commented on August 24, 2024

Some simple tests in my local suggests that args: ["-tags", "mongo"] works as expected.
Q: What version of go are you using? I'm using go version go1.17 darwin/amd64 but may try updating to 1.20
Q: Do you know what version of bash you have installed?

from pre-commit-golang.

Xumeiquer avatar Xumeiquer commented on August 24, 2024
$ go version
go version go1.20 darwin/amd64
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.
$ zsh --version
zsh 5.8 (x86_64-apple-darwin20.0)

I do have a package and and file named mongo and mongo.go respectively. I renamed them as well as adjusted all the dependencies and the build for go-sec-repo-mod keeps failing, since the unique one failing is go-sec-repo-mod it may be something related with the gosec which is called underneath.

from pre-commit-golang.

TekWizely avatar TekWizely commented on August 24, 2024

Thanks (and sorry) for re-arranging your repo for a test :)

Q: Is the gosec issue still related to the -tags args?

So you do have a folder named mongo?
That could be an issue for hooks that are file based, but the *-repo hooks shouldn't have an issue with that.

Just the same, you might get in the habit of adding a trailing -- to your args list when an argument value matches a file/folder:

Lemme know if you feel there's a bug in my pre-commit library that we need to keep working on?

from pre-commit-golang.

Xumeiquer avatar Xumeiquer commented on August 24, 2024

Well, I am not 100% sure the issue is with gosec, but I am sure all of your scripts are being called the same way so if some of them work, I don't see a reason for the other to not work.

I'll open an issue on gosec after some investigation. Regarding this issue feel free to close it.

Thank you for your kind and rapid assistence.

from pre-commit-golang.

TekWizely avatar TekWizely commented on August 24, 2024

Closing but feel free to re-open if you do in fact discover a bug, or if gosec isn't meant to be used in ways that I have configured ... Some hooks don't support certain run conditions (per file, per module, etc) ...

from pre-commit-golang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.