Skip to content

Commit

Permalink
include command in spawn errors (fixes npm#3908)
Browse files Browse the repository at this point in the history
factor to centralize
wrap child, make it aware of filename
  • Loading branch information
smikes authored and othiym23 committed Nov 7, 2014
1 parent 0aefae9 commit e007a2c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ explore.usage = "npm explore <pkg> [ -- <cmd>]"
explore.completion = require("./utils/completion/installed-shallow.js")

var npm = require("./npm.js")
, spawn = require("child_process").spawn
, spawn = require("./utils/spawn")
, path = require("path")
, fs = require("graceful-fs")

Expand Down
2 changes: 1 addition & 1 deletion lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ help.completion = function (opts, cb) {
}

var path = require("path")
, spawn = require("child_process").spawn
, spawn = require("./utils/spawn")
, npm = require("./npm.js")
, log = require("npmlog")
, opener = require("opener")
Expand Down
10 changes: 9 additions & 1 deletion lib/utils/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function errorHandler (er) {

case "ELIFECYCLE":
log.error("", er.message)
log.error("", ["","Failed at the "+er.pkgid+" "+er.stage+" script."
log.error("", ["","Failed at the "+er.pkgid+" "+er.stage+" script '"+er.script+"'."
,"This is most likely a problem with the "+er.pkgname+" package,"
,"not with npm itself."
,"Tell the author that this fails on your system:"
Expand Down Expand Up @@ -339,6 +339,14 @@ function errorHandler (er) {
].join("\n"))
break

case "ENOENT":
log.error("enoent", [er.message
,"This is most likely not a problem with npm itself"
,"and is related to npm not being able to find a file."
,er.file?"\nCheck if the file '"+er.file+"' is present.":""
].join("\n"))
break

default:
log.error("", er.message || er)
log.error("", ["", "If you need help, you may report this error at:"
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.chainableExec = chainableExec
exports.whichAndExec = whichAndExec

var exec = require("child_process").execFile
, spawn = require("child_process").spawn
, spawn = require("./spawn")
, npm = require("../npm.js")
, which = require("which")
, git = npm.config.get("git")
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exports.cmd = cmd
exports.makeEnv = makeEnv

var log = require("npmlog")
, spawn = require("child_process").spawn
, spawn = require("./spawn")
, npm = require("../npm.js")
, path = require("path")
, fs = require("graceful-fs")
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = spawn

var _spawn = require("child_process").spawn,
EventEmitter = require("events").EventEmitter

function spawn (cmd, args, options) {
var raw = _spawn(cmd, args, options)
cooked = new EventEmitter()

raw.on("error", function (er) {
er.file = cmd
cooked.emit("error", er)
}).on("close", function (code, signal) {
cooked.emit("close", code, signal)
})

cooked.stdin = raw.stdin
cooked.stdout = raw.stdout
cooked.stderr = raw.stderr

return cooked
}
33 changes: 33 additions & 0 deletions test/tap/spawn-enoent-help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var path = require("path")
var test = require("tap").test
var rimraf = require("rimraf")
var mkdirp = require("mkdirp")
var common = require("../common-tap.js")

var pkg = path.resolve(__dirname, "spawn-enoent-help")

test("setup", function (t) {
rimraf.sync(pkg)
mkdirp.sync(pkg)
t.end()
})

test("enoent help", function (t) {
common.npm(["help", "config"], {
cwd: pkg,
env: {
PATH: "",
Path: "",
"npm_config_loglevel": "warn",
"npm_config_viewer": "woman"
}
}, function (er, code, sout, serr) {
t.similar(serr, /Check if the file 'emacsclient' is present./)
t.end()
})
})

test("clean", function (t) {
rimraf.sync(pkg)
t.end()
})
2 changes: 1 addition & 1 deletion test/tap/spawn-enoent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test("enoent script", function (t) {
"npm_config_loglevel": "warn"
}
}, function (er, code, sout, serr) {
t.similar(serr, /npm ERR! Failed at the x@1\.2\.3 start script\./)
t.similar(serr, /npm ERR! Failed at the x@1\.2\.3 start script 'wharble-garble-blorst'\./)
t.end()
})
})
Expand Down

0 comments on commit e007a2c

Please sign in to comment.