Skip to content

stdenv: Errors in hooks (such as postUnpack) get ignored #7524

Closed

Description

@wizeman

It seems that errors in stdenv hooks are being ignored.

For example, if I add this to pkgs/tools/networking/socat/default.nix:

  postUnpack = '' 
    false
    true
  '';

Then when I run nix-build '<nixpkgs>' -A pkgs.socat, it will succeed even though it should fail, because a subcommand failed (the false subcommand).

I'm not completely sure about the following, but as far as I can see, the problem is that pkgs/stdenv/generic/setup.sh has this code:

 15 runHook() {                                                                                          
 16     local hookName="$1"                                                                              
 17     shift                                                                                            
 18     local var="$hookName"                                                                            
 19     if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi                                  
 20     eval "local -a dummy=(\"\${$var[@]}\")"                                                          
 21     for hook in "_callImplicitHook 0 $hookName" "${dummy[@]}"; do                                    
 22         if ! _eval "$hook" "$@"; then return 1; fi                                                   
 23     done                                                                                             
 24     return 0                                                                                         
 25 }                                                                                                    

In line 22, we are evaluating the hook in the context of an if condition.

According to this comment, POSIX specifies that set -e gets ignored if we're running inside an if condition. And it seems that there is nothing that we can do to get set -e to be obeyed when running inside that context, not even starting a subshell...

Activity

  1. added
    0.kind: bug
    on Apr 23, 2015
  2. vcunat commented on Apr 23, 2015

    @vcunat
    Member

    I noticed lately ignored errors in some phases. This seems the source of the problem, thanks for finding it!

    Suggestion: in this if-case we can just directly run the hook without any if, right? It seems only to propagate the error, but -e should do it for us I hope.

  3. vcunat commented on Apr 23, 2015

    @vcunat
    Member

    /cc author @edolstra. From a quick skim, runOneHook is also a problem. But it seems fixable – in any case, I think we can always first run the command separately and then run if on $?.

  4. self-assigned this
    on Apr 23, 2015
  5. seppeljordan commented on Apr 24, 2015

    @seppeljordan
    Contributor

    runOneHook seems to be a different story. It looks like this function is supposed to try out different hooks until it finds one that succeeds. So it would make more sense to leave the call for the hook inside of the if statement but pass a -e option to the subshell the executes the hook.

    # Run all hooks with the specified name, until one succeeds (returns a                                                                                 
    # zero exit code). If none succeed, return a non-zero exit code.                                                                                       
    runOneHook() {
      ...
    }
    
  6. wizeman commented on Apr 24, 2015

    @wizeman
    MemberAuthor

    So it would make more sense to leave the call for the hook inside of the if statement but pass a -e option to the subshell the executes the hook.

    But based on the comment I linked above, I thought that if we're inside the if statement, then the -e option will not have any effect, even if we run it inside a subshell?

  7. seppeljordan commented on Apr 24, 2015

    @seppeljordan
    Contributor

    Yes you are right, but I meant some construct like this:

        for hook in "_callImplicitHook 1 $hookName" "${dummy[@]}"; do
            set +e
            (set -e; _eval "$hook")
            RES=$?
            set -e
            return $RES
        done
    

    Then we still have the possibility to try out different hooks until one succeeds but can catch errors inside of the hooks.

  8. edolstra commented on May 12, 2015

    @edolstra
    Member

    runHook is easy to fix, I'll push a fix soon.

    However, the runOneHook case is basically unfixable. We can't run the hook in an if because that will disable -e, and we can't execute it outside of an if because that will cause the shell to exit. Executing in a subshell is not really an option because the assumption has always been that hooks run in the same shell (e.g. variables set by a hook should be visible outside). Though maybe we should just specify that certain hooks are executed in subshells.

    Bash is a terrible, terrible programming language.

  9. wkennington commented on May 12, 2015

    @wkennington
    Contributor

    Maybe it's time for a better programming language in the stdenv? There is
    always awk which we have :).

    On Tue, May 12, 2015, 09:40 Eelco Dolstra [email protected] wrote:

    runHook is easy to fix, I'll push a fix soon.

    However, the runOneHook case is basically unfixable. We can't run the
    hook in an if because that will disable -e, and we can't execute it
    outside of an if because that will cause the shell to exit. Executing in
    a subshell is not really an option because the assumption has always been
    that hooks run in the same shell (e.g. variables set by a hook should be
    visible outside). Though maybe we should just specify that certain hooks
    are executed in subshells.

    Bash is a terrible, terrible programming language.

    —
    Reply to this email directly or view it on GitHub
    #7524 (comment).

  10. 9 remaining items

  11. added a commit that references this issue on Jun 27, 2015
  12. added 2 commits that reference this issue on Jul 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

0.kind: bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions