make: fix linter in git worktrees

When running `make lint` in a git worktree, the diff processor fails
with "no version control repository found" because the Docker container
only mounts the worktree directory, not the main git directory that the
worktree's .git file references.

This causes golangci-lint's `new-from-rev` filter to not work, resulting
in all ~32k existing lint issues being reported instead of only newly
introduced ones.

Fix by detecting when we're in a worktree (.git is a file, not a
directory) and mounting the main .git directory into the container so
revgrep can access the git history.
This commit is contained in:
Gijs van Dam committed 2026-02-03 19:45:56 +01:00
1 parent 20a6b1726f
commit 2643988524
1 file changed
+18 -5
+18 -5
View File
@@ -75,20 +75,33 @@ endif
# Paths inside container must match GOCACHE/GOMODCACHE in tools/Dockerfile. # Paths inside container must match GOCACHE/GOMODCACHE in tools/Dockerfile.
ifdef CI ifdef CI
# CI mode: bind mount to host paths that GitHub Actions caches. # CI mode: bind mount to host paths that GitHub Actions caches.
DOCKER_TOOLS = docker run \ DOCKER_TOOLS_BASE = docker run \
--rm \ --rm \
-v $${HOME}/.cache/go-build:/tmp/build/.cache \ -v $${HOME}/.cache/go-build:/tmp/build/.cache \
-v $${HOME}/go/pkg/mod:/tmp/build/.modcache \ -v $${HOME}/go/pkg/mod:/tmp/build/.modcache \
-v $${HOME}/.cache/golangci-lint:/root/.cache/golangci-lint \ -v $${HOME}/.cache/golangci-lint:/root/.cache/golangci-lint \
-v $$(pwd):/build lnd-tools -v $$(pwd):/build
DOCKER_TOOLS = $(DOCKER_TOOLS_BASE) lnd-tools
DOCKER_TOOLS_LINT = $(DOCKER_TOOLS)
else else
# Local mode: Docker named volumes for fast macOS/Windows performance. # Local mode: Docker named volumes for fast macOS/Windows performance.
DOCKER_TOOLS = docker run \ # Detect if we're in a git worktree. Use git rev-parse --git-common-dir to get
# the path to the main git directory for the linter's diff processor to work
# correctly with the new-from-rev setting.
GIT_COMMON_DIR := $(shell \
common_dir="$$(git rev-parse --git-common-dir 2>/dev/null)"; \
if [ "$$common_dir" != ".git" ] && [ -n "$$common_dir" ]; then \
echo "$$common_dir"; \
fi)
GIT_VOLUME := $(if $(GIT_COMMON_DIR),-v "$(GIT_COMMON_DIR):$(GIT_COMMON_DIR):ro",)
DOCKER_TOOLS_BASE = docker run \
--rm \ --rm \
-v lnd-go-build-cache:/tmp/build/.cache \ -v lnd-go-build-cache:/tmp/build/.cache \
-v lnd-go-mod-cache:/tmp/build/.modcache \ -v lnd-go-mod-cache:/tmp/build/.modcache \
-v lnd-go-lint-cache:/root/.cache/golangci-lint \ -v lnd-go-lint-cache:/root/.cache/golangci-lint \
-v $$(pwd):/build lnd-tools -v $$(pwd):/build
DOCKER_TOOLS = $(DOCKER_TOOLS_BASE) lnd-tools
DOCKER_TOOLS_LINT = $(DOCKER_TOOLS_BASE) $(GIT_VOLUME) lnd-tools
endif endif
GREEN := "\\033[0;32m" GREEN := "\\033[0;32m"
@@ -358,7 +371,7 @@ check-go-version: check-go-version-dockerfile check-go-version-yaml
#? lint-source: Run static code analysis #? lint-source: Run static code analysis
lint-source: docker-tools lint-source: docker-tools
@$(call print, "Linting source.") @$(call print, "Linting source.")
$(DOCKER_TOOLS) custom-gcl run -v $(LINT_WORKERS) $(DOCKER_TOOLS_LINT) custom-gcl run -v $(LINT_WORKERS)
#? lint-config-check: Verify that the lint config is up to date #? lint-config-check: Verify that the lint config is up to date
# We use the official linter here not our custom one because for checking the # We use the official linter here not our custom one because for checking the