Skip to content

Commit 1fbf2d2

Browse files
authored
Fix golangci-lint linter when multiple modules exist (tinkerbell#45)
## Description * Uses full paths to executables, so changed dirs don't matter * Switches to always using `find -execdir` to run golangci-lint to avoid an seldomly used corner case bit rotting * Uses the correct variable name for the golangci-lint config file ## Why is this needed Fixes: tinkerbell#39 Closes: tinkerbell#40 ## How Has This Been Tested? Ran `make lint` a bunch in hook to make sure it works. ## How are existing users impacted? What migration steps/scripts do we need? golangci-lint will actually run if multiple modules are detected (or added w/o having to re-run lint-install).
2 parents 2fdfa62 + 9a1461e commit 1fbf2d2

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

Makefile

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ LINTERS :=
2121
FIXERS :=
2222

2323
SHELLCHECK_VERSION ?= v0.8.0
24-
SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
24+
SHELLCHECK_BIN := $(LINT_ROOT)/out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
2525
$(SHELLCHECK_BIN):
26-
mkdir -p out/linters
26+
mkdir -p $(LINT_ROOT)/out/linters
2727
curl -sSfL -o $@.tar.xz https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_OS_LOWER).$(LINT_ARCH).tar.xz \
2828
|| echo "Unable to fetch shellcheck for $(LINT_OS)/$(LINT_ARCH): falling back to locally install"
2929
test -f $@.tar.xz \
30-
&& tar -C out/linters -xJf $@.tar.xz \
31-
&& mv out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@ \
30+
&& tar -C $(LINT_ROOT)/out/linters -xJf $@.tar.xz \
31+
&& mv $(LINT_ROOT)/out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@ \
3232
|| printf "#!/usr/bin/env shellcheck\n" > $@
3333
chmod u+x $@
3434

@@ -41,9 +41,9 @@ shellcheck-fix: $(SHELLCHECK_BIN)
4141
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
4242

4343
HADOLINT_VERSION ?= v2.8.0
44-
HADOLINT_BIN := out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
44+
HADOLINT_BIN := $(LINT_ROOT)/out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
4545
$(HADOLINT_BIN):
46-
mkdir -p out/linters
46+
mkdir -p $(LINT_ROOT)/out/linters
4747
curl -sSfL -o $@.dl https://github.com/hadolint/hadolint/releases/download/$(HADOLINT_VERSION)/hadolint-$(LINT_OS)-$(LINT_ARCH) \
4848
|| echo "Unable to fetch hadolint for $(LINT_OS)/$(LINT_ARCH), falling back to local install"
4949
test -f $@.dl && mv $(HADOLINT_BIN).dl $@ || printf "#!/usr/bin/env hadolint\n" > $@
@@ -55,28 +55,28 @@ hadolint-lint: $(HADOLINT_BIN)
5555

5656
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
5757
GOLANGCI_LINT_VERSION ?= v1.43.0
58-
GOLANGCI_LINT_BIN := out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
58+
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
5959
$(GOLANGCI_LINT_BIN):
60-
mkdir -p out/linters
61-
rm -rf out/linters/golangci-lint-*
62-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
63-
mv out/linters/golangci-lint $@
60+
mkdir -p $(LINT_ROOT)/out/linters
61+
rm -rf $(LINT_ROOT)/out/linters/golangci-lint-*
62+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LINT_ROOT)/out/linters $(GOLANGCI_LINT_VERSION)
63+
mv $(LINT_ROOT)/out/linters/golangci-lint $@
6464

6565
LINTERS += golangci-lint-lint
6666
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
67-
$(GOLANGCI_LINT_BIN) run
67+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" \;
6868

6969
FIXERS += golangci-lint-fix
7070
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
71-
$(GOLANGCI_LINT_BIN) run --fix
71+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
7272

7373
YAMLLINT_VERSION ?= 1.26.3
74-
YAMLLINT_ROOT := out/linters/yamllint-$(YAMLLINT_VERSION)
74+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
7575
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
7676
$(YAMLLINT_BIN):
77-
mkdir -p out/linters
78-
rm -rf out/linters/yamllint-*
79-
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
77+
mkdir -p $(LINT_ROOT)/out/linters
78+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
79+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
8080
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
8181

