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

stdenv-setup: use set -u #28057

Merged
merged 1 commit into from Aug 25, 2017
Merged

Conversation

Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Aug 9, 2017

Motivation for this change

For my cross work, I need to make some fairly in-depth changes to stdenv's setup. This, and other development, is var easier with set -u as a debugging aid.

Care is taken to set +u when running hooks so as to not break existing packages. Eventually, we might try to use set -u everywhere but that is a far larger project.

Things done

Please check what applies. Note that these are not hard requirements but merely serve as information for reviewers.


CC @orivej

@Ericson2314 Ericson2314 added 2.status: work-in-progress 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 9.needs: community feedback labels Aug 9, 2017
@mention-bot
Copy link

@Ericson2314, thanks for your PR! By analyzing the history of the files in this pull request, we identified @edolstra, @dezgeg and @wkennington to be potential reviewers.

@Ericson2314 Ericson2314 removed the 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on label Aug 9, 2017
@@ -1,4 +1,4 @@
set -e
set -eux
Copy link
Member Author

Choose a reason for hiding this comment

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

Just did set -x while debugging.

local hookName="$1"
shift
local var="$hookName"
if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi
local -a "var=(\${$var[@]})"
Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this abomination actually works.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you can use

hooksRef="${hookName%Hook}Hooks[@]"
hooks=("${!hooksRef}")

Copy link
Member Author

Choose a reason for hiding this comment

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

I did the trailing [@] trick before, but set -u doesn't like it. Bash is such a mess.

%Hookis cool though; I'll definitely use that!

Copy link
Contributor

Choose a reason for hiding this comment

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

It is surprising that bash is happy with option 1:

set -eu
hooksVar=abcHooks
hooksRef="${hooksVar}[@]"
p() {
    # option 1
    local -a hooks="(\"\${$hooksVar[@]}\")"
    # option 2
    hooks=("${!hooksRef+${!hooksRef}}")

    echo "${#hooks[@]} item(s):"
    for item in "${hooks[@]}"; do
        printf '  %q\n' "$item"
    done
}
# not empty
abcHooks=('a b' 'c d')
p
# empty item
abcHooks=('')
p
# empty array
abcHooks=()
p
# undefined
unset abcHooks
p

_eval "$hook" "$@"
done

eval "$oldOpts"
Copy link
Member Author

Choose a reason for hiding this comment

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

Want to be robust no matter who calls

@Ericson2314 Ericson2314 changed the title stdenv-setup: WIP use set -u [WIP] stdenv-setup: use set -u Aug 9, 2017
@Ericson2314 Ericson2314 force-pushed the stdenv-set-u branch 5 times, most recently from 52566b7 to ab628e7 Compare August 9, 2017 19:18
@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 9, 2017

Ok after much wrestling, stdenv seems to be building sucessfully now on macOS and Linux.

@@ -987,3 +1041,6 @@ runHook userHook


dumpVars

# Disable nounset for nix-shell.
set +u
Copy link
Member Author

Choose a reason for hiding this comment

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

We should probably make nix-shell do this, like it does for set -e.

@orivej
Copy link
Contributor

orivej commented Aug 9, 2017

Sorry if this observation is not well informed, but why are set +u and set -u so distant from each other? Couldn't they just wrap pieces of code that need them, such that each set +u is obviously balanced with the next set -u?

@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 9, 2017

@orivej I try to do that. The worst offender is because eval "$foo" can early return, so I have to set -u in the caller to fake the RIAA/try..finally.

@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 9, 2017

@orivej BTW, I'd love to get rid of the redundancy between _eval and _callImplicitHook, or even better get rid of the latter altogether, but I couldn't come up with a way after much thinking about it in the morning.

@Ericson2314 Ericson2314 force-pushed the stdenv-set-u branch 3 times, most recently from ab563c4 to 9fc7726 Compare August 9, 2017 22:32
@Ericson2314 Ericson2314 changed the title [WIP] stdenv-setup: use set -u stdenv-setup: use set -u Aug 10, 2017
@Ericson2314
Copy link
Member Author

@globin Good to go now

globin added a commit to mayflower/hydra-jobs that referenced this pull request Aug 10, 2017
@dezgeg
Copy link
Contributor

