Issue 233490
Summary [Clangd]: [modules] -> clangd reuses state dependent BMIs after rebuilding
Labels
Assignees
Reporter lslimani
    
Version: clangd 23.1.3 (Arch Linux package), `--experimental-modules-support`

After a module interface is edited, clangd rebuilds that module but keeps reusing its cached BMIs of the modules that import it. That covers the in-memory cache and the persistent cache in `.cache/clangd/modules`. Those BMIs were compiled against the old BMI of the dependency, so the TU sees inconsistent modules. Here that shows up as bogus diagnostics. In a larger project the same inconsistency crashes the ASTReader (see #228558). Because of the persistent cache, the state survives clangd restarts. It only goes away when the dependent's own sources change or the cache is deleted.

No build-system BMIs are involved: the compilation database has no `-fmodule-file=` flags.

Reproduce:

[repro.sh](https://github.com/user-attachments/files/33675985/repro.sh)

My Output:

```
clangd version 23.1.3
== 1st check, empty module cache:
I[19:28:19.724] Built module a to .cache/clangd/modules/a.cppm-AC946E8EA5D259C1/971C3D7B0263AC7E/a-20261019-192819-724941.pcm
I[19:28:19.729] Built module b to .cache/clangd/modules/b.cppm-A6702FF4FB39E865/025FCB8E7083D639/b-20261019-192819-729374.pcm
I[19:28:19.734] All checks completed, 0 errors
== 2nd check, after editing a.cppm:
I[19:28:20.758] Built module a to .cache/clangd/modules/a.cppm-AC946E8EA5D259C1/971C3D7B0263AC7E/a-20261019-192820-758432.pcm
I[19:28:20.761] Reusing persistent module b from .cache/clangd/modules/b.cppm-A6702FF4FB39E865/025FCB8E7083D639/b.pcm
E[19:28:20.764] [typecheck_nonviable_condition] Line 3: no viable conversion from 'n::A' to 'const Pad0'
I[19:28:20.766] All checks completed, 1 errors
== clang++ building both modules from the current sources, for comparison:
OK, no errors
```

The parameter of `n::twice(const A &)` from the stale `b.pcm` resolves to `Pad0` in the new `a.pcm`. The same happens within one clangd session through the in-memory cache ("Reusing module b from ...").

Analysis:

`IsModuleFileUpToDate()` in ModulesBuilder.cpp loads the cached BMI together with the newly built dependency and validates only the input files of each loaded module file. For standard C++ modules, the IMPORT record stores no size, timestamp or signature of the imported module file (ASTWriter writes zeros), so a BMI that was compiled against an older BMI of its dependency still passes. The cache key, made of module name, module unit source and compile command hash, does not involve the dependencies either.


expected:

Dependents should be rebuilt when a dependency is rebuilt. For example, clangd could include the signatures of the BMIs a module was built against in its cache key, or store them next to the BMI and compare them in `IsModuleFileUpToDate()`.

Related: #228558. A clang-side check for mismatched imports would also make `IsModuleFileUpToDate()` reject these files.


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to