Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't match string with leading dot #217

Open
vwkd opened this issue Aug 28, 2021 · 3 comments
Open

Can't match string with leading dot #217

vwkd opened this issue Aug 28, 2021 · 3 comments

Comments

@vwkd
Copy link

vwkd commented Aug 28, 2021

(Thanks for reporting an issue to micromatch! If you haven't already read the contributor guidelines, Please do that now, then proceed to fill out the details below.)

It seems that a leading dot prevents a glob from matching. For example, ./my/*/file.js should match ./my/relative/file.js but it doesn't.

Please describe the minimum necessary steps to reproduce this issue:

import micromatch from "micromatch";

const glob = `./my/*/file.js`
const isMatch = micromatch.matcher(glob);
console.log(isMatch(./my/relative/file.js`);
// logs false

What is happening that shouldn't be?

Glob doesn't match string.

What should be happening instead?

Glob should match string. It works with minimatch and also globster.xyz.

@jonschlinkert
Copy link
Member

Try passing a custom format function on the options, as follows:

const micromatch = require('micromatch');

const format = input => input.replace(/^\.\//, '');

console.log(micromatch.isMatch('./my/relative/file.js', 'my/*/file.js', { format })); //=> true
console.log(micromatch.isMatch('./my/relative/file.js', './my/*/file.js', { format })); //=> true
console.log(micromatch.isMatch('./my/relative/file.js', '**/my/*/file.js', { format })); //=> true

The issue of leading dot slashes has been discussed a few times on this and related repositories. Since ./ is contrived and is only really used in require() and import statements, it's not a pattern that is typically found in real file system paths. The most reliable way to handle such patterns is to use path.normalize() or otherwise ensure that your file paths are normalized before using any matcher. Or you can use micromatch's format option. We decided on this approach because it seemed to be the lesser of all other evils.

Related:

Please let me know if this helps.

@vwkd
Copy link
Author

vwkd commented Sep 18, 2021

Thanks for the reply. Feel free to close this if this indeed works as intended. Though it would be helpful to mention this in the README so other people won't have to run into debugging this issue.

@elmpp
Copy link

elmpp commented Apr 15, 2022

const match = micromatch(['/a/file/path/.hiddenFile.ejs.txt', '*.ejs.*', {matchBase: true}]

This is awful DX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants