Skip to content

chore: add lint for codersdk dependencies #14638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ lint/ts:

lint/go:
./scripts/check_enterprise_imports.sh
./scripts/check_codersdk_imports.sh
linter_ver=$(shell egrep -o 'GOLANGCI_LINT_VERSION=\S+' dogfood/contents/Dockerfile | cut -d '=' -f 2)
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v$$linter_ver run
.PHONY: lint/go
Expand Down
20 changes: 20 additions & 0 deletions scripts/check_codersdk_imports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

# This file checks all codersdk imports to be sure it doesn't import any packages
# that are being replaced in go.mod.

set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
cdroot

deps=$(./scripts/list_dependencies.sh github.com/coder/coder/v2/codersdk)

set +e
replaces=$(grep "^replace" go.mod | awk '{print $2}')
conflicts=$(echo "$deps" | grep -xF -f <(echo "$replaces"))

if [ -n "${conflicts}" ]; then
error "$(printf 'codersdk cannot import the following packages being replaced in go.mod:\n%s' "${conflicts}")"
fi
log "codersdk imports OK"
25 changes: 25 additions & 0 deletions scripts/list_dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# This script lists all dependencies of a given package, including dependencies
# of test files.

# Usage: list_dependencies <package>

set -euo pipefail

if [[ "$#" -ne 1 ]]; then
echo "Usage: $0 <package>"
exit 1
fi

package="$1"
all_deps=$(go list -f '{{join .Deps "\n"}}' "$package")
test_imports=$(go list -f '{{ join .TestImports " " }}' "$package")
xtest_imports=$(go list -f '{{ join .XTestImports " " }}' "$package")

for pkg in $test_imports $xtest_imports; do
deps=$(go list -f '{{join .Deps "\n"}}' "$pkg")
all_deps+=$'\n'"$deps"
done

echo "$all_deps" | sort | uniq
Loading