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

[AssetMapper] Fix JavaScriptImportPathCompiler regression in regex #54079

Merged
merged 1 commit into from
Feb 29, 2024

Conversation

PhilETaylor
Copy link
Contributor

@PhilETaylor PhilETaylor commented Feb 27, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #54078
License MIT

Fixes regression in regex, fix provided by @nicolas-grekas in #54078

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.4" but it seems your PR description refers to branch "6.4,".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@PhilETaylor
Copy link
Contributor Author

I have also updated the https://regex101.com/r/1iBAIb/1 to https://regex101.com/r/1iBAIb/2 by adding the same change there.

@weaverryan
Copy link
Member

Thanks for this :). Do we know if there is a test case that could be created to show the problem? Due to the complexity of this regex, we rely heavily on the tests to avoid regressions like this.

@PhilETaylor
Copy link
Contributor Author

PhilETaylor commented Feb 27, 2024

@weaverryan The reproducer I created was in the linked issue #54078

symfony new . --version="6.4.*" --webapp
php bin/console importmap:req @yaireo/tagify
php bin/console importmap:update 

@PhilETaylor
Copy link
Contributor Author

If Im honest I dont really know what the root problem is enough to create some regression unit tests sorry. I just stumbled on and reported the issue first once I had a reproducer, and used @nicolas-grekas provided solution as a PR when requested.

@nicolas-grekas
Copy link
Member

This happens because of big js files to parse with some content that trigger deep backtracking. Not easy to reliably test I fear...

@smnandre
Copy link
Contributor

Thank you @PhilETaylor for this fix.
We're trying to handle so many cases with a single regexp...

Hopefully this one would bring us near the 99% we target, without having to implement a real JS parser (that would slow things down a lot for everyone)

@OskarStark OskarStark changed the title [AssetMapper] Fix JavaScriptImportPathCompiler regression in regex [AssetMapper] Fix JavaScriptImportPathCompiler regression in regex Feb 29, 2024
@smnandre
Copy link
Contributor

This happens because of big js files to parse with some content that trigger deep backtracking. Not easy to reliably test I fear...

I experienced the bug right now, while updating a branch i was working on the ux website... Fixed locally with a dirty patch...

            $relativeImportPath = $this->makeRelativeForJavaScript($relativeImportPath);

            return str_replace($importedModule, $relativeImportPath, $fullImportString);
-        }, $content, -1, $count, \PREG_OFFSET_CAPTURE);
+        }, $content, -1, $count, \PREG_OFFSET_CAPTURE) ?? $content;
    }

With your example i think i see the problem here ... https://regex101.com/r/WueQkU/1

Capture d’écran 2024-02-29 à 14 00 57

.. and this file does not contains a single "import" 😭

So what about we also add a pre-check and ignore files that do not contain "import" ?

@stof
Copy link
Member

stof commented Feb 29, 2024

@smnandre A str_contains check for import might indeed help for the case of files without any imports to exclude them faster. But I think we could still have such nasty backtracking for big JS files containing. So we need this PR.

Btw, I just thought about a case not covered by the current regex. But I'll wait for this PR to be merged before doing my PR as they would conflict.

@smnandre
Copy link
Contributor

So we need this PR.

Of course! I just suggested an additional check :)

I'll open another one to throw an expression instead of returning null.

@nicolas-grekas
Copy link
Member

Thank you @PhilETaylor.

@nicolas-grekas nicolas-grekas merged commit 91278da into symfony:6.4 Feb 29, 2024
5 of 9 checks passed
fabpot added a commit that referenced this pull request Mar 4, 2024
…PCRE error (smnandre)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[AssetMapper] Throw exception in Javascript compiler when PCRE error

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #...
| License       | MIT

`preg_match_callback` can return null when a PCRE error occured, leading there to a TypeError.

Let's throw an exception and expose the error message.

(follows #54078 / complementary to #54079 )

Commits
-------

2333b58 [AssetMapper] Throw exception in Javascript compiler when PCRE error
@ph-il
Copy link

ph-il commented Mar 4, 2024

Got the same problem with
'@fullcalendar/core/internal.js' => [
'version' => '6.1.11',
],
So if you need more test case.

@fabpot fabpot mentioned this pull request Mar 4, 2024
@fabpot fabpot mentioned this pull request Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants