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

Merged CSS/JS files require pub/static/_cache writeable, contrary to documentation #13225

Open
scottsb opened this issue Jan 16, 2018 · 63 comments
Labels
Area: Other Developer Tools Component: Deploy Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: ready for dev Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S1 Affects critical data or functionality and forces users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@scottsb
Copy link
Member

scottsb commented Jan 16, 2018

Preconditions

  1. Have Magento installed with two filesystem owners as documented, with all code files (source and CLI-generated) read-only for web server user. This includes the full pub/static directory.
  2. Turn on the merging of CSS or JS files.

Steps to reproduce

Clear static file cache and load home page.

Expected result

No exception should be generated.

Actual result

Exception is logged:

[2018-01-16 16:18:56] main.CRITICAL: Directory "/var/www/html/___/pub/static/_cache/merged" cannot be created Warning!mkdir(): Permission denied {"exception":"[object] (Magento\\Framework\\Exception\\FileSystemException(code: 0): Directory \"/var/www/html/___/pub/static/_cache/merged\" cannot be created Warning!mkdir(): Permission denied at /var/www/html/___/vendor/magento/framework/Filesystem/Driver/File.php:225)"} []

Commentary

This could arguably be seen as a documentation issue in which case I can report it in the devdocs repo. However, as I understand it, it seems that the documentation does describe the intended state, meaning the code should be changed instead. I'm not sure if it's possible, but if it were, ideally the merged files would be generated by static-content:deploy as alluded to in #9542. This would allow multiple servers in a cluster to be certain to have the merged files present in response to requests without the directory being shared among the nodes.

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Jan 16, 2018
@davidalger
Copy link
Member

+1 here guys! Nothing should be written to pub/static when in production mode, and a setup:static-content:deploy should write out merged files to static/_cache. This causes problems in load-balanced environments where a snapshot of a pre-compiled build is deployed.

@dan-ding
Copy link

bump
sort of related: #9542

@ghost ghost self-assigned this Jul 31, 2018
@ghost ghost added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Progress: needs update labels Jul 31, 2018
@ghost
Copy link

ghost commented Jul 31, 2018

Hi @scottsb i cannot reproduce you scenario, which version of magento you've tested ?

@graemel
Copy link

graemel commented Aug 15, 2018

I see this behaviour in 2.1.

@davidalger
Copy link
Member

This is a problem with 2.2.x as well. It starts with the files in static/_cache/merged not being written during static content deploy, and then the issue is compounded (on either a read-only file system or ) when Magento attempts to write merged JS / CSS files to disk during a web request.

@ghost
Copy link

ghost commented Aug 17, 2018

@scottsb , we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

@ghost ghost closed this as completed Aug 17, 2018
@ishakhsuvarov ishakhsuvarov reopened this Aug 17, 2018
@ishakhsuvarov
Copy link
Contributor

@davidalger @scottsb Sorry for the confusion. This ticket was closed mistakenly. We will take a look at it again and let you know.

@ghost ghost added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Component: Deploy and removed Progress: needs update labels Aug 17, 2018
@ghost
Copy link

ghost commented Aug 17, 2018

Hi @scottsb , thank you for your report.
We've acknowledged the issue and added to our backlog.

@ghost ghost added the Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release label Aug 20, 2018
@ghost ghost removed their assignment Aug 20, 2018
@kirkmadera
Copy link

We also build separate from deploy and ran into this issue. We are expecting pub/static to actually be static and read only. We have also made the workaround to make the cache dir writable. Would be cleaner if these merged files generated as part of static content deploy.

@scrivvles
Copy link

We are experiencing this same issue on our production Magento 2.2.6 server.

@gwharton
Copy link
Contributor

Looking forward to an update on this. Running 2.2.7 on a cluster and had to disable merging of css, not because of non writeable /pub/static/_cache folder, as I have allowed it, but individual servers don't seem to be triggering the generation of the cache files. I seem to get many different min.css files generated, with different hash's, but not always the one that the browser is requesting. I'd love to see these generated in the static:content:deploy phase.

@kirkmadera
Copy link

I think I understand the reasoning behind this. It's because it has to create the merged file for every possible frontend page, which would be tough to generate statically, especially without a full running app present to crawl.

