blade-ui-kit / blade-heroicons

A package to easily make use of Heroicons in your Laravel Blade views.
https://blade-ui-kit.com/blade-icons
MIT License
560 stars 50 forks source link

Cannot run Optimize command with Laravel 11 #47

Closed dwightwatson closed 5 months ago

dwightwatson commented 8 months ago

Description:

I upgraded an existing app to Laravel 11, tests ran fine, but it failed during deployment running php artisan optimize. The page works fine when viewed in a browser (the icon appears). I tried to determine if it was something specific to my project but I was able to replicate this in a brand new Laravel 11 project also.

➜  heroicons php artisan optimize

   INFO  Caching framework bootstrap, configuration, and metadata.  

  config ............................................................................................................................... 7.62ms DONE
  events ............................................................................................................................... 0.71ms DONE
  routes ............................................................................................................................... 5.56ms DONE
  views ............................................................................................................................... 28.61ms FAIL

   InvalidArgumentException 

  Unable to locate a class or view for component [heroicon-s-bars-3].

  at vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php:311
    307▕         if (Str::startsWith($component, 'mail::')) {
    308▕             return $component;
    309▕         }
    310▕ 
  ➜ 311▕         throw new InvalidArgumentException(
    312▕             "Unable to locate a class or view for component [{$component}]."
    313▕         );
    314▕     }
    315▕ 

      +2 vendor frames 

  3   [internal]:0
      Illuminate\View\Compilers\ComponentTagCompiler::Illuminate\View\Compilers\{closure}(["<x-heroicon-s-bars-3 />", "heroicon-s-bars-3", "", ""])
      +7 vendor frames 

  11  [internal]:0
      Illuminate\Foundation\Console\ViewCacheCommand::Illuminate\Foundation\Console\{closure}(Object(Symfony\Component\Finder\SplFileInfo), "/Users/dwight/Sites/heroicons/resources/views/welcome.blade.php")

Steps To Reproduce:

Create a new Laravel 11 project, add this library.

laravel new heroicons
cd heroicons
composer require blade-ui-kit/blade-heroicons

Add an icon to the welcome page: <x-heroicon-s-bars-3 />

php artisan optimize
driesvints commented 8 months ago

Managed to reproduce it. Super odd. I don't have time atm to deep dive into this so would appreciate it if anyone has any insights.

dwightwatson commented 7 months ago

I just realised that Laravel 11's artisan optimize command now includes view caching which wasn't included in Laravel 10, which explains why I'm only running into this now.

dwightwatson commented 7 months ago

In the ViewCacheCommand it gets an instance of BladeCompiler from the view. I'm not fully familiar with what is going on here:

$compiler = $this->laravel['view']->getEngineResolver()->resolve('blade')->getCompiler();

Dumping this instance, it doesn't have all the Heroicon aliases registered:

Illuminate\View\Compilers\BladeCompiler^ {#255
  #files: Illuminate\Filesystem\Filesystem^ {#256}
  #cachePath: "/Users/dwight/Sites/heroicons/storage/framework/views"
  #basePath: ""
  #shouldCache: true
  #compiledExtension: "php"
  #extensions: []
  #customDirectives: array:1 [
    "svg" => Closure($expression)^ {#254
      class: "BladeUI\Icons\BladeIconsServiceProvider"
      this: BladeUI\Icons\BladeIconsServiceProvider {#196 …}
    }
  ]
  #conditions: []
  #prepareStringsForCompilationUsing: []
  #precompilers: []
  #path: null
  #compilers: array:3 [
    0 => "Extensions"
    1 => "Statements"
    2 => "Echos"
  ]
  #rawTags: array:2 [
    0 => "{!!"
    1 => "!!}"
  ]
  #contentTags: array:2 [
    0 => "{{"
    1 => "}}"
  ]
  #escapedTags: array:2 [
    0 => "{{{"
    1 => "}}}"
  ]
  #echoFormat: "e(%s)"
  #footer: []
  #rawBlocks: []
  #anonymousComponentPaths: []
  #anonymousComponentNamespaces: []
  #classComponentAliases: array:2 [
    "dynamic-component" => "Illuminate\View\DynamicComponent"
    "icon" => "BladeUI\Icons\Components\Icon"
  ]
  #classComponentNamespaces: []
  #compilesComponentTags: true
  #firstCaseInSwitch: true
  #echoHandlers: []
  #lastFragment: null
  -encodingOptions: 15
  #lastSection: null
  #forElseCounter: 0
} // vendor/laravel/framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php:61

If I swap that out and get the BladeCompiler directly from the container myself though, all the aliases are present:

