From 8b5a7940b0cd1f6f232933099f12f383c3b3c32a Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Fri, 25 Feb 2022 17:06:28 -0500 Subject: [PATCH] go: Bunch of fixes when using excludedPackages and other bits Few things going on in this commit: Do not print "Building subPakage $pkg" message if actually going to skip the package. This was confusing to me when I was trying to figure out how to set excludedPackages and seeing the "Building subpackage $pkg" messages for packages I wanted to skip. Turns out this messages was being printed before checking if we actually wanted to build the package and not necessarily that my excludedPackages was wrong. Make go-packages look a little bit more like go-modules, by adding testdata to the default list of excluded packages. This commit also does some setup outside the buildGoDir function so that we avoid checking `excludedPackages` for every package and cut down the number of grep calls by half since we always want at least one grep for the default excludedPackages, might as well just add to the patterns being checked. Finally, adds documentation for usage of excludedPackages and subPackages. I had to read the implementation to figure out how to correctly use these function arguments since there was no documentation and different uses in the code base. So this commit documents usage of the arguments. --- doc/languages-frameworks/go.section.md | 6 +++++- pkgs/development/go-modules/generic/default.nix | 12 ++++++++++-- pkgs/development/go-packages/generic/default.nix | 13 +++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/doc/languages-frameworks/go.section.md b/doc/languages-frameworks/go.section.md index 411205d08e43..9c67a514335e 100644 --- a/doc/languages-frameworks/go.section.md +++ b/doc/languages-frameworks/go.section.md @@ -142,4 +142,8 @@ Removes the pre-existing vendor directory. This should only be used if the depen ### `subPackages` {#var-go-subPackages} -Limits the builder from building child packages that have not been listed. If `subPackages` is not specified, all child packages will be built. +Specified as a string or list of strings. Limits the builder from building child packages that have not been listed. If `subPackages` is not specified, all child packages will be built. + +### `excludedPackages` {#var-go-excludedPackages} + +Specified as a string or list of strings. Causes the builder to skip building child packages that match any of the provided values. If `excludedPackages` is not specified, all child packages will be built. diff --git a/pkgs/development/go-modules/generic/default.nix b/pkgs/development/go-modules/generic/default.nix index 76d0dc961c5a..ace8b820ee23 100644 --- a/pkgs/development/go-modules/generic/default.nix +++ b/pkgs/development/go-modules/generic/default.nix @@ -171,13 +171,20 @@ let buildPhase = args.buildPhase or '' runHook preBuild + exclude='\(/_\|examples\|Godeps\|testdata' + if [[ -n "$excludedPackages" ]]; then + IFS=' ' read -r -a excludedArr <<<$excludedPackages + printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}" + excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf + exclude+='\\|'"$excludedAlternates" + fi + exclude+='\)' + buildGoDir() { local d; local cmd; cmd="$1" d="$2" . $TMPDIR/buildFlagsArray - echo "$d" | grep -q "\(/_\|examples\|Godeps\|testdata\)" && return 0 - [ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0 local OUT if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${tags:+-tags=${lib.concatStringsSep "," tags}} ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then @@ -214,6 +221,7 @@ let export NIX_BUILD_CORES=1 fi for pkg in $(getGoDirs ""); do + grep -q "$exclude" <<<$pkg && continue echo "Building subPackage $pkg" buildGoDir install "$pkg" done diff --git a/pkgs/development/go-packages/generic/default.nix b/pkgs/development/go-packages/generic/default.nix index 7c4d173b937b..6e96f443ff9f 100644 --- a/pkgs/development/go-packages/generic/default.nix +++ b/pkgs/development/go-packages/generic/default.nix @@ -150,13 +150,20 @@ let runHook renameImports + exclude='\(/_\|examples\|Godeps\|testdata' + if [[ -n "$excludedPackages" ]]; then + IFS=' ' read -r -a excludedArr <<<$excludedPackages + printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}" + excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf + exclude+='\\|'"$excludedAlternates" + fi + exclude+='\)' + buildGoDir() { local d; local cmd; cmd="$1" d="$2" . $TMPDIR/buildFlagsArray - echo "$d" | grep -q "\(/_\|examples\|Godeps\)" && return 0 - [ -n "$excludedPackages" ] && echo "$d" | grep -q "$excludedPackages" && return 0 local OUT if ! OUT="$(go $cmd $buildFlags "''${buildFlagsArray[@]}" ''${tags:+-tags=${lib.concatStringsSep "," tags}} ''${ldflags:+-ldflags="$ldflags"} -v -p $NIX_BUILD_CORES $d 2>&1)"; then if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then @@ -195,6 +202,8 @@ let export NIX_BUILD_CORES=1 fi for pkg in $(getGoDirs ""); do + grep -q "$exclude" <<<$pkg && continue + echo "Building subPackage $pkg" buildGoDir install "$pkg" done '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''