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
Prev Previous commit
Next Next commit
buildGoModule: also set flags on go list
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Feb 11, 2024
commit ef1d463af670b35e164707802db29ca285da0614
49 changes: 35 additions & 14 deletions pkgs/build-support/go/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,30 @@ let
echo "''${filteredPkgs[@]}"
}

collectFlags() {
. $TMPDIR/buildFlagsArray
declare -a flags
flags+=($buildFlags "''${buildFlagsArray[@]}")
flags+=("''${tags:+-tags=''${tags// /,}}")
flags+=("''${ldflags:+-ldflags="$ldflags"}")
flags+=("-p=$NIX_BUILD_CORES")
for flag in "''${flags[@]}"; do
echo "$flag" >&2
echo "$flag"
done
}

buildGoDirs() {
local cmd="$1"
shift
local pkgs=("$@")

read -ra pkgs < <(filterExcluded "''${pkgs[@]}")

. $TMPDIR/buildFlagsArray

declare -a flags
flags+=($buildFlags "''${buildFlagsArray[@]}")
flags+=(''${tags:+-tags=''${tags// /,}})
flags+=(''${ldflags:+-ldflags="$ldflags"})
flags+=("-p" "$NIX_BUILD_CORES")
read -ra flags < <(collectFlags)
for flag in "''${flags[@]}"; do
echo "$flag" >&2
done

if [ "$cmd" = "test" ]; then
flags+=(-vet=off)
Expand All @@ -239,17 +249,21 @@ let
}

getPackagesToBuild() {
read -ra flags < <(collectFlags)
if [[ -n "$subPackages" ]]; then
local subPkgs
read -ra subPkgs < <(echo "$subPackages" | sed "s,\(^\| \),\1./,g")
go list \
-f '{{ .Dir }}' \
''${subPkgs[@]} | \
"''${flags[@]}" \
"''${subPkgs[@]}" | \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fallback this to ./... to avoid the duplication?

xargs echo
else
# Make Go recurse all packages of the module.
go list \
-f '{{ .Dir }}' ./... | \
-f '{{ .Dir }}' \
"''${flags[@]}" \
./... | \
xargs echo
fi
}
Expand Down Expand Up @@ -290,19 +304,24 @@ let
export GOFLAGS=''${GOFLAGS//-trimpath/}

getPackagesToTest() {
read -ra flags < <(collectFlags)
if [[ -n "$subPackages" ]]; then
# Find all packages belonging to this Go module that are dependencies
# of the targeted subPackages.
local subPkgs
read -ra subPkgs < <(echo "$subPackages" | sed "s,\(^\| \),\1./,g")
go list \
-f '{{ if ne .Module nil }}{{ if .Module.Main }}{{ .Dir }}{{ end }}{{ end }}' \
-deps ''${subPkgs[@]} | \
-deps \
"''${flags[@]}" \
"''${subPkgs[@]}" | \
xargs echo
else
# Make Go recurse all packages of the module.
go list \
-f '{{ .Dir }}' ./... | \
-f '{{ .Dir }}' \
"''${flags[@]}" \
./... | \
xargs echo
fi
}
Expand All @@ -328,7 +347,7 @@ let
disallowedReferences = lib.optional (!allowGoReference) go;

passthru = passthru // { inherit go goModules vendorHash; }
// lib.optionalAttrs (args' ? vendorSha256 ) { inherit (args') vendorSha256; };
// lib.optionalAttrs (args' ? vendorSha256) { inherit (args') vendorSha256; };

meta = {
# Add default meta information
Expand All @@ -337,7 +356,9 @@ let
});
in
lib.warnIf (args' ? vendorSha256) "`vendorSha256` is deprecated. Use `vendorHash` instead"
lib.warnIf (buildFlags != "" || buildFlagsArray != "")
lib.warnIf
(buildFlags != "" || buildFlagsArray != "")
"Use the `ldflags` and/or `tags` attributes instead of `buildFlags`/`buildFlagsArray`"
lib.warnIf (builtins.elem "-buildid=" ldflags) "`-buildid=` is set by default as ldflag by buildGoModule"
lib.warnIf
(builtins.elem "-buildid=" ldflags) "`-buildid=` is set by default as ldflag by buildGoModule"
package
Comment on lines -307 to 364
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo the formatting, to have less noise