Skip to content
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

buildGoModule: find build/test targets with go list #284568

Draft
wants to merge 9 commits into
base: staging
Choose a base branch
from
Next Next commit
buildGoModule: find build/test targets with go list
  • Loading branch information
katexochen committed Feb 11, 2024
commit c53e728ffe171c7f438fcf3cedd60653508fded0
49 changes: 34 additions & 15 deletions pkgs/build-support/go/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ let
fi
exclude+='\)'

buildGoDir() {
local cmd="$1" dir="$2"
buildGoDirs() {
local cmd="$1"
shift
local dirs=("$@")

. $TMPDIR/buildFlagsArray

Expand All @@ -214,7 +216,7 @@ let
fi

local OUT
if ! OUT="$(go $cmd "''${flags[@]}" $dir 2>&1)"; then
if ! OUT="$(go $cmd "''${flags[@]}" "''${dirs[@]}" 2>&1)"; then
if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then
echo "$OUT" >&2
return 1
Expand All @@ -226,13 +228,19 @@ let
return 0
}

getGoDirs() {
local type;
type="$1"
getPackagesToBuild() {
if [ -n "$subPackages" ]; then
echo "$subPackages" | sed "s,\(^\| \),\1./,g"
local subpkgs
subpkgs=$(echo "$subPackages" | sed "s,\(^\| \),\1./,g")
go list \
-f '{{ if ne .Module nil }}{{ if .Module.Main }}{{ .Dir }}{{ end }}{{ end }}' \
-deps "$subpkgs" | \
xargs echo
else
find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude"
# Make Go recurse all packages of the module.
go list \
-f '{{ .Dir }}' ./... | \
xargs echo
fi
}

Expand All @@ -248,10 +256,7 @@ let
if [ -z "$enableParallelBuilding" ]; then
export NIX_BUILD_CORES=1
fi
for pkg in $(getGoDirs ""); do
echo "Building subPackage $pkg"
buildGoDir install "$pkg"
done
buildGoDirs install $(getPackagesToBuild)"
'' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
# normalize cross-compiled builds w.r.t. native builds
(
Expand All @@ -273,9 +278,23 @@ let
# We do not set trimpath for tests, in case they reference test assets
export GOFLAGS=''${GOFLAGS//-trimpath/}

for pkg in $(getGoDirs test); do
buildGoDir test "$pkg"
done
getPackagesToTest() {
if [ -n "$subPackages" ]; then
# Find all packages belonging to this Go module that are dependencies
# of the targeted subPackages.
go list \
-f '{{ if ne .Module nil }}{{ if .Module.Main }}{{ .Dir }}{{ end }}{{ end }}' \
-deps "$subPackages" | \
xargs echo
else
# Make Go recurse all packages of the module.
go list \
-f '{{ .Dir }}' ./... | \
xargs echo
fi
}

buildGoDirs test $(getPackagesToTest)

runHook postCheck
'';
Expand Down