magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

CSS minifiying sometimes results in blank CSS file due to PREG_JIT_STACK_LIMIT_ERROR in tubalmartin/cssmin

Open nahall opened this issue 1 year ago • 8 comments

Preconditions and environment

Magento 2.4+ PHP 8+

Steps to reproduce

Enable CSS minify

Expected result

CSS is minified properly

Actual result

Minified CSS ends up blank

Additional information

No response

Release note

Magento uses the tubalmartin/cssmin dependency to do CSS minification. In PHP 8+ this can results in blank minified CSS files, depending on the content of the CSS that is being minified. This is due to a bug in vendor/tubalmartin/cssmin/src/Minifier.php line 324:

    $css = preg_replace_callback(
        '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S",
        array($this, 'processStringsCallback'),
        $css
    );

Where this regexp is causing the backtrack limit to be reached and generating a PREG_JIT_STACK_LIMIT_ERROR. However, Minifier.php is not checking for this error so the $css variable ends up being set to null which ends up causing the deprecation message:

exception: Deprecated Functionality: preg_replace(): Passing null to parameter #3 ($subject) of type array|string

However, this is a red herring as the reason $css is null is due to this error not being caught in the earlier function call.

The bug report in tubalmartin/cssmin is https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port/issues/66

It seems like the options would be to fix the tubalmartin/cssmin dependency or switch Magento to a different library for CSS minification.

Current workaround is to disable CSS minification in Magento's config.

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [X] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

nahall avatar Dec 23 '23 19:12 nahall

Hi @nahall. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Dec 23 '23 19:12 m2-assistant[bot]

Hi @engcom-Dash. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
    1. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

m2-assistant[bot] avatar Dec 28 '23 04:12 m2-assistant[bot]

@magento give me 2.4-develop instance

engcom-Dash avatar Dec 28 '23 11:12 engcom-Dash

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

Hi @engcom-Dash, here is your Magento Instance: https://34dd16ce2d6b21223b86a148b078666d.instances-prod.magento-community.engineering Admin access: https://34dd16ce2d6b21223b86a148b078666d.instances-prod.magento-community.engineering/admin_5b3b Login: e3c315f8 Password: 30a9a585e723

Hi @nahall

Thanks for reporting and collaboration.

Verified the issue on magento develop instance and issue is not reproducible.

We have followed the below steps to reproduce:

  1. Log in to your Magento 2 admin panel 2.Navigate to Stores>Configuration 3.Go into the advanced tab and then the developer tab under configuration. 4.Go to CSS settings and set the minify option to ‘Yes'

The CSS is minified. properly. Please refer the attached screenshot. cssminified

engcom-Dash avatar Jan 03 '24 05:01 engcom-Dash

This does not fail with the stock Magento CSS but only in certain cases.

Since the problematic regexp in the CSS minifier is:

    $css = preg_replace_callback(
        '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S",
        array($this, 'processStringsCallback'),
        $css
    );

It seems likely that the CSS needs to contain a number of quotation marks in order to encounter the problem.

nahall avatar Jan 03 '24 15:01 nahall

Hi @nahall

Thanks for reporting and collaboration.

Can you please check if you are using any third party modules and try to reproduce the issue by disabling one by one. Please let us know if you are still facing the issue.

Thanks

engcom-Dash avatar Jan 08 '24 13:01 engcom-Dash

Hi @nahall

We have noticed that this issue has not been updated since long time.
Hence we assume that this issue is fixed now, so we are closing it. Please feel to raise a fresh ticket or reopen this ticket if you need more assistance on this.

Thanks.

engcom-Dash avatar Jan 24 '24 05:01 engcom-Dash

The bug is still happening. It is not because of third party modules but rather in certain cases of CSS.

nahall avatar Jan 24 '24 14:01 nahall

Here is workaround: https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port/issues/66#issuecomment-1868359514

VladyslavSikailo avatar Mar 26 '24 15:03 VladyslavSikailo

Why is this ticket closed? This is happening today.

Ryangr0 avatar Jun 20 '24 12:06 Ryangr0