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

remove dependency on path #23

Closed
wants to merge 4 commits into from
Closed

Conversation

Toxicable
Copy link

fixes micromatch/micromatch#175

This removes all dependency on the path module from nodejs.
This makes it easier for users downstream to bundle micromatch for use in browsers when using pre-configured bundlers like when using the Angular CLI or create react app

@@ -32,7 +36,7 @@ exports.isWindows = options => {
if (options && typeof options.windows === 'boolean') {
return options.windows;
}
return win32 === true || path.sep === '\\';
return win32 === true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to check the path sep, since we already know if we're on windows from the above check.
And path.sep is only ever \ when on windows https://nodejs.org/api/path.html#path_path_sep

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And path.sep is only ever \ when on windows

Naively, yes. But this also gave us a super easy way to explicitly set \\ as the path separator for tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but as below, I don't think setting path.sep should be a supported use case, so using that logic isn't correct with that in mind

lib/picomatch.js Outdated
return regex.test(path.basename(input));

// if it ends with a trailing / we filter out the last empty item that split will make
const basename = input.split(/[\\/]/).filter(v => v !== '').pop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this regex out of the function to save mem.

@paulmillr
Copy link
Member

The tests are broken now. They need to be fixed. Check for path.sep was there because we have been changing it in tests, I think.

@Toxicable
Copy link
Author

Ah right, just saw the comment here:

// Don't use "path.sep" here, as it's conceivable that it might have been
// modified somewhere by the user. Node.js only handles these two path separators
// with similar logic, and this is only for unit tests, so we should be fine.

We could require path back in, making it a dynamic require but i'd like to avoid that if possible.
Do you still think this comment is valid? While I agree, it's possible that the user can mutate path.sep, I don't think this should be a supported use case.

@paulmillr
Copy link
Member

Let's hear @jonschlinkert 's opinion.

@jonschlinkert
Copy link
Member

jonschlinkert commented Aug 22, 2019 via email

@paulmillr
Copy link
Member

@Toxicable let’s remove tests then!

test/options.js Outdated
assert.deepEqual(match(['E:\\a\\b\\c.md'], 'E:/**/*.md'), ['E:/a/b/c.md']);
assert.deepEqual(match(['E:\\a\\b\\c.md'], 'E:/**/*.md', { windows: false }), []);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why were these removed?

assert(isMatch('c:\\', '**', { windows: true }));
assert(isMatch('C:\\Users\\', '**', { windows: true }));
assert(isMatch('C:cwd/another', '**', { windows: true }));
assert(isMatch('C:cwd\\another', '**', { windows: true }));
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are wrong. The entire point was to test these using platform detection, and without windows: true

path.sep = '/';
assert(isMatch('c:\\', '*{,/}', { windows: true }));
assert(!isMatch('C:\\Users\\', '*', { windows: true }));
assert(!isMatch('C:cwd\\another', '*', { windows: true }));
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow, this test is simulating being on windows right?
Why not only run it on windows then?

assert(isMatch('\\\\unc\\admin$', '//*/*$', { windows: true }));
assert(isMatch('\\\\unc\\admin$\\system32', '//*/*$/*32', { windows: true }));
assert(isMatch('\\\\unc\\share\\foo', '//u*/s*/f*', { windows: true }));
assert(isMatch('foo\\bar\\baz', 'f*/*/*', { windows: true }));
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@Toxicable
Copy link
Author

@jonschlinkert For all the tests that are using windows specific inputs, with this new setup they should only be run on a windows host, right?.
It dosen't make sense to test a windows path when you're running on a host that uses posix paths.

@jonschlinkert
Copy link
Member

As I recall, I implemented the tests that way, and made the code comments in the tests, to remind myself that there was an edge case and reason they needed to be that way. Unfortunately, I don't remember at this point what the edge case was, and I'm not confident enough in my memory to say if I'm even correct. I'm not saying you're wrong, as you very well may be correct.

It dosen't make sense to test a windows path when you're running on a host that uses posix paths.

As my 10-year old nephew always says: "Never? Never ever ever?"

It sounds like you're saying that I'm wrong and that there are no edge cases that your changes will effect, and that you 100% aware of all edge cases on all platforms. Is that correct? Are you positive that there are no scenarios in which a user on a non-windows platform would see windows paths, over a remote connection or otherwise?

I'm not saying it happens, or that it happens often, I'm saying that it seems like there was a scenario where it happened, and I'm not 100% sure that it won't.

@jonschlinkert
Copy link
Member

Hey and just to be clear, I do appreciate the dialog and contribution. I'm just usually skeptical when an absolute assertion is made about Windows paths.

@Toxicable
Copy link
Author

Im not confidant that I know off the top of my head all use cases.
However, lets go back to the core issue here.

We currently do a check to see if path.sep is some value.
By allowing the user and our testing suite to mutate this value they can "simulate" being on a different OS.
However, in the situation where a user needs to process a windows path a\\b\\c on linux, we provide the { windows: true } option as a public API.

I propose that that since we already have a public API for supporting windows on a non windows OS, we don't need to do the path.sep check.

So for our testing suite we can test windows works on linux with { windows: true } and for other tests where this is not true, using a windows path as input, we should only test it on a Windows OS host.
An alternative for our testing suite is to instead mutate process.architecture instead
, but again it's not a supported public API.

I'd also just like to point our that users should never modify path.sep, or process.architecture
They'll run into more issues than just issues here.

@Toxicable
Copy link
Author

@jonschlinkert So whats the plan here.
Are we going to go ahead with this? Knowing that we cannot use path.sep as platform detection anymore?

@jonschlinkert
Copy link
Member

So whats the plan here.

Sorry for the really late reply. I think it's fine, but I just need to have a day where I can really dedicate some time to reviewing this comprehensively before I merge. Everything you said and did makes a lot of sense, I just need to digest it.

@jonschlinkert
Copy link
Member

@dawgwe1 you've been blocked

@micromatch micromatch deleted a comment Sep 25, 2021
@micromatch micromatch deleted a comment Sep 25, 2021
@idenisovs
Copy link

idenisovs commented Jan 31, 2022

Any progress in here? Our frontend builds is failing now because of path dependency in picomatch.

@acao
Copy link
Contributor

acao commented Feb 14, 2022

path and other node internals like os have been removed in #73, so this PR can be closed

If you want to use picomatch without any node.js dependencies, you can use this for now https://www.npmjs.com/package/picomatch-browser

@Toxicable Toxicable closed this Mar 9, 2023
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

Successfully merging this pull request may close these issues.

browser support - without bundler
5 participants