Skip to content

Commit

Permalink
bug #53701 [AssetMapper] Fix exception if assets directory is missing…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
nicolas-grekas committed Feb 1, 2024
2 parents 4fe7828 + 962a044 commit 30a02c4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
param('kernel.project_dir'),
abstract_arg('array of excluded path patterns'),
abstract_arg('exclude dot files'),
param('kernel.debug'),
])

->set('asset_mapper.public_assets_path_resolver', PublicAssetsPathResolver::class)
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/AssetMapper/AssetMapperRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(
private readonly string $projectRootDir,
private readonly array $excludedPathPatterns = [],
private readonly bool $excludeDotFiles = true,
private readonly bool $debug = false
) {
}

Expand Down Expand Up @@ -147,7 +148,7 @@ private function getDirectories(): array
$this->absolutePaths = [];
foreach ($this->paths as $path => $namespace) {
if ($filesystem->isAbsolutePath($path)) {
if (!file_exists($path)) {
if (!file_exists($path) && $this->debug) {
throw new \InvalidArgumentException(sprintf('The asset mapper directory "%s" does not exist.', $path));
}
$this->absolutePaths[realpath($path)] = $namespace;
Expand All @@ -161,7 +162,9 @@ private function getDirectories(): array
continue;
}

throw new \InvalidArgumentException(sprintf('The asset mapper directory "%s" does not exist.', $path));
if ($this->debug) {
throw new \InvalidArgumentException(sprintf('The asset mapper directory "%s" does not exist.', $path));
}
}

return $this->absolutePaths;
Expand Down

0 comments on commit 30a02c4

Please sign in to comment.