Coder Social home page Coder Social logo

tekwizely / pre-commit-golang Goto Github PK

View Code? Open in Web Editor NEW
273.0 5.0 33.0 81 KB

Pre-commit hooks for Golang with support for monorepos, the ability to pass arguments and environment variables to all hooks, and the ability to invoke custom go tools.

License: MIT License

Shell 100.00%
golang precommit-hooks mit-license

pre-commit-golang's Introduction

Pre-Commit-GoLang MIT license

A set of git pre-commit hooks for Golang with support for multi-module monorepos, the ability to pass arguments and environment variables to all hooks, and the ability to invoke custom go tools.

Requires the Pre-Commit.com Hook Management Framework.


Installation

You can copy/paste the following snippet into your .pre-commit-config.yaml file.

NOTE A more fleshed-out version can be found in sample-config.yaml

    # ==========================================================================
    # Golang Pre-Commit Hooks | https://github.com/tekwizely/pre-commit-golang
    #
    # !! ALL Hooks enabled by default - Comment out hooks you are not using
    #
    # Visit the project home page to learn more about the available Hooks,
    # including useful arguments you might want to pass into them.
    #
    # NOTE: When passing options to hooks, if your options contain a reference
    #       to an existing file, then you will need to use a trailing '--'
    #       argument to separate the hook options from the modified-file list
    #       that Pre-Commit passes into the hook.
    #       For repo-based hooks, '--' is not needed.
    #
    # NOTE: You can pass environment variables to hooks using args with the 
    #       following format:
    #
    #           --hook:env:NAME=VALUE
    #
    # Consider adding aliases to longer-named hooks for easier CLI usage.
    # ==========================================================================
-   repo: https://github.com/tekwizely/pre-commit-golang
    rev: master
    hooks:
    #
    # Go Build
    #
    -   id: go-build-mod
    -   id: go-build-pkg
    -   id: go-build-repo-mod
    -   id: go-build-repo-pkg
    #
    # Go Mod Tidy
    #
    -   id: go-mod-tidy
    -   id: go-mod-tidy-repo
    #
    # Go Test
    #
    -   id: go-test-mod
    -   id: go-test-pkg
    -   id: go-test-repo-mod
    -   id: go-test-repo-pkg
    #
    # Go Vet
    #
    -   id: go-vet
    -   id: go-vet-mod
    -   id: go-vet-pkg
    -   id: go-vet-repo-mod
    -   id: go-vet-repo-pkg
    #
    # Revive
    #
    -   id: go-revive
    -   id: go-revive-mod
    -   id: go-revive-repo-mod
    #
    # GoSec
    #
    -   id: go-sec-mod
    -   id: go-sec-pkg
    -   id: go-sec-repo-mod
    -   id: go-sec-repo-pkg
    #
    # StaticCheck
    #
    -   id: go-staticcheck-mod
    -   id: go-staticcheck-pkg
    -   id: go-staticcheck-repo-mod
    -   id: go-staticcheck-repo-pkg
    #
    # StructSlop
    #
    -   id: go-structslop-mod
    -   id: go-structslop-pkg
    -   id: go-structslop-repo-mod
    -   id: go-structslop-repo-pkg
    #
    # Formatters
    #
    -   id: go-fmt
    -   id: go-fmt-repo
    -   id: go-fumpt        # replaces go-fmt
    -   id: go-fumpt-repo   # replaces go-fmt-repo
    -   id: go-imports      # replaces go-fmt
    -   id: go-imports-repo # replaces go-fmt-repo
    -   id: go-returns      # replaces go-imports & go-fmt
    -   id: go-returns-repo # replaces go-imports-repo & go-fmt-repo
    #
    # Style Checkers
    #
    -   id: go-lint
    -   id: go-critic
    #
    # GolangCI-Lint
    # - Fast Multi-Linter
    # - Can be configured to replace MOST other hooks
    # - Supports repo config file for configuration
    # - https://github.com/golangci/golangci-lint
    #
    -   id: golangci-lint
    -   id: golangci-lint-mod
    -   id: golangci-lint-pkg
    -   id: golangci-lint-repo-mod
    -   id: golangci-lint-repo-pkg
    #
    # Invoking Custom Go Tools
    # - Configured *entirely* through the `args` attribute, ie:
    #   args: [ go, test, ./... ]
    # - Use arg `--hook:error-on-output` to indicate that any output from the tool
    #   should be treated as an error.
    # - Use the `name` attribute to provide better messaging when the hook runs
    # - Use the `alias` attribute to be able to invoke your hook via `pre-commit run`
    #
    -   id: my-cmd
    -   id: my-cmd-mod
    -   id: my-cmd-pkg
    -   id: my-cmd-repo
    -   id: my-cmd-repo-mod
    -   id: my-cmd-repo-pkg