dd(app('blade.compiler'));
Illuminate\View\Compilers\BladeCompiler^ {#1372
  #files: Illuminate\Filesystem\Filesystem^ {#1373}
  #cachePath: "/Users/dwight/Sites/heroicons/storage/framework/views"
  #basePath: ""
  #shouldCache: true
  #compiledExtension: "php"
  #extensions: []
  #customDirectives: array:1 [
    "svg" => Closure($expression)^ {#1371
      class: "BladeUI\Icons\BladeIconsServiceProvider"
      this: BladeUI\Icons\BladeIconsServiceProvider {#1321 …}
    }
  ]
  #conditions: []
  #prepareStringsForCompilationUsing: []
  #precompilers: []
  #path: null
  #compilers: array:3 [
    0 => "Extensions"
    1 => "Statements"
    2 => "Echos"
  ]
  #rawTags: array:2 [
    0 => "{!!"
    1 => "!!}"
  ]
  #contentTags: array:2 [
    0 => "{{"
    1 => "}}"
  ]
  #escapedTags: array:2 [
    0 => "{{{"
    1 => "}}}"
  ]
  #echoFormat: "e(%s)"
  #footer: []
  #rawBlocks: []
  #anonymousComponentPaths: []
  #anonymousComponentNamespaces: []
  #classComponentAliases: array:1178 [
    "dynamic-component" => "Illuminate\View\DynamicComponent"
    "icon" => "BladeUI\Icons\Components\Icon"
    "heroicon-c-academic-cap" => "BladeUI\Icons\Components\Svg"
    ... and so on

Perhaps this is a bug with the ViewCacheCommand instead?

abishekrsrikaanth commented 7 months ago

Is this issue when only using heroicons or all icon packages part of blade-ui-kit?

dwightwatson commented 7 months ago

I didn't test with blade-ui-kit as I was using herocions specifically.

Easy fix for the meantime is to simply call config:cache, event:cache, route:cache and view:cache separately instead of the optimize command. But probably still something that is worth fixing.

abishekrsrikaanth commented 7 months ago

Ya, that is what I ended up doing for all my deployment scripts after upgrading to Laravel 11. I hope this gets fixed soon.

mihob commented 6 months ago

Any news on this issue? i have the same problem with heroicon-o-minus.

driesvints commented 6 months ago

Sorry all, I really want to see this one fixed but just don't have the time right now (I have a kid underway). I'd love for anyone to be able to help out here.

bobmulder commented 5 months ago

Best of luck and congratulations, @driesvints! I faced the same issue, and will try to debug today.

Jubeki commented 5 months ago

This seems to be fixed in laravel/framework v11.8.0.

Not exactly sure where it was fixed, probably related PR in the framework: https://github.com/laravel/framework/pull/51450

driesvints commented 5 months ago

Would be cool if more people could confirm this!

@Jubeki is https://github.com/laravel/framework/issues/50619 fixed then as well?

Jubeki commented 5 months ago

Should be, as it describes the same issue. (Probably best to wait for confirmation from other people)

dwightwatson commented 5 months ago

Yeah - this is back in action for me after updating. Nice catch @Jubeki

poldixd commented 4 weeks ago

I get the same error. In my development environment on the Mac (macos 15) the command runs without error.

❯ pa optimize

   INFO  Caching framework bootstrap, configuration, and metadata.  

  config .............................................................................................................................. 27.07ms DONE
  events ............................................................................................................................... 1.27ms DONE
  routes .............................................................................................................................. 23.28ms DONE
  views .............................................................................................................................. 488.88ms DONE

❯ pa about --only=Environment

  Environment ......................................................................................................................................  
  Application Name ......................................................................................................................... Laravel  
  Laravel Version .......................................................................................................................... 11.27.2  
  PHP Version ............................................................................................................................... 8.3.12  
  Composer Version ........................................................................................................................... 2.8.1  
  Environment ................................................................................................................................ local  
  Debug Mode ............................................................................................................................... ENABLED  
  URL ...................................................................................................................... 
  Maintenance Mode ............................................................................................................................. OFF  
  Timezone ........................................................................................................................... Europe/Berlin  
  Locale ........................................................................................................................................ de

❯ php -v
PHP 8.3.12 (cli) (built: Sep 24 2024 18:08:04) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.12, Copyright (c), by Zend Technologies

When I upload the project to our web server, I get an error:

user@webserver-02:~/httpdocs$ php artisan optimize

   INFO  Caching framework bootstrap, configuration, and metadata.  

  config .............................................................................................................................. 90.97ms DONE
  events ............................................................................................................................... 3.68ms DONE
  routes .............................................................................................................................. 94.27ms DONE
  views ................................................................................................................................ 1 Sek. FAIL

In ComponentTagCompiler.php line 311:

  Unable to locate a class or view for component [heroicon-o-eye-slash].  