8282
LINTERS += yamllint-lint

Makefile.tmpl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ FIXERS :=
2222

2323
{{ if .Shell -}}
2424
SHELLCHECK_VERSION ?= v0.8.0
25-
SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
25+
SHELLCHECK_BIN := $(LINT_ROOT)/out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
2626
$(SHELLCHECK_BIN):
27-
mkdir -p out/linters
27+
mkdir -p $(LINT_ROOT)/out/linters
2828
curl -sSfL -o [email protected] https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_OS_LOWER).$(LINT_ARCH).tar.xz \
2929
|| echo "Unable to fetch shellcheck for $(LINT_OS)/$(LINT_ARCH): falling back to locally install"
3030
31-
&& tar -C out/linters -xJf [email protected] \
32-
&& mv out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@ \
31+
&& tar -C $(LINT_ROOT)/out/linters -xJf [email protected] \
32+
&& mv $(LINT_ROOT)/out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@ \
3333
|| printf "#!/usr/bin/env shellcheck\n" > $@
3434
chmod u+x $@
3535

@@ -45,9 +45,9 @@ shellcheck-fix: $(SHELLCHECK_BIN)
4545

4646
{{ if .Dockerfile -}}
4747
HADOLINT_VERSION ?= v2.8.0
48-
HADOLINT_BIN := out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
48+
HADOLINT_BIN := $(LINT_ROOT)/out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
4949
$(HADOLINT_BIN):
50-
mkdir -p out/linters
50+
mkdir -p $(LINT_ROOT)/out/linters
5151
curl -sSfL -o [email protected] https://github.com/hadolint/hadolint/releases/download/$(HADOLINT_VERSION)/hadolint-$(LINT_OS)-$(LINT_ARCH) \
5252
|| echo "Unable to fetch hadolint for $(LINT_OS)/$(LINT_ARCH), falling back to local install"
5353
test -f [email protected] && mv $(HADOLINT_BIN).dl $@ || printf "#!/usr/bin/env hadolint\n" > $@
@@ -62,12 +62,12 @@ hadolint-lint: $(HADOLINT_BIN)
6262
{{ if .Go -}}
6363
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
6464
GOLANGCI_LINT_VERSION ?= v1.43.0
65-
GOLANGCI_LINT_BIN := out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
65+
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
6666
$(GOLANGCI_LINT_BIN):
67-
mkdir -p out/linters
68-
rm -rf out/linters/golangci-lint-*
69-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
70-
mv out/linters/golangci-lint $@
67+
mkdir -p $(LINT_ROOT)/out/linters
68+
rm -rf $(LINT_ROOT)/out/linters/golangci-lint-*
69+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LINT_ROOT)/out/linters $(GOLANGCI_LINT_VERSION)
70+
mv $(LINT_ROOT)/out/linters/golangci-lint $@
7171

7272
LINTERS += golangci-lint-lint
7373
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
@@ -81,12 +81,12 @@ golangci-lint-fix: $(GOLANGCI_LINT_BIN)
8181

8282
{{ if .YAML -}}
8383
YAMLLINT_VERSION ?= 1.26.3
84-
YAMLLINT_ROOT := out/linters/yamllint-$(YAMLLINT_VERSION)
84+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
8585
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
8686
$(YAMLLINT_BIN):
87-
mkdir -p out/linters
88-
rm -rf out/linters/yamllint-*
89-
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
87+
mkdir -p $(LINT_ROOT)/out/linters
88+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
89+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
9090
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
9191

9292
LINTERS += yamllint-lint

lint-install.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,7 @@ func goLintCmd(root string, level string, fix bool) string {
254254
}
255255

256256
klog.Infof("found %d modules within %s: %s", len(found), root, found)
257-
if len(found) == 0 || (len(found) == 1 && found[0] == strings.Trim(root, "/")) {
258-
return fmt.Sprintf("$(GOLANGCI_LINT_BIN) run%s", suffix)
259-
}
260-
261-
return fmt.Sprintf(`find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLINT_CONFIG)"%s \;`, suffix)
257+
return fmt.Sprintf(`find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)"%s \;`, suffix)
262258
}
263259

264260
// shellLintCmd returns the appropriate shell lint command for a project.

0 commit comments

Comments
 (0)