Overview

Hook Targets

File-Based Hooks

Some hooks run against matching staged files individually.

Module-Based Hooks

Some hooks work on a per-module basis. The hooks run against module root folders containing one or more matching staged files.

Module Root Folder: A folder containing a go.mod file. Discovered by walking up the folder path from the staged file.

Module Mode

Module-based hooks enable module mode (GO111MODULE=on) before invoking their respective tools.

Package-Based Hooks

Some hooks work on a per-package basis. The hooks run against folders containing one or more staged files.

Package Folder: A folder containing one or more .go files.

Package Mode

Package-based hooks disable module mode (GO111MODULE=off) before invoking their respective tools.

Repo-Based Hooks

Some hooks run against the entire repo. The hooks only run once (if any matching files are staged), and are NOT provided the list of staged files,


Hook Suffixes

Hooks have suffixes in their name that indicate their targets:

Suffix Target Description
<none> Files Targets staged files directly
-mod Module Targets module root folders of staged .go files
-pkg Package Targets folders containing staged .go files
-repo Repo Root Targets the repo root folder
-repo-mod All Modules Targets all module root folders in the repo
-repo-pkg All Packages Targets all package folders in the repo

Multiple Hook Invocations

Due to OS command-line-length limits, Pre-Commit can invoke a hook multiple times if a large number of files are staged.

For file and repo-based hooks, this isn't an issue, but for module and package-based hooks, there is a potential for the hook to run against the same module or package multiple times, duplicating any errors or warnings.


Invoking Custom Tools

While this project includes builtin hooks for many popular go tools, it's not possible to include builtin hooks for every tool that users might want to use.

To help accommodate those users, this project includes the ability to invoke custom go tools.

See the my-cmd hooks for more information.


Useful Hook Parameters

-   id: hook-id
    args: [arg1, arg2, ..., '--'] # Pass options ('--' is optional)
    always_run: true              # Run even if no matching files staged
    alias: hook-alias             # Create an alias
    verbose: true                 # Display output, even if no errors

Passing Options To Hooks

You can pass options into individual hooks to customize tool behavior.

If your options contain a reference to an existing file, then you will need to use a trailing '--' argument to separate the hook options from the modified-file list that Pre-Commit passes into the hook.

NOTE: For repo-based hooks, '--' is not needed.

See each hook's description below for some popular options that you might want to use.

Additionally, you can view each tool's individual home page or help settings to learn about all the available options.

Passing Environment Variables To Hooks

You can pass environment variables to hooks to customize tool behavior.

NOTE: The Pre-Commit framework does not directly support the ability to pass environment variables to hooks.

This feature is enabled via support for a specially-formatted argument:

  • --hook:env:NAME=VALUE

The hook script will detect this argument and set the variable NAME to the value VALUE before invoking the configured tool.

You can pass multiple --hook:env: arguments.

The arguments can appear anywhere in the args: list.

Always Run

By default, hooks ONLY run when matching file types (usually *.go) are staged.

When configured to "always_run", a hook is executed as if EVERY matching file were staged.

Aliases / Names

pre-commit supports the ability to assign both an alias and a name to a configured hook:

config description
alias (optional) allows the hook to be referenced using an additional id when using pre-commit run <hookid>
name (optional) override the name of the hook - shown during hook execution

