*.module file not included, even after cache clear I added a *.module file to a custom module, to implement some hooks.

I found that the *.module was never included, even after cache clear (via devel menu "Cache clear". Even putting a "die()" statement at the top of the file did not crash anything.

Only after drush pmu + drush en, was the *.module file included in every request.

What am I missing? Is this a bug?
Here are my own observations. It contains some details that were not mentioned in @kiamlaluno's answer.

Things that need rebuilding

There are two "caches" that need to be rebuilt, if a *.module file with hook implementations was just created, in a module that previously did not have such a file.


The "container.modules" container parameter, which we can inspect with dpm(\Drupal::getContainer()-&gt;getParameter('container.modules'));.
Each entry has a ['filename' =&gt; *] key, which can be either null or 'MODULENAME.module'.
If it is null, then Drupal has not found the *.module file yet, and will not include it when looking for hook implementations.
The hook implementations, which we can inspect with dpm(\Drupal::moduleHandler()-&gt;getImplementations($hook_name));.


Operations that rebuild those things - or not

The first part, the container parameter, is not rebuilt on devel cache clear, or on drush cr. This has been the real problem.

The second part, the module_implements cache, is not rebuilt on devel cache clear, but it is rebuilt on drush cr.

To rebuild the first part, the container parameters, one can, as suggested by @kiamlaluno, uninstall and re-enable the module with the new hooks. But this can be quite impractical, because uninstalling can cause data loss.

Solution:

Luckily there is an easier solution: Enable any other module that was previously not installed, and then uninstall it again.

E.g. for me it was "statistics" module. But can be any other.

drush pm-list | grep "Not installed"
drush en statistics
drush pmu statistics


EDIT: NOT SO FAST!

I have to correct myself, for now:


The above method of enabling and uninstalling any other module does fix the container parameter 'filename' setting, at first.
However, flushing the cache again afterwards removes the 'filename' setting again.


This means that this setting is stored in a more persistent place somewhere.

The reason: APCu file cache: cli vs web

ExtensionDiscovery uses a cache to store discovered extension files.
In my case, this is Drupal\Component\FileCache\FileCache, with a Drupal\Component\FileCache\ApcuFileCacheBackend apcu backend.

The problem: The APCu cache has isolated realms for cli and for web.
This means, if the file cache is cleared on cli, e.g. via drush, the entries in web are still there.

This problem seems to apply only to the file cache in ExtensionDiscovery. Other caches are not affected. Probably this is because this file cache sits on a lower level within Drupal bootstrap.

Another part of the problem, possibly, is that the file cache is never cleared completely. At least, I cannot find such a place. Instead, individual entries are deleted by key.