Permalink
Browse files

Handle circular dep installs properly

Check that the current directory is require-able: the parent is
node_modules and the package.json's name matches the directory name.

Fix #4312
  • Loading branch information...
1 parent adcbe18 commit 5dc93e8c82604c45b6067b1acf1c768e0bfce754 @substack substack committed with isaacs Dec 23, 2013
Showing with 58 additions and 1 deletion.
  1. +2 −1 lib/install.js
  2. +49 −0 test/tap/circular-dep.js
  3. +7 −0 test/tap/circular-dep/minimist/package.json
View
@@ -175,7 +175,8 @@ function install (args, cb_) {
, parent: data
, root: true
, wrap: null }
- if (data) {
+ if (data && data.name === path.basename(where) &&
+ path.basename(path.dirname(where)) === "node_modules") {
context.family[data.name] = context.ancestors[data.name] = data.version
}
var fn = npm.config.get("global") ? installMany : installManyTop
@@ -0,0 +1,49 @@
+var test = require("tap").test
+ , fs = require("fs")
+ , path = require("path")
+ , existsSync = fs.existsSync || path.existsSync
+ , npm = require("../../")
+ , rimraf = require("rimraf")
+ , mr = require("npm-registry-mock")
+ , common = require("../common-tap.js")
+ , server
+
+test("installing a package that depends on the current package", function (t) {
+ t.plan(1)
+
+ setup(function () {
+ npm.install("optimist", function (err) {
+ if (err) return t.fail(err)
+ npm.dedupe(function(err) {
+ if (err) return t.fail(err)
+ t.ok(existsSync(path.join(__dirname,
+ "circular-dep", "minimist", "node_modules", "optimist",
+ "node_modules", "minimist"
+ )))
+ cleanup()
+ server.close()
+ })
+ })
+ })
+})
+
+function setup (cb) {
+ process.chdir(path.join(__dirname, "circular-dep", "minimist"))
+ cleanup()
+ fs.mkdirSync(path.join(__dirname,
+ "circular-dep", "minimist", "node_modules"))
+ mr(common.port, function (s) {
+ server = s
+ npm.load({
+ registry: common.registry,
+ cache: path.resolve(__dirname, "circular-dep", "cache")
+ }, cb)
+ })
+}
+
+function cleanup() {
+ rimraf.sync(path.join(__dirname,
+ "circular-dep", "minimist", "node_modules"))
+ rimraf.sync(path.join(__dirname,
+ "circular-dep", "cache"))
+}
@@ -0,0 +1,7 @@
+{
+ "name": "minimist",
+ "version": "0.0.5",
+ "dependencies": {
+ "optimist": "0.6.0"
+ }
+}

0 comments on commit 5dc93e8

Please sign in to comment.