blade-heroicons icon indicating copy to clipboard operation
blade-heroicons copied to clipboard

Cannot run Optimize command with Laravel 11

Open dwightwatson opened this issue 3 months ago • 6 comments

  • Library Version: 2.3.0
  • Laravel Version: 11.0.5
  • PHP Version: 8.3.3

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

dwightwatson avatar Mar 13 '24 22:03 dwightwatson

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.

driesvints avatar Mar 15 '24 21:03 driesvints

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 avatar Mar 17 '24 22:03 dwightwatson

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?

dwightwatson avatar Mar 17 '24 23:03 dwightwatson

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

abishekrsrikaanth avatar Apr 11 '24 09:04 abishekrsrikaanth

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.

dwightwatson avatar Apr 11 '24 09:04 dwightwatson

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

abishekrsrikaanth avatar Apr 11 '24 16:04 abishekrsrikaanth

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

mihob avatar May 06 '24 16:05 mihob

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.

driesvints avatar May 07 '24 07:05 driesvints