Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions hooks/golang/golangci-lint-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,49 @@ lint_all() {
version_output=$(golangci-lint --version)
echo "$version_output"

## Extract semver number (e.g., 1.54.2 or 2.1.2)
version=$(echo "$version_output" | grep -oP 'version \K[0-9]+\.[0-9]+\.[0-9]+')
## Extract major version number (e.g., 1 or 2)
major_version=$(echo "$version_output" | sed -n 's/.*version \([0-9]*\).*/\1/p')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (reliability): The sed pattern 's/.*version \([0-9]*\).*/\1/p' may silently fail if golangci-lint --version output format changes or doesn't match expectations.

Why this matters: If the extraction fails, $major_version will be empty, causing the comparison [ "$major_version" = "2" ] to always fail. This would silently fall back to 1.x behavior even when running 2.x, potentially causing linter failures or unexpected behavior.

Suggested fix: Add explicit validation after extraction:

Suggested change
major_version=$(echo "$version_output" | sed -n 's/.*version \([0-9]*\).*/\1/p')
## Validate major version was successfully extracted
if [ -z "$major_version" ]; then
echo "Error: Failed to extract golangci-lint major version from: $version_output" >&2
return 1
fi

This ensures the script fails fast with a clear error message rather than silently misbehaving.

## Use correct format flag depending on version
if [ "$(printf '%s\n' "$version" "1.55.0" | sort -V | head -n1)" = "1.55.0" ]; then
# version >= 1.55.0 → use --format
format_flag="--format"
if [ "$major_version" = "2" ]; then
## golangci-lint 2.x
## - Default linters (errcheck, govet, ineffassign, staticcheck, unused) are enabled by default
## - Output format flags changed to --color
golangci-lint run \
--allow-parallel-runners \
--fix \
--verbose \
--color always \
--timeout 5m \
./...
else
# version < 1.55.0 → use --out-format
format_flag="--out-format"
fi
## golangci-lint 1.x
Comment on lines 24 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (consistency): This uses a different extraction pattern (grep -oP) than the major version extraction (line 11 uses sed -n). Consider using a single consistent approach for maintainability.

Why this matters: Using different regex engines (PCRE via grep -P vs sed) makes the code harder to maintain and reason about. If one pattern needs updating, it's unclear whether both should be updated.

Suggested approach: Extract the full version once at the top, then derive both major and full version from it:

Suggested change
else
# version < 1.55.0 → use --out-format
format_flag="--out-format"
fi
## golangci-lint 1.x
## Extract full semver for 1.x format flag detection
version=$(echo "$version_output" | sed -n 's/.*version \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p')

This maintains consistency with the sed-based approach while supporting the 1.55.0 comparison logic.

## Extract full semver number for 1.x format flag detection
version=$(echo "$version_output" | sed -n 's/.*version \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p')

## Use correct format flag depending on version
if [ "$(printf '%s\n' "$version" "1.55.0" | sort -V | head -n1)" = "1.55.0" ]; then
# version >= 1.55.0 → use --format
format_flag="--format"
else
# version < 1.55.0 → use --out-format
format_flag="--out-format"
fi

golangci-lint run \
--allow-parallel-runners \
--fix \
--verbose \
"$format_flag" colored-line-number \
--color always \
--enable errcheck \
--enable gosimple \
--enable govet \
--enable ineffassign \
--enable staticcheck \
--enable unused \
--timeout 5m \
./...
golangci-lint run \
--allow-parallel-runners \
--fix \
--verbose \
"$format_flag" colored-line-number \
--color always \
--enable errcheck \
--enable gosimple \
--enable govet \
--enable ineffassign \
--enable staticcheck \
--enable unused \
--timeout 5m \
./...
fi
return $?
}

Expand Down
Loading