user@webserver-02:~/httpdocs$ php artisan about --only=Environment

  Environment ......................................................................................................................................  
  Application Name ...........................................................................................................   
  Laravel Version .......................................................................................................................... 11.25.0  
  PHP Version ............................................................................................................................... 8.3.12  
  Composer Version ........................................................................................................................... 2.8.1  
  Environment ........................................................................................................................... production  
  Debug Mode ................................................................................................................................... OFF  
  URL ........................................................................................................................   
  Maintenance Mode ......................................................................................................................... ENABLED  
  Timezone ........................................................................................................................... Europe/Berlin  
  Locale ........................................................................................................................................ de 

user@webserver-02:~/httpdocs$ php -v
PHP 8.3.12 (cli) (built: Sep 26 2024 16:52:15) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.12, Copyright (c), by Zend Technologies

Here is the stack trace:

[2024-10-14 12:34:09] production.ERROR: Unable to locate a class or view for component [heroicon-o-eye-slash]. {"exception":"[object] (InvalidArgumentException(code: 0): Unable to locate a class or view for component [heroicon-o-eye-slash]. at /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php:311)
[stacktrace]
#0 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php(235): Illuminate\\View\\Compilers\\ComponentTagCompiler->componentClass()
#1 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php(220): Illuminate\\View\\Compilers\\ComponentTagCompiler->componentString()
#2 [internal function]: Illuminate\\View\\Compilers\\ComponentTagCompiler->Illuminate\\View\\Compilers\\{closure}()
#3 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php(215): preg_replace_callback()
#4 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php(89): Illuminate\\View\\Compilers\\ComponentTagCompiler->compileSelfClosingTags()
#5 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php(76): Illuminate\\View\\Compilers\\ComponentTagCompiler->compileTags()
#6 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php(438): Illuminate\\View\\Compilers\\ComponentTagCompiler->compile()
#7 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php(270): Illuminate\\View\\Compilers\\BladeCompiler->compileComponentTags()
#8 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php(184): Illuminate\\View\\Compilers\\BladeCompiler->compileString()
#9 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php(64): Illuminate\\View\\Compilers\\BladeCompiler->compile()
#10 [internal function]: Illuminate\\Foundation\\Console\\ViewCacheCommand->Illuminate\\Foundation\\Console\\{closure}()
#11 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Collections/Arr.php(605): array_map()
#12 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Collections/Collection.php(789): Illuminate\\Support\\Arr::map()
#13 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php(61): Illuminate\\Support\\Collection->map()
#14 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php(43): Illuminate\\Foundation\\Console\\ViewCacheCommand->compileViews()
#15 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(257): Illuminate\\Foundation\\Console\\ViewCacheCommand->Illuminate\\Foundation\\Console\\{closure}()
#16 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php(38): Illuminate\\Support\\Collection->each()
#17 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\ViewCacheCommand->handle()
#18 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#19 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure()
#20 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#21 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call()
#22 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\\Container\\Container->call()
#23 /var/www/vhosts/WEBSITE/httpdocs/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#24 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run()
#25 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(67): Illuminate\\Console\\Command->run()
#26 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(40): Illuminate\\Console\\Command->runCommand()
#27 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(38): Illuminate\\Console\\Command->callSilent()
#28 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php(40): Illuminate\\Foundation\\Console\\OptimizeCommand->Illuminate\\Foundation\\Console\\{closure}()
#29 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/View/Components/Factory.php(60): Illuminate\\Console\\View\\Components\\Task->render()
#30 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(39): Illuminate\\Console\\View\\Components\\Factory->__call()
#31 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(257): Illuminate\\Foundation\\Console\\OptimizeCommand->Illuminate\\Foundation\\Console\\{closure}()
#32 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php(39): Illuminate\\Support\\Collection->each()
#33 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Foundation\\Console\\OptimizeCommand->handle()
#34 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#35 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure()
#36 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#37 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): Illuminate\\Container\\BoundMethod::call()
#38 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\\Container\\Container->call()
#39 /var/www/vhosts/WEBSITE/httpdocs/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#40 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run()
#41 /var/www/vhosts/WEBSITE/httpdocs/vendor/symfony/console/Application.php(1047): Illuminate\\Console\\Command->run()
#42 /var/www/vhosts/WEBSITE/httpdocs/vendor/symfony/console/Application.php(316): Symfony\\Component\\Console\\Application->doRunCommand()
#43 /var/www/vhosts/WEBSITE/httpdocs/vendor/symfony/console/Application.php(167): Symfony\\Component\\Console\\Application->doRun()
#44 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): Symfony\\Component\\Console\\Application->run()
#45 /var/www/vhosts/WEBSITE/httpdocs/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1203): Illuminate\\Foundation\\Console\\Kernel->handle()
#46 /var/www/vhosts/WEBSITE/httpdocs/artisan(13): Illuminate\\Foundation\\Application->handleCommand()
#47 {main}
"}