pre-commit-golang icon indicating copy to clipboard operation
pre-commit-golang copied to clipboard

Unknown flag --config with golangci-lint

Open Dysta opened this issue 2 years ago • 4 comments

Hello, after reading the doc, I faced an issue that I don't really understand.

Here is my precommit config :

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml

-   repo: https://github.com/tekwizely/pre-commit-golang
    rev: v1.0.0-rc.1
    # ? add a hook in the order you want to run it
    hooks:
    -   id: go-fmt
        name: go fmt
    -   id: golangci-lint
        name: go lint
        args: [--config .golangci.yaml]

    -   id: go-test-pkg
        name: go test
        args: ["--hook:env:GO111MODULE=on"]
        stages: [push]

But the run result in a weird error: ERRO Can't get config for command line: can't parse args: unknown flag: --config .golangci.yaml

Since the doc itself recommande to use the --config args, idk what i'm doing wrong to have this error.

I tested with

        args: ["--", --config .golangci.yaml]

since i read that the first "--" is consumed but I'm really blocked rn.

Thank you for help

Dysta avatar Jan 09 '24 14:01 Dysta

Hi @Dysta - After a quick glance at your description, I believe what you need is:

args: [--config, .golangci.yaml]

notice the ,

args: [] is an array type and arguments that you would separate with spaces on the command line need to passed as individual array elements here.

The pre-commit code passes each individual argument in a way that treats spaces as part of the same argument. i.e. without the ',' you're passing a single argument with value "--config .golangci.yaml" which the program doesn't recognize.

Hope that helps ! Give it a try and let me know how it goes - Thanks for taking the time to report an issue and for using my project !

-TW

TekWizely avatar Jan 19 '24 23:01 TekWizely

Hi @Dysta just following up to see if you were able to fix your error?

TekWizely avatar Jan 25 '24 17:01 TekWizely

Hello, sorry for late response, not enough time atm, i'll let you know this monday

Dysta avatar Jan 26 '24 17:01 Dysta

Hello, sorry for late response, not enough time atm, i'll let you know this monday

@Dysta No problem, just want to make sure you get it working - Lemme know when you have a chance to look at it.

TekWizely avatar Feb 02 '24 16:02 TekWizely

Hello, sorry for very late response. It tested with

hooks:
    -   id: go-fmt
        name: go fmt
    -   id: golangci-lint
        name: go lint
        # entry: golangci-lint run --config .golangci.yaml --timeout 5m0s
        args: [--config, .golangci.yaml, --timeout, 5m0s]

and still the same error :

ERRO Can't read config: can't read viper config: open --timeout: no such file or directory 
ERRO Can't read config: can't read viper config: open 5m0s: no such file or directory 
ERRO Can't read config: can't read viper config: Unsupported Config Type "go"       

Note that the "entry" is a workaround that work well

Dysta avatar Mar 28 '24 16:03 Dysta

@Dysta Sorry I forgot an important part, I believe you need:

args: [--config, .golangci.yaml, --timeout, 5m0s, --]

Because your args contain a reference to an existing file, you need to pass a trailing -- so the hooks know where the args stop and the actual file list begins.

Please try that and report your findings,

-TW

TekWizely avatar Mar 31 '24 23:03 TekWizely

@Dysta Just checking to see if you tested the trialing -- fix ?

TekWizely avatar Apr 17 '24 15:04 TekWizely

Hello, sorry again for the late response, with the trailing -- its working like a charm 👍 Not very clear in the doc thought

Dysta avatar Apr 23 '24 15:04 Dysta