minimatch: "**/" pattern doesn't match "./" path part

This seems like a bug to me, intuitively, but I don’t see it addressed in the docs anywhere:

var minimatch = require("minimatch");

minimatch("foo/bar.js", "**/foo/**")     // true
minimatch("./foo/bar.js", "./**/foo/**") // true
minimatch("./foo/bar.js", "**/foo/**")   // false

Shouldn’t the last example return true because **/ should match ./?

Ref. cowboy/node-globule#11

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 16
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

@kimurakenshi the import plugin has another rule that requires a normalized path; i suggest using that as well.

** does not match . unless {dot:true} is set, and even if it is set, it does not ever match . or .. as path portions.

Paths starting with ./ and ../ never match a pattern that doesn’t start explicitly with ./ or ../, even if {dot:true} is set.

This is by design, and matches the behavior of bash 4.3+.

@corbanbrook I had a similar issue - #51 - and solved it by passing {dot: true} as the third argument to minimatch():

minimatch("/Users/corban/.go/src/app.js", "**/app.js", {dot: true}) // true

This doesn’t fix @cowboy’s case though:

minimatch("./foo/bar.js", "**/foo/**", {dot: true}) // false

https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-useless-path-segments.md If you have further questions, please file them on eslint-plugin-import so we don’t create more noise here.

As stated back in February, * and ** (and other wildcard patterns) do not match path portions starting with . unless {dot:true} is set, and ** will not never match . or .. as path portions. (Otherwise you’d have an infinite regress; if a/b/c exists, then a/./b/./c also exists, as does a/././b/././c etc. Should ** return an infinite list of entries?)

If you are receiving paths that may contain . or .. in them (for example, you have a pattern like a/b/c and the user can pass in a/./b/c, and expects a match), I recommend resolving those prior to passing them to minimatch. You can do this using the join or resolve methods in Node’s built-in path module.

I know this is an old issue, but I manage to fix my problem with matchBase option. In my case, I wanted that all .scss files - regardless the folder level - were matched. Using **/.scss didn’t solve the problem, but the matchBase: true did.