diff --git a/tools/lint-go-gopls.sh b/tools/lint-go-gopls.sh index 2cd26ca6fe..63f349fe43 100755 --- a/tools/lint-go-gopls.sh +++ b/tools/lint-go-gopls.sh @@ -7,11 +7,29 @@ IGNORE_PATTERNS=( "is deprecated" # TODO: fix these ) +# Install gopls if not already installed, then use the installed binary for +# faster execution. Using 'go run' each time adds overhead. +"$GO" install "$GOPLS_PACKAGE" +GOPLS_BIN=$("$GO" env GOPATH)/bin/gopls + +# Verify gopls was installed successfully +if [[ ! -x "$GOPLS_BIN" ]]; then + echo "Error: Failed to install gopls" >&2 + exit 1 +fi + +# Parallelism is configurable via GOPLS_PARALLEL env var. +# Default to 1 (sequential) to avoid memory pressure on local machines. +# CI can set a higher value if desired (e.g., GOPLS_PARALLEL=4). +PARALLEL=${GOPLS_PARALLEL:-1} + # lint all go files with 'gopls check' and look for lines starting with the # current absolute path, indicating a error was found. This is necessary # because the tool does not set non-zero exit code when errors are found. # ref: https://github.com/golang/go/issues/67078 -ERROR_LINES=$("$GO" run "$GOPLS_PACKAGE" check -severity=warning "$@" 2>/dev/null | grep -E "^$PWD" | grep -vFf <(printf '%s\n' "${IGNORE_PATTERNS[@]}")); +# Use xargs with configurable parallelism (-P) to process files. +# Use null-delimited format (-0) to handle filenames with spaces correctly. +ERROR_LINES=$(printf '%s\0' "$@" | xargs -0 -P "$PARALLEL" -n 100 "$GOPLS_BIN" check -severity=warning 2>/dev/null | grep -E "^$PWD" | grep -vFf <(printf '%s\n' "${IGNORE_PATTERNS[@]}")); NUM_ERRORS=$(echo -n "$ERROR_LINES" | wc -l) if [ "$NUM_ERRORS" -eq "0" ]; then