These are beneficial for a couple of reasons:

  • Creating short names for long-named hooks for easier CLI usage:
# ...
      hooks:
        -   id: go-build-repo-mod
            alias: build

usage

$ pre-commit run build
  • Having variations of a given hook with different configurations:
# ...
    hooks:
      -   id: go-fmt

          # Add a second go-fmt hook with -w enabled
          # Configure so it only runs when manually invoked
      -   id: go-fmt
          args: [ -w ]
          alias: go-fmtw-alias
          name: go-fmtw-name
          stages: [manual]

NOTE: When creating variations, take note that the alias is used to execute the hook, but the the name is used in the hook report.

usage: alias vs name

 $ pre-commit run --hook-stage manual go-fmtw-alias

 go-fmtw-name................................Passed

Verbose Hook Output

When the "verbose" flag is enabled, all output generated by the hook will be displayed, even if there were no errors.

This can be useful, for example, for hooks that display warnings, but don't generate error codes for them.


Hooks


go-build

Compiles packages, along with their dependencies, but does not install the results.

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

Comes with Golang ( golang.org )

Help

go-mod-tidy

Makes sure go.mod matches the source code in the module.

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

Comes with Golang ( golang.org )

Help

go-test

Automates testing, printing a summary of test results.

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

Comes with Golang ( golang.org )

Help

go-sec

Inspects source code for security problems by scanning the Go AST.

Hook ID Description
go-sec-mod Run 'cd $(mod_root $FILE); gosec [$ARGS] ./...' for each staged .go file
go-sec-pkg Run 'gosec [$ARGS] ./$(dirname $FILE)' for each staged .go file
go-sec-repo-mod Run 'cd $(mod_root); gosec [$ARGS] ./...' for each module in the repo
go-sec-repo-pkg Run 'gosec [$ARGS] ./...' in repo root folder
Install (via bingo)
bingo install github.com/securego/gosec/v2/cmd/gosec
Help

go-staticcheck

A state of the art linter for the Go programming language. Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules.

Hook ID Description
go-staticcheck-mod Run 'cd $(mod_root $FILE); staticcheck [$ARGS] ./...' for each staged .go file
go-staticcheck-pkg Run 'staticcheck [$ARGS] ./$(dirname $FILE)' for each staged .go file
go-staticcheck-repo-mod Run 'cd $(mod_root); staticcheck [$ARGS] ./...' for each module in the repo
go-staticcheck-repo-pkg Run 'staticcheck [$ARGS] ./...' in repo root folder
Install (via bingo)
bingo install honnef.co/go/tools/cmd/staticcheck
Help

go-structslop

Recommends struct field rearrangements to provide for maximum space/allocation efficiency.

  • Can modify files (see -apply)
Hook ID Description
go-structslop-mod Run 'cd $(mod_root $FILE); structslop [$ARGS] ./...' for each staged .go file
go-structslop-pkg Run 'structslop [$ARGS] ./$(dirname $FILE)' for each staged .go file
go-structslop-repo-mod Run 'cd $(mod_root); structslop [$ARGS] ./...' for each module in the repo
go-structslop-repo-pkg Run 'structslop [$ARGS] ./...' in repo root folder
Install (via bingo)
bingo install github.com/orijtech/structslop/cmd/structslop
Useful Args
-apply : apply suggested fixes
Help

go-vet

Examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.

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
Install

Comes with Golang ( golang.org )

Help

go-fmt

Formats Go programs. It uses tabs for indentation and blanks for alignment. Alignment assumes that an editor is using a fixed-width font.

  • Can modify files (see -w)
Hook ID Description
go-fmt Run 'gofmt -l -d [$ARGS] $FILE' for each staged .go file
go-fmt-repo Run 'gofmt -l -d [$ARGS] .' in repo root folder
Install

Comes with Golang ( golang.org )

Useful Args
-d=false : Hide diffs
-s       : Try to simplify code
-w       : Update source file directly
Help

go-fumpt

Enforce a stricter format than gofmt, while being backwards compatible.

  • Replaces go-fmt
  • Can modify files (see -w)
