@@ -103,20 +103,30 @@ function tryPackage(requestPath, exts, isMain) {
103
103
if ( ! pkg ) return false ;
104
104
105
105
var filename = path . resolve ( requestPath , pkg ) ;
106
+ return readPackagePath ( filename , exts , isMain ) ;
107
+ }
108
+
109
+ function readPackagePath ( filename , exts , isMain ) {
106
110
return tryFile ( filename , isMain ) ||
107
111
tryExtensions ( filename , exts , isMain ) ||
108
112
tryExtensions ( path . resolve ( filename , 'index' ) , exts , isMain ) ;
109
113
}
110
114
115
+ function readFilePath ( requestPath , isMain ) {
116
+ if ( isMain ) {
117
+ return fs . realpathSync ( requestPath ) ;
118
+ }
119
+ return path . resolve ( requestPath ) ;
120
+ }
121
+
111
122
// check if the file exists and is not a directory
112
123
// resolve to the absolute realpath if running main module,
113
124
// otherwise resolve to absolute while keeping symlinks intact.
114
125
function tryFile ( requestPath , isMain ) {
115
126
const rc = stat ( requestPath ) ;
116
- if ( isMain ) {
117
- return rc === 0 && fs . realpathSync ( requestPath ) ;
127
+ if ( rc === 0 ) {
128
+ return readFilePath ( requestPath , isMain ) ;
118
129
}
119
- return rc === 0 && path . resolve ( requestPath ) ;
120
130
}
121
131
122
132
// given a path check a the file exists with any of the set extensions
@@ -131,6 +141,34 @@ function tryExtensions(p, exts, isMain) {
131
141
return false ;
132
142
}
133
143
144
+ function tryFindPath ( basePath , exts , isMain , isAbsolute ) {
145
+ var filename ;
146
+
147
+ if ( isAbsolute ) {
148
+ const rc = stat ( basePath ) ;
149
+ if ( rc === 0 ) { // File.
150
+ filename = readFilePath ( basePath , isMain ) ;
151
+ } else if ( rc === 1 ) { // Directory.
152
+ filename = tryPackage ( basePath , exts , isMain ) ;
153
+ }
154
+
155
+ if ( ! filename ) {
156
+ filename = tryExtensions ( basePath , exts , isMain ) ;
157
+ }
158
+ }
159
+
160
+ if ( ! filename ) {
161
+ filename = tryPackage ( basePath , exts , isMain ) ;
162
+ }
163
+
164
+ if ( ! filename ) {
165
+ // try it with each of the extensions at "index"
166
+ filename = tryExtensions ( path . resolve ( basePath , 'index' ) , exts , isMain ) ;
167
+ }
168
+
169
+ return filename ;
170
+ }
171
+
134
172
var warned = false ;
135
173
Module . _findPath = function ( request , paths , isMain ) {
136
174
if ( path . isAbsolute ( request ) ) {
@@ -144,52 +182,24 @@ Module._findPath = function(request, paths, isMain) {
144
182
return Module . _pathCache [ cacheKey ] ;
145
183
}
146
184
147
- var exts ;
185
+ const exts = Object . keys ( Module . _extensions ) ;
148
186
const trailingSlash = request . length > 0 &&
149
187
request . charCodeAt ( request . length - 1 ) === 47 /*/*/ ;
188
+ const isAbsolute = ! trailingSlash ;
150
189
151
190
// For each path
152
191
for ( var i = 0 ; i < paths . length ; i ++ ) {
153
192
// Don't search further if path doesn't exist
154
193
const curPath = paths [ i ] ;
155
194
if ( curPath && stat ( curPath ) < 1 ) continue ;
156
- var basePath = path . resolve ( curPath , request ) ;
157
- var filename ;
158
-
159
- if ( ! trailingSlash ) {
160
- const rc = stat ( basePath ) ;
161
- if ( rc === 0 ) { // File.
162
- if ( ! isMain ) {
163
- filename = path . resolve ( basePath ) ;
164
- } else {
165
- filename = fs . realpathSync ( basePath ) ;
166
- }
167
- } else if ( rc === 1 ) { // Directory.
168
- if ( exts === undefined )
169
- exts = Object . keys ( Module . _extensions ) ;
170
- filename = tryPackage ( basePath , exts , isMain ) ;
171
- }
172
195
173
- if ( ! filename ) {
174
- // try it with each of the extensions
175
- if ( exts === undefined )
176
- exts = Object . keys ( Module . _extensions ) ;
177
- filename = tryExtensions ( basePath , exts , isMain ) ;
178
- }
179
- }
196
+ // If _basePath is a symlink, use the real path rather than the path of the
197
+ // symlink as cache key.
198
+ const _basePath = path . resolve ( curPath , '_' + request ) ;
199
+ const basePath = path . resolve ( curPath , request ) ;
180
200
181
- if ( ! filename ) {
182
- if ( exts === undefined )
183
- exts = Object . keys ( Module . _extensions ) ;
184
- filename = tryPackage ( basePath , exts , isMain ) ;
185
- }
186
-
187
- if ( ! filename ) {
188
- // try it with each of the extensions at "index"
189
- if ( exts === undefined )
190
- exts = Object . keys ( Module . _extensions ) ;
191
- filename = tryExtensions ( path . resolve ( basePath , 'index' ) , exts , isMain ) ;
192
- }
201
+ const filename = tryFindPath ( _basePath , exts , true , isAbsolute ) ||
202
+ tryFindPath ( basePath , exts , isMain , isAbsolute ) ;
193
203
194
204
if ( filename ) {
195
205
// Warn once if '.' resolved outside the module dir
@@ -427,6 +437,7 @@ Module._resolveFilename = function(request, parent, isMain) {
427
437
}
428
438
429
439
var resolvedModule = Module . _resolveLookupPaths ( request , parent ) ;
440
+
430
441
var id = resolvedModule [ 0 ] ;
431
442
var paths = resolvedModule [ 1 ] ;
432
443
0 commit comments