Skip to content

Commit

Permalink
fix: Do not print duplicate stdio after a streaming command errors (#…
Browse files Browse the repository at this point in the history
…1832)

Fixes #1790
  • Loading branch information
mnutt authored and evocateur committed Dec 27, 2018
1 parent ef18236 commit 2bcc366
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
27 changes: 27 additions & 0 deletions core/command/__tests__/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,33 @@ describe("core-command", () => {
});
}
});

it("does not log stdout/stderr after streaming ends", async () => {
class PkgErrorCommand extends Command {
initialize() {
return true;
}

execute() {
const err = new Error("message");

err.cmd = "test-pkg-err";
err.stdout = "pkg-err-stdout";
err.stderr = "pkg-err-stderr";
err.pkg = {
name: "pkg-err-name",
};

throw err;
}
}

try {
await new PkgErrorCommand({ cwd: testDir, stream: true });
} catch (err) {
expect(console.error.mock.calls).toHaveLength(0);
}
});
});

describe("loglevel", () => {
Expand Down
2 changes: 1 addition & 1 deletion core/command/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Command {
err => {
if (err.pkg) {
// Cleanly log specific package error details
logPackageError(err);
logPackageError(err, this.options.stream);
} else if (err.name !== "ValidationError") {
// npmlog does some funny stuff to the stack by default,
// so pass it directly to avoid duplication.
Expand Down
7 changes: 6 additions & 1 deletion core/command/lib/log-package-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const log = require("libnpm/log");

module.exports = logPackageError;

function logPackageError(err) {
function logPackageError(err, stream = false) {
log.error(err.cmd, `exited ${err.code} in '${err.pkg.name}'`);

if (stream) {
// Streaming has already printed all stdout/stderr
return;
}

if (err.stdout) {
log.error(err.cmd, "stdout:");
directLog(err.stdout);
Expand Down

0 comments on commit 2bcc366

Please sign in to comment.