Hook ID Description
go-fumpt Run 'gofumpt -l -d [$ARGS] $FILE' for each staged .go file
go-fumpt-repo Run 'gofumpt -l -d [$ARGS] .' in repo root folder
Install (via bingo)
bingo install mvdan.cc/gofumpt
Useful Args
-d=false : Hide diffs
-extra   : Enable extra rules which should be vetted by a human
-s       : Try to simplify code
-w       : Update source file directly
Help

go-imports

Updates your Go import lines, adding missing ones and removing unreferenced ones.

  • Replaces go-fmt
  • Can modify files (see -w)
Hook ID Description
go-imports Run 'goimports -l -d [$ARGS] $FILE' for each staged .go file
go-imports-repo Run 'goimports -l -d [$ARGS] .' in repo root folder
Install (via bingo)
bingo install golang.org/x/tools/cmd/goimports
Useful Args
-d=false        : Hide diffs
-format-only    : Do not fix imports, act ONLY as go-fmt
-local prefixes : Add imports matching prefix AFTER 3rd party packages
                  (prefixes = comma-separated list)
-v              : Verbose logging
-w              : Update source file directly
Help

go-returns

Implements a Go pretty-printer (like go-fmt) that also adds zero-value return values as necessary to incomplete return statements.

  • Replaces go-fmt and go-imports
  • Can modify files (see -w)
Hook ID Description
go-returns Run 'goreturns -l -d [$ARGS] $FILE' for each staged .go file
go-returns-repo Run 'goreturns -l -d [$ARGS] .' in repo root folder
Install (via bingo)
bingo install github.com/sqs/goreturns
Useful Args
-b              : Remove bare returns
-d=false        : Hide diffs
-i=false        : Disable go-imports
-local prefixes : Add imports matching prefix AFTER 3rd party packages
                  (prefixes = comma-separated list)
-p              : Print non-fatal type-checking errors to STDERR
-s              : Try to simplify code
-w              : Update source file directly
Help

go-lint

A linter for Go source code, meant to carry out the stylistic conventions put forth in Effective Go and CodeReviewComments.

Hook ID Description
go-lint Run 'golint -set_exit_status [$ARGS] $FILE' for each staged .go file
Install (via bingo)
bingo install golang.org/x/lint/golint
Help

go-revive

~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.

Hook ID Description
go-revive Run 'revive [$ARGS] $FILE' for each staged .go file
go-revive-mod Run 'cd $(mod_root $FILE); revive [$ARGS] ./...' for each staged .go file
go-revive-repo-mod Run 'cd $(mod_root); revive [$ARGS] ./...' for each module in the repo
Support for Repository-Level Config

As of time of writing, revive only auto-checks for configs in ${HOME}/revive.toml, and doesn't check the local folder (ie. ${REPO_ROOT}/revive.toml).

To make revive more useful, these hooks add built-in support for a repository-level config file.

Auto-Configured

These hooks are configured to auto-add -config=revive.toml when the file is present in the repository root.

Triggerred When Modified

These hooks are configured to run when the repo-level revive.toml file is modified (and staged).

NOTE: Although configured to run, the file-based go-revive hook will, by default, effectively do nothing if there are no staged .go files to run against.

Install (via bingo)
bingo install github.com/mgechev/revive
Useful Args
-config [PATH]     : Path to config file (TOML)
-exclude [PATTERN] : Pattern for files/directories/packages to be excluded from linting
-formatter [NAME]  : formatter to be used for the output
Displaying Warnings

By default, revive doesn't generate errors on warnings, so warning messages may not be displayed if there are no accompanying error messages.

You can use the "verbose: true" hook configuration to always show hook output.

Help

go-critic

The most opinionated Go source code linter for code audit.

Hook ID Description
go-critic Run 'gocritic check [$ARGS] $FILE' for each staged .go file
Install

https://github.com/go-critic/go-critic#installation

