Skip to content

Plugin filter RegEx doesn't support case-insensitive flag #4121

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

Closed
graysonlang opened this issue Mar 24, 2025 · 4 comments
Closed

Plugin filter RegEx doesn't support case-insensitive flag #4121

graysonlang opened this issue Mar 24, 2025 · 4 comments

Comments

@graysonlang
Copy link

graysonlang commented Mar 24, 2025

The filter argument for the onResolve and onLoad plugin API doesn't appear to respect the RegExp case-insensitivity flag.

For example, the following will only match files ending in .cpp or .c but not .C or .CPP even though the i flag is specified at the end of the RegExp literal.

build.onResolve({ filter: /\.c(pp)?$/i }, (args) => { ... }

I also attempted to use the new RegExp('\.c(pp)?$', 'i') form but it didn't work either. 😔

@hyrious
Copy link

hyrious commented Mar 24, 2025

Yes, looking at the source it passes only the source of regexp from JS to Go. You may manually match all cases without the help of flags.

{ filter: /\.[Cc]([pP][pP])?$/ }

@graysonlang
Copy link
Author

@hyrious Thanks for the link to the code that verifies what I'm seeing. I was mostly filing the issue to bring light to the lack of support. As you mentioned, this particular case could certainly be recomposed to handle all of the permutations as part of the pattern, though I intentionally picked a relatively simple case as an example. Sadly, expanding out my actual use case is a little bit messier. 😬

@evanw
Copy link
Owner

evanw commented Mar 27, 2025

Thanks for bringing this to my attention. This is just an oversight. It hadn't occurred to me to test this, and it's never come up for me. Of course it's very reasonable that this should work.

The way esbuild handles this is by taking the JavaScript regexp source code and passing it to Go's regexp engine. Apparently Go handles flags differently from JavaScript. The JavaScript regular expression new RegExp('\.c(pp)?$', 'i') is equivalent to the Go regular expression regexp.MustCompile(`(?i)\.c(pp)?$`) (the i flag turns into a (?i) prefix). So I'll need to do that substitution in esbuild's API.

@evanw evanw closed this as completed in 8f56771 Mar 27, 2025
@graysonlang
Copy link
Author

Thanks, Evan!

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