Skip to content

[BUG] trap hook terminated #6147

@diachedelic

Description

@diachedelic

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

Scripts run via npm run have their trap hooks terminated prematurely. This is undesirable because the script does not get a chance to clean up after itself. Worse, it might clean up only some of its mess.

Expected Behavior

Any Bash "trap" hooks to run to completion.

Steps To Reproduce

Create a file demo.sh containing

#!/bin/bash
trap "echo start; sleep 1; echo end" EXIT
cat

and make it executable with chmod +x demo.sh. Notice that it has a trap hook that runs on exit. The hook echoes "start", does some work, then echoes "end". Declare it as the script "demo" in a package.json file:

{
    "scripts":{
        "demo":"./demo.sh"
    }
}

Run the "demo" script, then interrupt it by typing ctrl-c. Notice that the trap hook does not run to completion:

$ npm run demo

> demo
> ./demo.sh

^Cstart

On the other hand, running the script directly runs the trap hook to completion:

$ ./demo.sh
^Cstart
end

Environment

  • npm: 9.4.0
  • Node.js: v19.6.0
  • OS Name: MacOS 13.1
  • System Model Name: MacBook Pro
  • npm config:
; "user" config from /Users/me/.npmrc

//registry.npmjs.org/:_authToken = (protected) 

; node bin location = /nix/store/fnhfi19p4wlj38wjrxipnb355jxf3zzn-nodejs-19.6.0/bin/node
; node version = v19.6.0
; npm local prefix = /private/tmp
; npm version = 9.4.0
; cwd = /private/tmp
; HOME = /Users/me
; Run `npm config ls -l` to show all defaults.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 9.xwork is associated with a specific npm 9 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions