I recently upgraded our Drupal install from 8.3.7 => 8.4.5. A major issue I am having is that any Twig templates with an attach_library call throws the below error:
LogicException: Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead. in /vagrant/drupal-8.4.5/core/lib/Drupal/Core/Render/Renderer.php:241 [error]
Stack trace:
#0 /vagrant/drupal-8.4.5/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false)
#1 /vagrant/drupal-8.4.5/core/lib/Drupal/Core/Template/TwigExtension.php(380): Drupal\Core\Render\Renderer->render(Array)
#2 /vagrant/drupal-8.4.5/sites/default/files/php/twig/5a9f160ada026_file-link.html.twig__xP4ZKUjMG1Br_4AamdWV9Fbb/MO6fCVev6wq08Go5uaIPMEZ5_r-uQk-YHGauA3DKccg.php(43): Drupal\Core\Template\TwigExtension->attachLibrary('classy/file')
This error is produced when running 'drush -d twigc'. The biggest issue with this is that this stops once it gets to this error. So when I make a change to a template and clear cache, that twig template renders empty on the page and I can never get it back again since the twig templates cannot be recompiled successfully. I have tested this with Drush 8 and Drush 9. Of course the 'twigc' command is only in Drush 9, I can still replicate this issue using Drush 8 when rebuilding cache. This errors is being thrown in core template files as well, not just installed modules or any custom stuff.