I honestly think documenting in Magento docs that pub/static/_cache needs to be writable and shared across all frontend servers would be enough.

@gwharton
Copy link
Contributor

Why shared. Does magento keep track of which ones have been generated, and in effect only generates it on one of the cluster nodes and 404s it from the rest of the cluster as all cluster nodes think its already generated, not knowing it is only present on one node.

That would certainly explain what i am seeing.

@scottsb
Copy link
Member Author

scottsb commented Jan 11, 2019

I was mulling over this last night and was thinking of how this could be solved. This is rough and may not be workable, but it might be a start:

  1. Change the generation of the files to happen on-demand rather than as part of the triggering page request. This would allow whatever server receives the request to generate the file locally, even if it's a different server than triggered it originally. This file could then be cached using the general cache config (such as in Redis or at least in the expected var cache directory if using disk). In order for this to work the URL either has to include sufficient data or reference some cache key (which would be stored during the initial page request).

  2. The first step alone would avoid the present filesystem permissions issue but at the significant cost of requiring a Magento boot to serve the merged files, even if actual processing is quick once cached. So the next step would be changing the default Varnish VCL config to include these merged static files. (Currently all non-HTML assets are excluded by default.) This only solves the problem for those using Varnish, but that's recommended config for prod in any case.

A different, more exotic option would be to keep the merging in the initial page request but to push the data into the full page cache inline. This wouldn't be hard for Magento's built-in FPC, but for Varnish it would require us to push data out to it rather than waiting on it to cache while proxying. This is possible but is a rather non-standard approach.

@scottsb
Copy link
Member Author

scottsb commented Jan 11, 2019

@gwharton Yes, Magento merges the file as part of the page request and saves it the local disk. It's expected that the file is then served directly by the web server as a static file from then on (it never routes through PHP/Magento). But if the merged asset request goes to a different server in the cluster, then it won't be present locally if the directory isn't shared.

@gwharton
Copy link
Contributor

@scottsb Yeah, I just deployed a shared writable pub/static/_cache folder across my cluster and re-enabled merging of css files and all seems ok.

@geerlingguy
Copy link

geerlingguy commented Jan 23, 2019

