Skip to content

[AssetMapper] Fixing bug where a circular exception could be thrown while making error message #51345

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\NullLogger;
use Symfony\Component\AssetMapper\AssetDependency;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\MappedAsset;

Expand Down Expand Up @@ -57,8 +58,12 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
if (!$dependentAsset) {
$message = sprintf('Unable to find asset "%s" imported from "%s".', $matches[1], $asset->sourcePath);

if (null !== $assetMapper->getAsset(sprintf('%s.js', $resolvedPath))) {
$message .= sprintf(' Try adding ".js" to the end of the import - i.e. "%s.js".', $matches[1]);
try {
if (null !== $assetMapper->getAsset(sprintf('%s.js', $resolvedPath))) {
$message .= sprintf(' Try adding ".js" to the end of the import - i.e. "%s.js".', $matches[1]);
}
} catch (CircularAssetsException $e) {
// avoid circular error if there is self-referencing import comments
}

$this->handleMissingImport($message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AssetMapper\Exception;

/**
* Thrown when a circular reference is detected while creating an asset.
*/
class CircularAssetsException extends RuntimeException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\AssetMapper\Factory;

use Symfony\Component\AssetMapper\AssetMapperCompiler;
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
Expand All @@ -36,7 +37,7 @@ public function __construct(
public function createMappedAsset(string $logicalPath, string $sourcePath): ?MappedAsset
{
if (\in_array($logicalPath, $this->assetsBeingCreated, true)) {
throw new RuntimeException(sprintf('Circular reference detected while creating asset for "%s": "%s".', $logicalPath, implode(' -> ', $this->assetsBeingCreated).' -> '.$logicalPath));
throw new CircularAssetsException(sprintf('Circular reference detected while creating asset for "%s": "%s".', $logicalPath, implode(' -> ', $this->assetsBeingCreated).' -> '.$logicalPath));
}

if (!isset($this->assetsCache[$logicalPath])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface;
use Symfony\Component\AssetMapper\Compiler\JavaScriptImportPathCompiler;
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\MappedAsset;

Expand Down Expand Up @@ -277,6 +278,31 @@ public static function provideMissingImportModeTests(): iterable
];
}

public function testErrorMessageAvoidsCircularException()
{
$assetMapper = $this->createMock(AssetMapperInterface::class);
$assetMapper->expects($this->any())
->method('getAsset')
->willReturnCallback(function ($logicalPath) {
if ('htmx' === $logicalPath) {
return null;
}

if ('htmx.js' === $logicalPath) {
throw new CircularAssetsException();
}
});

$asset = new MappedAsset('htmx.js', '/path/to/app.js');
$compiler = new JavaScriptImportPathCompiler();
$content = '//** @type {import("./htmx").HtmxApi} */';
$compiled = $compiler->compile($content, $asset, $assetMapper);
// To form a good exception message, the compiler will check for the
// htmx.js asset, which will throw a CircularAssetsException. This
// should not be caught.
$this->assertSame($content, $compiled);
}

private function createAssetMapper(): AssetMapperInterface
{
$assetMapper = $this->createMock(AssetMapperInterface::class);
Expand Down