microsoft / microsoft/vscode-sudo-prompt

throw new Error causes stdout to be undefined

Open
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Background

Usually in JavaScript if you log, then throw an Error both will be outputted sequentially:

console.log('Installing...');
throw new Error('Example error');

Outputs:

Installing...
Error: Example error

Problem

However when running the same code inside sudoPrompt.exec console.log messages are not outputted if there is an Error thrown.

Example:

import { fileURLToPath } from 'url';
import * as sudoPrompt from '@vscode/sudo-prompt';
import { execFileSync } from 'child_process';

const __filename = fileURLToPath(import.meta.url);

function isAdmin(){
  if (process.platform === 'win32') {
    try {
      execFileSync('net', ['session'], { stdio: 'ignore' });
      return true;
    } catch {
      return false;
    }
  } else {
    return process.getuid && process.getuid() === 0;
  }
}

function runAsAdmin() {
  const cmd = `node "${__filename}" --admin`;
  sudoPrompt.exec(cmd, { name: 'Standalone Installer' }, (error, stdout, stderr) => {
    console.log('callback', stdout, stderr);
    if (stdout) {
      console.log('[sudo-prompt stdout]', stdout);
    }
    if (stderr) {
      console.log('[sudo-prompt stderr]', stderr);
    }
    if (error) {
      console.error('[sudo-prompt error]', error);
    }
  });
}

function install() {
  console.log('Installing...');
  throw new Error('Example error');
}

function main() {
  if (isAdmin() || process.argv.includes('--admin')) {
    console.log('Running as admin...');
    install();
  } else {
    console.log('Not running as admin...');
    runAsAdmin();
  }
}

main();

Outputs:

Not running as admin...
callback undefined undefined
[sudo-prompt error] Error: Example error

This causes issues debugging and for any command line script/tool which needs to output progress/results and then fail with errors.

Expected result

Not running as admin...
callback Installing... undefined
[sudo-prompt stdout] Installing...
[sudo-prompt error] Error: Example error

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the sudoPrompt.exec callback and the shown install/main flow, focusing on how a thrown Error affects captured stdout and stderr. Reproduce the example and verify that the callback receives "Installing..." as stdout before the error, matching the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.