Permalink
Browse files

Don't try to chdir when it'll fail

  • Loading branch information...
1 parent d7dcace commit bd5629559d50317bf7acca36e4857999c5fdcacd @isaacs isaacs committed Feb 25, 2011
Showing with 12 additions and 1 deletion.
  1. +12 −1 lib/utils/lifecycle.js
@@ -18,7 +18,12 @@ function lifecycle (pkg, stage, wd, cb) {
if (!pkg) return cb(new Error("Invalid package data"))
log(pkg._id, stage)
- wd = validWd(wd || path.join(npm.dir, pkg.name, pkg.version, "package"))
+ var pkgdir = path.join(npm.dir, pkg.name, pkg.version, "package")
+ wd = validWd(wd || pkgdir)
+ if (wd !== pkgdir && !npm.config.get("unsafe-perm")) {
+ log.warn(pkg._id, "Skipping "+stage+" script, since it'll fail.")
+ return cb()
+ }
// set the env variables, then run scripts as a child process.
var env = makeEnv(pkg)
@@ -146,11 +151,14 @@ function runPackageLifecycle (pkg, env, wd, cb) {
, user = pkg._link || up ? null : npm.config.get("user")
, group = pkg._link || up ? null : npm.config.get("group")
, cmd = env.npm_lifecycle_script
+ , cwd = process.cwd()
log.verbose(up, "unsafe-perm in lifecycle")
+ process.chdir(wd)
exec( "sh", ["-c", cmd], env, true, wd
, user, group
, function (er, code, stdout, stderr) {
+ process.chdir(cwd)
if (er && !npm.ROLLBACK) {
log("Failed to exec "+stage+" script", pkg._id)
er.message = pkg._id + " "
@@ -185,9 +193,12 @@ function runHookLifecycle (pkg, env, wd, cb) {
fs.stat(hook, function (er) {
if (er) return cb()
+ var cwd = process.cwd()
+ process.chdir(wd)
exec( "sh", ["-c", cmd], env, true, wd
, user, group
, function (er) {
+ process.chdir(cwd)
if (er) {
er.message += "\nFailed to exec "+stage+" hook script"
log(er, pkg._id)

0 comments on commit bd56295

Please sign in to comment.