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

[BUG] npx/npm exec allows prompt to overwrite output #7821

Closed
2 tasks done
bbrk24 opened this issue Oct 6, 2024 · 1 comment
Closed
2 tasks done

[BUG] npx/npm exec allows prompt to overwrite output #7821

bbrk24 opened this issue Oct 6, 2024 · 1 comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps

Comments

@bbrk24
Copy link

bbrk24 commented Oct 6, 2024

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

When running a script that writes to stdout using fs.write or its relatives via npx or npm exec, the terminal prompt overwrites any output after the last newline (or all output, if no newline was written). See DanielXMoore/Civet#1424 for the discussion and troubleshooting that led to us realizing this is npm's fault.

Expected Behavior

The terminal prompt should be written after the text, not over top of it

Steps To Reproduce

  1. Put this script in a .mjs file:
    import { writeSync } from 'node:fs'
    const buf = Buffer.alloc(1, 0x61)
    writeSync(1, buf, 0, 1)
  2. Use npx or npm exec to cause this script to be executed
  3. Notice that the prompt is written over top of the "a", causing the output to be deleted.

Environment

  • npm: 10.9.0
  • Node.js: 20.18.0
  • OS Name: Ubuntu 22.04 running in WSL 2 on Windows 11
  • npm config:
; "user" config from /home/bbrk24/.npmrc

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

; node bin location = /home/bbrk24/.nvm/versions/node/v20.18.0/bin/node
; node version = v20.18.0
; npm local prefix = /home/bbrk24/workspace/template-qdeql
; npm version = 10.9.0
; cwd = /home/bbrk24/workspace/template-qdeql
; HOME = /home/bbrk24
; Run `npm config ls -l` to show all defaults.
@bbrk24 bbrk24 added Bug thing that needs fixing Needs Triage needs review for next steps labels Oct 6, 2024
@reggi
Copy link
Contributor

reggi commented Oct 7, 2024

Given the following project:

#!/usr/bin/env node
import { writeSync } from 'node:fs'
const buf = Buffer.alloc(1, 0x61)
writeSync(1, buf, 0, 1)
{
    "name": "example-7821",
    "bin": {
        "example-7821": "index.mjs"
    },
    "scripts": {
        "start": "node index.mjs"
    }
}

A workaround is to use -s to silent npm's output.

  7821 node index.mjs 
a%
  7821 npm -s start
a%
  7821 npm i . -sg  && npx -s example-7821
a%
  7821 npm -s exec . -- start
a%

It's not safe to assume npm won't log or disrupt the expectations of stdout and stderr for any given script. If you need finely tuned output -s is the best bet at ensuring that your script will write to stdout / stderr correctly. Ideally you wouldn't need to add -s in some of these cases, but given that people can have custom log levels, you'd need to use -s to overwrite individual configs anyway.

@reggi reggi closed this as completed Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps
Projects
None yet
Development

No branches or pull requests

2 participants