From d51e0d6bd9a3dc119ffcc2535d5d139a69177f8c Mon Sep 17 00:00:00 2001 From: David Beitey Date: Wed, 24 May 2023 15:27:43 +1000 Subject: [PATCH 1/3] feat: pass module dir as --path-prefix arg to golangci-lint This ensures that the path output reported by golangci-lint is qualified with respect to a given linted file's path within the repo. Without this option, just the filename itself is outputted, leading to ambiguity as to which file is problematic when multiple files have the same name (e.g. `main.go`). --- golangci-lint-mod.sh | 1 + golangci-lint-repo-mod.sh | 1 + lib/cmd-mod.bash | 3 +++ lib/cmd-repo-mod.bash | 3 +++ 4 files changed, 8 insertions(+) diff --git a/golangci-lint-mod.sh b/golangci-lint-mod.sh index bec73eb..f326b31 100755 --- a/golangci-lint-mod.sh +++ b/golangci-lint-mod.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) +cmd_pwd_arg="--path-prefix" . "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/golangci-lint-repo-mod.sh b/golangci-lint-repo-mod.sh index 3319f2a..645bc41 100755 --- a/golangci-lint-repo-mod.sh +++ b/golangci-lint-repo-mod.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) +cmd_pwd_arg="--path-prefix" . "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/lib/cmd-mod.bash b/lib/cmd-mod.bash index 4879221..5d6d67b 100644 --- a/lib/cmd-mod.bash +++ b/lib/cmd-mod.bash @@ -14,6 +14,9 @@ error_code=0 # for sub in $(find_module_roots "${FILES[@]}" | sort -u); do pushd "${sub}" > /dev/null || exit 1 + if [ "${cmd_pwd_arg:-}" ]; then + OPTIONS+=("${cmd_pwd_arg}=${sub}") + fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1) if [ -n "${output}" ]; then diff --git a/lib/cmd-repo-mod.bash b/lib/cmd-repo-mod.bash index c44b9d5..1abbcdd 100644 --- a/lib/cmd-repo-mod.bash +++ b/lib/cmd-repo-mod.bash @@ -14,6 +14,9 @@ error_code=0 # for sub in $(find . -name go.mod -not -path '*/vendor/*' -exec dirname "{}" ';' | sort -u); do pushd "${sub}" > /dev/null || exit 1 + if [ "${cmd_pwd_arg:-}" ]; then + OPTIONS+=("${cmd_pwd_arg}=${sub}") + fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1) if [ -n "${output}" ]; then From f69b840e946a4f0516c5ed8ff883eebc0b17c058 Mon Sep 17 00:00:00 2001 From: David Beitey Date: Wed, 7 Jun 2023 10:33:43 +1000 Subject: [PATCH 2/3] Use --hook:path-prefix arg to make golangci-lint --path-prefix optional --- README.md | 2 ++ golangci-lint-mod.sh | 2 +- golangci-lint-repo-mod.sh | 2 +- lib/cmd-mod.bash | 4 ++-- lib/cmd-repo-mod.bash | 4 ++-- lib/common.bash | 5 +++++ 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 50586c8..09a770b 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil # - Can be configured to replace MOST other hooks # - Supports repo config file for configuration # - https://github.com/golangci/golangci-lint + # - Use arg `--hook:path-prefix` to indicate that the repository root + # directory should be passed to the tool (for `*-mod` hooks) # - id: golangci-lint - id: golangci-lint-mod diff --git a/golangci-lint-mod.sh b/golangci-lint-mod.sh index f326b31..6169fdb 100755 --- a/golangci-lint-mod.sh +++ b/golangci-lint-mod.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) -cmd_pwd_arg="--path-prefix" +cmd_cwd_arg="--path-prefix" . "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/golangci-lint-repo-mod.sh b/golangci-lint-repo-mod.sh index 645bc41..fa1b3f8 100755 --- a/golangci-lint-repo-mod.sh +++ b/golangci-lint-repo-mod.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) -cmd_pwd_arg="--path-prefix" +cmd_cwd_arg="--path-prefix" . "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/lib/cmd-mod.bash b/lib/cmd-mod.bash index 5d6d67b..5fea01b 100644 --- a/lib/cmd-mod.bash +++ b/lib/cmd-mod.bash @@ -14,8 +14,8 @@ error_code=0 # for sub in $(find_module_roots "${FILES[@]}" | sort -u); do pushd "${sub}" > /dev/null || exit 1 - if [ "${cmd_pwd_arg:-}" ]; then - OPTIONS+=("${cmd_pwd_arg}=${sub}") + if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then + OPTIONS+=("${cmd_cwd_arg}=${sub}") fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1) diff --git a/lib/cmd-repo-mod.bash b/lib/cmd-repo-mod.bash index 1abbcdd..1434a10 100644 --- a/lib/cmd-repo-mod.bash +++ b/lib/cmd-repo-mod.bash @@ -14,8 +14,8 @@ error_code=0 # for sub in $(find . -name go.mod -not -path '*/vendor/*' -exec dirname "{}" ';' | sort -u); do pushd "${sub}" > /dev/null || exit 1 - if [ "${cmd_pwd_arg:-}" ]; then - OPTIONS+=("${cmd_pwd_arg}=${sub}") + if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then + OPTIONS+=("${cmd_cwd_arg}=${sub}") fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1) diff --git a/lib/common.bash b/lib/common.bash index 9d9da65..f1d8c54 100644 --- a/lib/common.bash +++ b/lib/common.bash @@ -2,6 +2,7 @@ : "${use_dot_dot_dot:=1}" : "${error_on_output:=0}" +: "${use_path_prefix:=0}" : "${ignore_file_pattern_array:=}" ## @@ -66,6 +67,10 @@ function parse_file_hook_args { fi shift ;; + --hook:path-prefix) + use_path_prefix=1 + shift + ;; --hook:*) printf "ERROR: Unknown hook option: '%s'\n" "${1}" >&2 exit 1 From af984cbe16733e1a3d952765a270468fcefefee9 Mon Sep 17 00:00:00 2001 From: David Beitey Date: Tue, 18 Feb 2025 15:43:32 +1000 Subject: [PATCH 3/3] Use templating for cmd_cwd_arg --- golangci-lint-mod.sh | 2 +- golangci-lint-repo-mod.sh | 2 +- lib/cmd-mod.bash | 2 +- lib/cmd-repo-mod.bash | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/golangci-lint-mod.sh b/golangci-lint-mod.sh index 6169fdb..8fe3d3b 100755 --- a/golangci-lint-mod.sh +++ b/golangci-lint-mod.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) -cmd_cwd_arg="--path-prefix" +cmd_cwd_arg="--path-prefix={{sub}}" . "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/golangci-lint-repo-mod.sh b/golangci-lint-repo-mod.sh index fa1b3f8..ba6b9f2 100755 --- a/golangci-lint-repo-mod.sh +++ b/golangci-lint-repo-mod.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) -cmd_cwd_arg="--path-prefix" +cmd_cwd_arg="--path-prefix={{sub}}" . "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/lib/cmd-mod.bash b/lib/cmd-mod.bash index 5fea01b..6fb3fd3 100644 --- a/lib/cmd-mod.bash +++ b/lib/cmd-mod.bash @@ -15,7 +15,7 @@ error_code=0 for sub in $(find_module_roots "${FILES[@]}" | sort -u); do pushd "${sub}" > /dev/null || exit 1 if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then - OPTIONS+=("${cmd_cwd_arg}=${sub}") + OPTIONS+=("${cmd_cwd_arg//\{\{sub\}\}/"$sub"}") fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1) diff --git a/lib/cmd-repo-mod.bash b/lib/cmd-repo-mod.bash index 1434a10..b35271b 100644 --- a/lib/cmd-repo-mod.bash +++ b/lib/cmd-repo-mod.bash @@ -15,7 +15,7 @@ error_code=0 for sub in $(find . -name go.mod -not -path '*/vendor/*' -exec dirname "{}" ';' | sort -u); do pushd "${sub}" > /dev/null || exit 1 if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then - OPTIONS+=("${cmd_cwd_arg}=${sub}") + OPTIONS+=("${cmd_cwd_arg//\{\{sub\}\}/"$sub"}") fi if [ "${error_on_output:-}" -eq 1 ]; then output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)