Decoding the Asset Manifest Error in Symfony Templates
When you switch from a static href to the dynamic {{ asset() }} helper in Twig, you are explicitly telling Symfony to resolve that path through its asset management system. If your project is set up with Webpack Encore or an equivalent bundler (common in modern Symfony 4 setups), this triggers a specific requirement: the presence of a manifest file at public/build/manifest.json.
The error message Asset manifest file "/../public/build/manifest.json" does not exist is not a generic template rendering failure. It is a precise signal that the asset compiler has not yet generated its cache file, or the build process was never run for the current environment.
Unlike the hardcoded string "css/mystyle.css", which simply points to a relative URL in the browser's address bar, {{ asset('css/mystyle.css') }} asks Symfony: "Where is this file located in the public directory after compilation?" If the answer isn't found in the manifest, the kernel halts rendering.
The Missing Build Step
In many modern Symfony workflows, CSS and JS files are not served directly from the public/ folder as written. Instead, they are processed by Webpack, bundled, minified, and named with content hashes (e.g., mystyle.css might become mystyle.a1b2c3d4.css). The manifest file records these transformations in a JSON format so that Twig can return the correct filename without hardcoding it.
If you are seeing this error, the build pipeline has likely not been executed for your current environment. Webpack Encore typically generates this manifest only when running in production mode or when explicitly instructed to do so.
To resolve this, you must trigger the asset compilation:
php bin/console assets:install public
npm run prod
The assets:install command ensures that any files generated by your bundler are copied into the public/ directory. The npm run prod (or yarn build) command runs the actual Webpack compilation, which generates the manifest.json.
Environment Variables and Cache
Sometimes the issue is not missing files but rather an environment mismatch. The asset compiler often checks for the APP_ENV variable. If your .env file specifies APP_ENV=dev, Webpack Encore might be configured to skip manifest generation or write it to a different location (like public/build_dev/).
Ensure your environment matches the output directory expectations. Check your webpack.config.js (or the equivalent in newer Encore versions) to see where the manifest is expected. If you are developing, you might intentionally want to bypass this strict manifest check by using the raw path, but for production, the manifest is mandatory.
Debugging Path Resolution
If you have run the build commands and the file still seems missing, inspect the public/build directory.
- Run
ls public/build/manifest.json. If it doesn't exist, the build failed silently or was skipped. - Check your
webpack.config.jsfor a custom output path configuration that might be pointing to a non-existent folder.
Trade-offs: Static vs. Dynamic Assets
You mentioned that using <link href="css/mystyle.css"> works. This is expected behavior because it bypasses the asset compiler entirely. It serves the file exactly as it sits in your public/css folder, regardless of whether it has been bundled or hashed.
However, relying on static paths defeats the primary purpose of an asset bundler: cache busting and dependency management. When you use {{ asset() }}, you ensure that if mystyle.css changes, the browser receives a new version because the filename in the manifest (and thus the rendered HTML) will have changed.
In a production environment, skipping the manifest leads to stale assets being served to users, breaking layouts or exposing unminified code. The error you encountered is Symfony's safety mechanism preventing you from deploying an inconsistent build state.
Final Recommendation
Before deploying any Symfony application using Webpack Encore, always ensure the asset compilation has completed successfully for the target environment. If you are unsure whether the manifest exists, adding a check in your controller or verifying the file system directly before rendering can prevent these runtime exceptions during critical moments of deployment.
