How do I hide a local task (tab) if a module is disabled?
I believe my problem was that if the module was disabled then the route would not exist - and this would have my links.task.yml trying to create a local task from a non existent route.

My solution was to generate a dynamic local task instead, so that the local task is only created, if the module is enabled:

mymodule.links.task.yml

mymodule.bravo:
  deriver: 'Drupal\mymodule\Plugin\Derivative\BravoLocalTasks'


src/Plugin/Derivative/BravoLocalTasks.php

namespace Drupal\mymodule\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;

/**
 * Generates local tasks.
 */
class BravoLocalTasks extends DeriverBase {

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler-&gt;moduleExists('statistics')) {
      $this-&gt;derivatives['mymodule.bravo'] = $base_plugin_definition;
      $this-&gt;derivatives['mymodule.bravo']['route_name'] = 'mymodule.bravo';
      $this-&gt;derivatives['mymodule.bravo']['base_route'] = 'mymodule.alpha';
      $this-&gt;derivatives['mymodule.bravo']['title'] = 'Bravo';
    }
    return $this-&gt;derivatives;
  }
}


I believe I will have to create a distinct class for every route that requires dynamic local tasks.

If you have a better solution, please post.