I've also had to mount pub/static/_cache as a shared folder in my Kubernetes environment since any time more than one Pod (container) is running, the file will be generated on one, but not another, and that kinda messes things up—plus I have to make sure it's writeable (which it wasn't earlier).

@Stoyvo
Copy link

Stoyvo commented Aug 13, 2022

Magento 2.4.4 - Issue still present.

CSS/JS should never be generated on web-request, these are static files, they do not change, having this part of the web request is a critical design flaw. It severely affects containerized applications and forces infrastructure work-arounds.

Merged JS / CSS should be built in static content deploy and not generated by frontend request.

@github-jira-sync-bot github-jira-sync-bot added Progress: PR Created Indicates that Pull Request has been created to fix issue and removed Progress: ready for grooming labels Sep 19, 2022
@mattgrul
Copy link

mattgrul commented Oct 3, 2022

With the current "PR Created" label being added I'm hoping we are close to a possible solution for this issue. However I can't seem to find any corresponding PR, does anyone have a link?

@bsp-cory
Copy link

bsp-cory commented Oct 3, 2022

@mattgrul if there is a PR, I would LOVE to see it. This has been driving me nuts for a couple of years now, and it would really help my quality of life if I didn't have to keep selectively changing permissions on these static files.

@kanevbg
Copy link

kanevbg commented Oct 3, 2022

I do not see PR either. Would be very pleased to see such too. Same opinion as of @Stoyvo , it's really unacceptable and pretty strange such a flaw to exists so much time on such advanced system. Someone should add more labels, like "platform health"

@erfanimani
Copy link
Contributor

For those wondering, the PR is likely created on Adobe's internal VCS, so you know it's being worked on.

@engcom-Hotel
Copy link
Contributor

Hello @mattgrul @bsp-cory @kanevbg @erfanimani,

The label Progress: PR Created was added due to the wrong status made in JIRA. It was corrected. Currently, there is no PR created for this issue.

We have tried to reproduce the issue in the latest development of Magento ie 2.4-develop but the issue is not reproducible. It might the issue is fixed and merged with the development branch.

The steps we have followed are as follows:

  1. Followed the below documentation to create 2 users one is for the web server and the other one is for CLI:
    https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/deployment/file-system-permissions.html
    https://experienceleague.adobe.com/docs/commerce-operations/installation-guide/prerequisites/file-system/configure-permissions.html?lang=en#about-the-shared-group
  2. Install Magento in the folder.
  3. Change with production mode: bin/magento deploy:mode:set production
  4. Ran static content deploy command: bin/magento setup:static-content:deploy
  5. Try to access the Homepage.

We are able to access the homepage without any issues. Please let us know in case we have missed anything.

Thanks

@engcom-Hotel engcom-Hotel added Issue: needs update Additional information is require, waiting for response and removed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Oct 27, 2022
@m2-community-project m2-community-project bot added Progress: ready for dev and removed Progress: PR Created Indicates that Pull Request has been created to fix issue labels Oct 27, 2022
@dooblem
Copy link

dooblem commented Oct 28, 2022

Did you enable merge js/css as mentionned in the issue ?

@mattgrul
Copy link

@engcom-Hotel It seems like you are not fully understanding the issue. If you read through the entire thread you will see this is an issue with load-balanced environments. The contents of static/_cache should be written during the compile stage, not, during the first initial web request.

@Stoyvo
Copy link

Stoyvo commented Oct 28, 2022

@engcom-Hotel

The appropriate way to test this will be:

  1. Magento 2 is installed, functional, visible on frontend.
  2. Change to Production mode php bin/magento deploy:mode:set production
  3. Enable "merged css": php bin/magento config:set dev/css/merge_css_files 1
  4. Flush Cache just incase php bin/magento cache:flush
  5. On server, verify no merged css files exist in ls pub/static/_cache/merged/
  6. Run static content deploy: php bin/magento setup:static-content:deploy
  7. Verify merged css is visible: ls pub/static/_cache/merged/
  8. Load frontend by loading Magento 2 in web browser
  9. Check again for merged css ls pub/static/_cache/merged/

IF merged css is missing in Step 7 AND missing in Step 9, then merged CSS is not working correctly, please retry the steps.

IF merged CSS is missing in Step 7, but is visible in Step 9, then you have correctly recreated the issue. Static content deploy did not generate the file as it's expected to.

@engcom-Hotel
Copy link
Contributor

engcom-Hotel commented Oct 31, 2022

Hello @Stoyvo,

Thanks for the detailed steps. We have followed the above steps as well. and for is the issue is still not reproducible. I have to add some points here to notice:

  1. We have used the shared group as mentioned in this document..
  2. We already have a www-data user in the www-data group, so we create one more user in the same group with CLI/SSH access.

Please let us know if we have missed anything.

Thanks

@engcom-Hotel engcom-Hotel added the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Oct 31, 2022
@github-jira-sync-bot
Copy link

❌ Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-1005

@engcom-Hotel engcom-Hotel added Issue: needs update Additional information is require, waiting for response and removed Issue: needs update Additional information is require, waiting for response labels Oct 31, 2022
@erfanimani
Copy link
Contributor

Related issue: #29172

@p24-max
Copy link

p24-max commented Apr 7, 2023

I'm also facing this issue.
pub/static/_cache/merged/ files are not generated with command bin/magento setup:static-content:deploy.
The first request(s) are extremly slow due to this.

Only "workaround" what I currently found is disabling the CSS merging, but this is not a solution.
Does someone found a better workaround to keep merging enabled?

--
bump @engcom-Hotel

@engcom-Hotel engcom-Hotel removed the Issue: needs update Additional information is require, waiting for response label Apr 10, 2023
@Stoyvo
Copy link

Stoyvo commented May 30, 2023

@p24-max

Does someone found a better workaround to keep merging enabled?

I use GCP containers with a "gfuse" mount to a public storage bucket where first request builds it, but it is used on every request going forward. This adds overhead to my container start time, but it's faster than rebuilding merged css.

@gwharton
Copy link
Contributor

7 years on, and we're all still generating static content triggered by the first few browsers of our sites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Other Developer Tools Component: Deploy Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: ready for dev Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S1 Affects critical data or functionality and forces users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
None yet
Development

No branches or pull requests