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] Asset Mapper fail after deleting "assets" folder #53669

Closed
Xbirdfr opened this issue Jan 29, 2024 · 7 comments
Closed

[AssetMapper] Asset Mapper fail after deleting "assets" folder #53669

Xbirdfr opened this issue Jan 29, 2024 · 7 comments

Comments

@Xbirdfr
Copy link

Xbirdfr commented Jan 29, 2024

Symfony version(s) affected

7.*

Description

After compiling assets, if we delete the original assets folder (I don't want it on prod), symfony crash with :

An exception has been thrown during the rendering of a template ("The asset mapper directory "assets/" does not exist.").

{% block importmap %}{{ importmap('app') }}{% endblock %}

How to reproduce

  • Setup a new symfony project
  • create a controller to display a page
  • compile assets
  • check your application (it's works)
  • delete the assets folder
  • check your application (it's don't work anymore)
symfony new --webapp assetmappertest .
symfony console make:controller home
symfony console assets:install public
symfony console importmap:install
symfony console asset-map:compile

Possible Solution

No response

Additional Context

No response

@fracsi
Copy link
Contributor

fracsi commented Jan 29, 2024

This also effects 6.4 as well. Currently you can use a simple workaround, just leave or create an empty assets directory.

@Xbirdfr
Copy link
Author

Xbirdfr commented Jan 29, 2024

assets folders + assets/controllers are needed at least (put a .empty do the trick)

@rynhndrcksn
Copy link
Contributor

rynhndrcksn commented Jan 30, 2024

It looks like this error is thrown from AssetMapperRepository.php, specifically line 164.

I've verified the following check fixes this, I'm unsure if it's the solution that the core Symfony team would want though, or if they'd want a more thorough check (like if this condition is true and public/assets/ exists or something):

if ($_ENV['APP_ENV'] !== 'dev' && $path === 'assets/') {
    continue;
}

@nicolas-grekas
Copy link
Member

I closed your PR a bit too quickly @rynhndrcksn, sorry about that. This cannot be the correct approach to me.
Maybe we could remove those exceptions in getDirectories? Or throw them only when $debug is on (the flag would be passed as a constructor argument).
WDYT @weaverryan?

@rynhndrcksn
Copy link
Contributor

rynhndrcksn commented Jan 30, 2024

No problem @nicolas-grekas ! I didn't know that hard coded values weren't okay, my apologies. I'll be happy to make the necessary changes once we get feedback from Ryan and make a new PR if that's okay!

@weaverryan
Copy link
Member

Indeed, you should be able to delete assets/ if you've compiled everything. If that's not possible (obviously it's not yet!) we should make it possible! The exception exists to help users not do a typo and then have trouble figuring out what they did wrong. In the PR #53677, I would:

A) Add a new argument to AssetMapperRepository constructor: $strictMode = true.
B) Add a new abstract arg after this one - https://github.com/symfony/symfony/blob/7.1/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php#L77 - called something like strict mode
C) Pass %kernel.debug% to this new argument here https://github.com/symfony/symfony/blob/7.1/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L1329
D) Finally, back in AssetMapperRepository, use $this->strictMode to determine if the exception should be thrown.

Cheers!

@rynhndrcksn
Copy link
Contributor

Ah okay, that makes a lot of sense, thank you Ryan! I'll get these modifications made and pushed later today!

nicolas-grekas added a commit that referenced this issue Feb 1, 2024
… in production (rynhndrcksn)

This PR was merged into the 6.4 branch.

Discussion
----------

 [AssetMapper] Fix exception if assets directory is missing in production

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

During deployment some people will compile their assets with Asset Mapper then delete the root `assets/` directory. This causes Asset Mapper to throw an exception, and a common work around people mentioned in the issue is to create an empty `assets/` directory.

This PR makes it so the exception is only thrown while `kernel.debug` is equal to true, letting developers know locally that there's an issue, but allowing people to safely do this in their `production` environments.

Commits
-------

962a044 Fix exception if assets dir is missing in prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants