A module ID with a trailing slash must be a dir.
authorisaacs <i@izs.me>
Thu, 27 Jan 2011 21:14:47 +0000 (13:14 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 27 Jan 2011 22:02:43 +0000 (14:02 -0800)
require('./foo/') should not try to load './foo.js'.  It should only
look for ./foo/index.js

Closes GH-588

lib/module.js
test/fixtures/nested-index/three.js [new file with mode: 0644]
test/fixtures/nested-index/three/index.js [new file with mode: 0644]
test/simple/test-module-loading.js

index fbb494a..698483f 100644 (file)
@@ -58,6 +58,8 @@ Module._findPath = function(request, paths) {
     paths = [''];
   }
 
+  var trailingSlash = (request.slice(-1) === '/');
+
   // check if the file exists and is not a directory
   function tryFile(requestPath) {
     try {
@@ -89,13 +91,16 @@ Module._findPath = function(request, paths) {
   // For each path
   for (var i = 0, PL = paths.length; i < PL; i++) {
     var basePath = path.resolve(paths[i], request);
+    var filename;
 
-    // try to join the request to the path
-    var filename = tryFile(basePath);
+    if (!trailingSlash) {
+      // try to join the request to the path
+      filename = tryFile(basePath);
 
-    if (!filename) {
-      // try it with each of the extensions
-      filename = tryExtensions(basePath);
+      if (!filename && !trailingSlash) {
+        // try it with each of the extensions
+        filename = tryExtensions(basePath);
+      }
     }
 
     if (!filename) {
diff --git a/test/fixtures/nested-index/three.js b/test/fixtures/nested-index/three.js
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/fixtures/nested-index/three/index.js b/test/fixtures/nested-index/three/index.js
new file mode 100644 (file)
index 0000000..e69de29
index a9aaf3a..6c09285 100644 (file)
@@ -52,6 +52,13 @@ var one = require('../fixtures/nested-index/one'),
     two = require('../fixtures/nested-index/two');
 assert.notEqual(one.hello, two.hello);
 
+common.debug('test index.js in a folder with a trailing slash');
+var three = require('../fixtures/nested-index/three'),
+    threeFolder = require('../fixtures/nested-index/three/'),
+    threeIndex = require('../fixtures/nested-index/three/index.js');
+assert.equal(threeFolder, threeIndex);
+assert.notEqual(threeFolder, three);
+
 common.debug('test cycles containing a .. path');
 var root = require('../fixtures/cycles/root'),
     foo = require('../fixtures/cycles/folder/foo');