File tree 2 files changed +35
-3
lines changed
2 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -149,7 +149,11 @@ export function glob(
149
149
// cache of fs.realpathSync results to avoid extra I/O
150
150
const realpathCache : Map < string , string > = new Map ( ) ;
151
151
const { includeDirectories = false , followSymlinks = false } = options ;
152
-
152
+ // if we _specifically asked_ for something in node_modules, fine, otherwise ignore it
153
+ // to avoid globs like '**/*.ts' finding all the .d.ts files in node_modules.
154
+ // however, if the pattern is something like `!**/node_modules/**`, this will also
155
+ // cause node_modules to be considered, though it will be discarded by minimatch.
156
+ const shouldIncludeNodeModules = pattern . includes ( "node_modules" ) ;
153
157
let dir = dirs . shift ( ) ;
154
158
155
159
const handleFile = ( path : string ) => {
@@ -216,8 +220,10 @@ export function glob(
216
220
} ) ) {
217
221
if ( child . isFile ( ) ) {
218
222
handleFile ( child . name ) ;
219
- } else if ( child . isDirectory ( ) && child . name !== "node_modules" ) {
220
- handleDirectory ( child . name ) ;
223
+ } else if ( child . isDirectory ( ) ) {
224
+ if ( shouldIncludeNodeModules || child . name !== "node_modules" ) {
225
+ handleDirectory ( child . name ) ;
226
+ }
221
227
} else if ( followSymlinks && child . isSymbolicLink ( ) ) {
222
228
handleSymlink ( child . name ) ;
223
229
}
Original file line number Diff line number Diff line change @@ -97,6 +97,32 @@ describe("fs.ts", () => {
97
97
} ) ;
98
98
} ) ;
99
99
100
+ describe ( "when node_modules is present in the pattern" , function ( ) {
101
+ it ( "should traverse node_modules" , function ( ) {
102
+ fix . dir ( "node_modules" ) . addFile ( "test.ts" ) . path ;
103
+ fix . write ( ) ;
104
+ equal (
105
+ glob ( `${ fix . cwd } /node_modules/test.ts` , fix . cwd ) . map ( ( f ) =>
106
+ basename ( f )
107
+ ) ,
108
+ [ "test.ts" ]
109
+ ) ;
110
+ } ) ;
111
+ } ) ;
112
+
113
+ describe ( "when node_modules is not present in the pattern" , function ( ) {
114
+ it ( "should not traverse node_modules" , function ( ) {
115
+ fix . dir ( "node_modules" ) . addFile ( "test.ts" ) . path ;
116
+ fix . write ( ) ;
117
+ equal (
118
+ glob ( `${ fix . cwd } /**/test.ts` , fix . cwd ) . map ( ( f ) =>
119
+ basename ( f )
120
+ ) ,
121
+ [ ]
122
+ ) ;
123
+ } ) ;
124
+ } ) ;
125
+
100
126
it ( "should ignore anything that is not a file, symbolic link, or directory" , function ( done ) {
101
127
// Use unix socket for example, because that's easiest to create.
102
128
// Skip on Windows because it doesn't support unix sockets
You can’t perform that action at this time.
0 commit comments