Useful Args
-enableAll        : Enable ALL checkers
-enable checkers  : comma-separated list of checkers to be enabled
                    (can include #tags)
-disable checkers : comma-separated list of checkers to be disabled
                    (can include #tags)

Tags

  • #diagnostic
  • #style
  • #opinionated
  • #performance
  • #experimental

Help


golangci-lint

A FAST linter aggregator, with colored output, fewer false-positives, and support for yaml/toml configuration.

  • Manages multiple linters
  • Can replace many/most other hooks
  • Can report only new issues (see --new)
  • Can modify files (see --fix)
Hook ID Description
golangci-lint Run 'golangci-lint run [$ARGS] $FILE' for each staged .go file
golangci-lint-mod Run 'cd $(mod_root $FILE); golangci-lint run [$ARGS] ./...' for each staged .go file
golangci-lint-pkg Run 'golangci-lint run [$ARGS] ./$(dirname $FILE)' for each staged .go file
golangci-lint-repo-mod Run 'cd $(mod_root); golangci-lint run [$ARGS] ./...' for each module in the repo
golangci-lint-repo-pkg Run 'golangci-lint run [$ARGS] ./...' in repo root folder
Install (via bingo)
bingo install github.com/golangci/golangci-lint/cmd/golangci-lint
Useful Args
   --config PATH     : Specify config file
   --disable linters : Disable specific linter(s)
   --enable-all      : Enable ALL linters
   --enable linters  : Enable specific linter(s)
   --fast            : Run only fast linters (from enabled linters sets)
   --fix             : Fix found issues (if supported by linter)
   --new             : Show only new issues (see help for further details)
   --no-config       : don't read config file
   --presets presets : Enable presets of linters

Presets

  • bugs
  • complexity
  • format
  • performance
  • style
  • unused
Help
Config File Help:

my-cmd

Using the my-cmd-* hooks, you can invoke custom go tools in various contexts.

Hook ID Description
my-cmd Run '$ARGS[0] [$ARGS[1:]] $FILE' for each staged .go file
my-cmd-mod Run 'cd $(mod_root $FILE); GO111MODULE=on $ARGS[0] [$ARGS[1:]]' for each staged .go file
my-cmd-pkg Run 'GO111MODULE=off $ARGS[0] [$ARGS[1:]] ./$(dirname $FILE)' for each staged .go file
my-cmd-repo Run '$ARGS[0] [$ARGS[1:]]' in the repo root folder
my-cmd-repo-mod Run 'cd $(mod_root); GO111MODULE=on $ARGS[0] [$ARGS[1:]]' for each module in the repo
my-cmd-repo-pkg Run 'GO111MODULE=off $ARGS[0] [$ARGS[1:]] in repo root folder

Configuring the hooks

The my-cmd hooks are configured entirely through the pre-commit args attribute, including specifying which tool to run (ie $ARGS[0] above).

This includes the need to manually add the ./... target for module-based tools that require it.

Examples

Here's an example of what it would look like to use the my-cmd hooks to invoke go test if it wasn't already included:

.pre-commit-config.yaml

# ...
      hooks:
            # Run 'cd $(mod_root $FILE); go test ./...' for each staged .go file
        -   id: my-cmd-mod
            name: go-test-mod
            alias: go-test-mod
            args: [ go, test, ./... ]
Names & Aliases

It is recommended that you use both name and alias attributes when defining my-cmd hooks.

The name will provide better messaging when the hook runs.

The alias will enable you to invoke the hook manually from the command-line when needed (see pre-commit help run)

error-on-output

Some tools, like gofmt, goimports, and goreturns, don't generate error codes, but instead expect the presence of any output to indicate warning/error conditions.

The my-cmd hooks accept a --hook:error-on-output argument to indicate this behavior.

Here's an example of what it would look like to use the my-cmd hooks to invoke gofmt if it wasn't already included:

.pre-commit-config.yaml

# ...
      hooks:
            # Run 'gofmt -l -d $FILE' for each staged .go file
            # Treat any output as indication of failure
        -   id: my-cmd
            name: go-fmt
            alias: go-fmt
            args: [ gofmt, -l, -d, --hook:error-on-output]

NOTE: The plain --error-on-output option is now deprecated, but still supported, as long as it's the very first entry in the args: list.


License

The tekwizely/pre-commit-golang project is released under the MIT License. See LICENSE file.

pre-commit-golang's People

Contributors

adambabik avatar jfroche avatar tekwizely avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pre-commit-golang's Issues

Use `exclude` or another method to cause `cmd-repo-mod.bash` to ignore certain directories

cmd-repo-mod.bash will find all directories with a Go module except those that match */vendor/*; however, there are times when I need it to ignore more patterns then this.

For the moment, I have forked the repo and just hard-coded a workaround, but it would be really useful if that script would respect exclude or have some other mechanic to pass in a list of patterns.

Unable to pass `-config .revive.toml` to revive hook.

I've tried multiple ways to pass a config file to revive, but it errors out every time:

  - repo: https://github.com/tekwizely/pre-commit-golang
    rev: v1.0.0-rc.1
    hooks:
      - id: go-fmt
      - id: go-revive
        args: [-config, .revive.toml]
cannot parse the config file: toml: line 1: expected '.' or '=', but got 'c' instead
cannot parse the config file: toml: line 1: expected '.' or '=', but got '/' instead
cannot parse the config file: toml: line 1: expected '.' or '=', but got 'c' instead
cannot parse the config file: toml: line 1: expected '.' or '=', but got 'm' instead

Running it manually with revive -config .revive.toml works completely fine.

      - id: go-revive
        args: ["--", -config, .revive.toml]

flag needs an argument: -config

      - id: go-revive
        args: ["--", "-config", ".revive.toml"]

flag needs an argument: -config

      - id: go-revive
        args: ["-config .revive.toml"]

flag provided but not defined: -config .revive.toml

go-mod-tidy-repo should not update submodules

go-mod-tidy-repo is running for every directory and is updating go dependencies inside submodules which I am not supposed to touch.
It should run for directories for current git repo.
I believe git ls-files can be used to not touch submodules.

Building multiple modules

Hi TW

I have a project structure which looks like below (monorepo where I have multiple go applications inside the /cmd folder) and they consume their library code from internal directory

image

All the application main.go is inside

cmd/app1/main.go (all the shared code is inside internal/app/app1 & internal/pkg directory)
cmd/app2/main.go (all the shared code is inside internal/app/app2 & internal/pkg directory)
...

Top level go.mod & go.sum is common for all applications

Now how can i use pre-commit-golang hooks to build each application ?

As per my directory structure, I have few private packages under internal/pkg/common/types directory and I see an error

internal/mocks/MessagingClient.go:6:2: package test-module/internal/pkg/common/types is not in GOROOT (/Users/rk/sdk/go1.16.6/src/test-module/internal/pkg/common/types)

false typecheck failures only occuring in pre-commt lint check

I'm attempting to use this pre-commit hook in a number of my projects, but am consistently facing problems with false typecheck errors. When I run golangci-lint in the following cases:

  • manually on the command line with: golangci-lint run
  • within visual studio via golang plugin
  • in github actions ci

these typecheck failures do not occur. They only occur as part of the pre-commit execution. They are usually to do with an identifier not existing but they clearly do otherwise these other ways of running the lint would fail, and the test wouldn't pass and the executbles/library would not be runnable and they are.

So what is the issue here? I have also learned that typecheck is not a real linter, so it can't be disabled, so that's not a workaround. To be honest, I can't see the point of typecheck as it doesn't catch issues that wouldn't be caught by the compiler, its just proving to be a big headache.

Examples of the type of errors I'm seeing can be found here:

If there is a work-around for this problem, please can you let me know.

Unknown flag --config with golangci-lint

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

Gosec fails when running as pre commit hook

My hook is configured as below

- repo: https://github.com/tekwizely/pre-commit-golang
  rev: 645165368711be670d1945c15060e746f5ed15c6
  hooks:
    - id: go-sec-repo-pkg
      args:
        - "-exclude-generated"

It should be noted that running gosec from the command like works just fine. Specifically I'm using the below command successfully

gosec -exclude-generated ./...

Both places gosec is installed using the below command

go install github.com/securego/gosec/v2/cmd/gosec@master

When I try to use gosec using the hooks provided by this repository I get a bunch of the following errors

...
Results:

Golang errors in file: [/home/tons/IdeaProjects/dhis2/im-database-manager/cmd/serve/main.go]:

  > [line 33 : column 2] - could not import github.com/aws/aws-sdk-go-v2/feature/s3/manager (invalid package name: "")

  > [line 35 : column 11] - could not import github.com/aws/aws-sdk-go-v2/config (invalid package name: "")

  > [line 36 : column 2] - could not import github.com/aws/aws-sdk-go-v2/service/s3 (invalid package name: "")

  > [line 38 : column 2] - could not import github.com/dhis2-sre/im-database-manager/internal/handler (invalid package name: "")

  > [line 39 : column 2] - could not import github.com/dhis2-sre/im-database-manager/internal/server (invalid package name: "")

  > [line 40 : column 2] - could not import github.com/dhis2-sre/im-database-manager/pkg/config (invalid package name: "")

  > [line 41 : column 2] - could not import github.com/dhis2-sre/im-database-manager/pkg/database (invalid package name: "")

  > [line 42 : column 2] - could not import github.com/dhis2-sre/im-database-manager/pkg/storage (invalid package name: "")

  > [line 43 : column 17] - could not import github.com/dhis2-sre/im-manager/pkg/client (invalid package name: "")

  > [line 44 : column 13] - could not import github.com/dhis2-sre/im-user/pkg/client (invalid package name: "")


Golang errors in file: [/home/tons/IdeaProjects/dhis2/im-database-manager/internal/apperror/apperror.go]:

  > [line 9 : column 2] - could not import github.com/gofrs/uuid (invalid package name: "")


Golang errors in file: [/home/tons/IdeaProjects/dhis2/im-database-manager/internal/handler/authentication.go]:

  > [line 10 : column 2] - could not import github.com/dhis2-sre/im-user/swagger/sdk/models (invalid package name: "")

  > [line 12 : column 2] - could not import github.com/dhis2-sre/im-database-manager/internal/apperror (invalid package name: "")

  > [line 13 : column 2] - could not import github.com/dhis2-sre/im-database-manager/pkg/config (invalid package name: "")

  > [line 15 : column 2] - could not import github.com/lestrrat-go/jwx/jwa (invalid package name: "")

  > [line 16 : column 2] - could not import github.com/lestrrat-go/jwx/jwk (invalid package name: "")

  > [line 17 : column 2] - could not import github.com/lestrrat-go/jwx/jwt (invalid package name: "")
...

This happens both locally and when running on GitHub as an Action (which can be seen here)

Any idea what goes wrong?

Basic list of hooks for beginning Go devs

I'm learning Go and I love pre-commit. This repo has A LOT of hooks, many of them overlap and I'm a bit overwhelmed.

What is a beginner-friendly list of hooks to use for a small project?

Maybe an idea to put a simple example.yaml as a TL;DR in the README

golangci-lint-repo-mod doesn't respect skip-dirs

for sub in $(find . -name go.mod | xargs -n1 dirname | sort -u) ; do

➜  ldap git:(master) ✗ find . -name go.mod | xargs -n1 dirname | sort -u  
│.
│./vendor/github.com/caddyserver/caddy
│./vendor/github.com/cespare/xxhash/v2
│./vendor/github.com/google/uuid
│./vendor/github.com/miekg/dns
│./vendor/github.com/prometheus/procfs
│./vendor/google.golang.org/grpc
│./vendor/gopkg.in/ldap.v3

Does override: from golangci-lint docs

      --skip-dirs-use-default          Use or not use default excluded directories:
                                         - (^|/)vendor($|/)
                                         - (^|/)third_party($|/)
                                         - (^|/)testdata($|/)
                                         - (^|/)examples($|/)
                                         - (^|/)Godeps($|/)
                                         - (^|/)builtin($|/)
                                        (default true)

As a result execution is too slow.

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.