diff --git a/Makefile b/Makefile index 97fc34cb81786..4da623d54be0b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/scripts/check_codersdk_imports.sh b/scripts/check_codersdk_imports.sh new file mode 100755 index 0000000000000..5f5a3b6ea5546 --- /dev/null +++ b/scripts/check_codersdk_imports.sh @@ -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" diff --git a/scripts/list_dependencies.sh b/scripts/list_dependencies.sh new file mode 100755 index 0000000000000..47a6313b53a4e --- /dev/null +++ b/scripts/list_dependencies.sh @@ -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 + +set -euo pipefail + +if [[ "$#" -ne 1 ]]; then + echo "Usage: $0 " + 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