dezgeg commented Aug 11, 2017

Not a fan. All this set +/-u flipping around looks really brittle, plus the constant :-} is getting to line noise territory...

@globin
Copy link
Member

globin commented Aug 11, 2017

@Ericson2314 Ericson2314 added this to Needed by the big PR in Cross compilation Aug 16, 2017
@Ericson2314 Ericson2314 modified the milestone: 17.09 Aug 16, 2017
Older bash version, like those in the bootstrap tools and on macOS,
currently confuse variables defined as an empty array with undefined
variables. `${foo+"${foo[@]}"}` will prevent `set -u` problems with
empty arrays and older without making a single '' in the empty case.

Care is taken to `set +u` when running hooks so as to not break existing
packages.
@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 23, 2017

Rebased an rebuilt. I'm trying to clean up cc-wrapper's default.nix (as opposed to the wrappers themselves, which I already did), and this impedes that too---set -u is essential to making sure that as I move things around all variables are still defined before use.

@Ericson2314 Ericson2314 moved this from Needed by the big PR---nice to move pick off pieces of it and move here, rebasing the big PR on top to Needed for binutils-wrapper in Cross compilation Aug 25, 2017
@Ericson2314 Ericson2314 mentioned this pull request Aug 25, 2017
8 tasks
@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 25, 2017

See https://github.com/NixOS/nixpkgs/projects/8 for all my PRs that (directly or indirectly) depend on this. To unblock those, I hope we can merge this, and merge it soon.

@Ericson2314
Copy link
Member Author

Ericson2314 commented Aug 25, 2017

OK I'm merging this now. I just need this to have a reasonable debugging environment to finish https://github.com/NixOS/nixpkgs/projects/8. After that project, if anyone insists this is change bad, I will personally revert. (It won't commute with other changes, so it's a somewhat less trivial revert I'm promising).

@Ericson2314
Copy link
Member Author

N.B. Checked each failure in https://hydra.mayflower.de/jobset/mayflower/hydra-jobs-production and confirmed it was unrelated.

@Ericson2314 Ericson2314 merged commit 2e7a390 into NixOS:staging Aug 25, 2017
@Ericson2314 Ericson2314 deleted the stdenv-set-u branch August 25, 2017 15:20
@dezgeg
Copy link
Contributor

dezgeg commented Aug 26, 2017

Like, WTF is this new apparent "policy" that feedback from others in multiple pull requests can just be ignored just on the basis on "I am in a hurry" or "I need this"? We are using git after all....

orivej added a commit to orivej/nixpkgs that referenced this pull request Sep 3, 2017
Environment variable filter in substituteAll was not precise and produced
undefined and invalid variable names.  Vladimír Čunát tried to fix that in [1],
but `env -0` did not work during Darwin bootstrap, so [2] reverted this change
and replaced an error due to invalid variables with a warning.  Recently in NixOS#28057
John Ericson added `set -u` to `setup.sh` and undefined variables made the setup
fail during e.g. `nix-build -A gnat` with `setup: line 519: !varName: unbound
variable`.

[1] NixOS@62fc885
[2] NixOS@81df035
Ericson2314 pushed a commit that referenced this pull request Sep 3, 2017
Environment variable filter in substituteAll was not precise and produced
undefined and invalid variable names.  Vladimír Čunát tried to fix that in [1],
but `env -0` did not work during Darwin bootstrap, so [2] reverted this change
and replaced an error due to invalid variables with a warning.  Recently in #28057
John Ericson added `set -u` to `setup.sh` and undefined variables made the setup
fail during e.g. `nix-build -A gnat` with `setup: line 519: !varName: unbound
variable`.

[1] 62fc885
[2] 81df035

(cherry picked from commit a09d9e7)
@Ericson2314
Copy link
Member Author

I am no longer trying to squeeze my cross work into 17.03, so there is no rush. I will continue to need this for cross work on master for 18.03, but I did merge it over other's objections. If someone wants to revert it until there is more consensus, I cannot complain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Cross compilation
Needed for binutils-wrapper
Development

Successfully merging this pull request may close these issues.

None yet

6 participants