https://github.com/atharva-potdar updated 
https://github.com/llvm/llvm-project/pull/242615

>From 776012c6210faae949644c2cfec3fb9fb700316f Mon Sep 17 00:00:00 2001
From: atharva-potdar <[email protected]>
Date: Sat, 14 Nov 2026 10:21:40 +0530
Subject: [PATCH 1/3] [clangd] Resolve out-of-tree module units via
 ProjectModules

When building C++23/26 projects that use `import std;` with CMake and Ninja,
compile_commands.json includes an entry for the standard library module unit
outside the project tree (e.g., /usr/share/libc++/v1/std.cppm).

When clangd opens a translation unit in the workspace,
GlobalCompilationDatabase::getProjectModules() finds the project's CDB by
walking parent directories and creates a ProjectModules instance using it.
ProjectModules::getSourceForModuleName() resolves `std` to
/usr/share/libc++/v1/std.cppm, but ModulesBuilder then queries
GlobalCompilationDatabase::getCompileCommand() and getProjectInfo() using
that module unit path.

Without --compile-commands-dir, DirectoryBasedGlobalCompilationDatabase walks
the ancestors of /usr/share/libc++/v1/std.cppm, finds no CDB, and fails with
"No compile command for /usr/share/libc++/v1/std.cppm". Similarly,
getProjectInfo() on the system path returns std::nullopt instead of the
consuming project's root.

Fall back to the ProjectModules CDB and the consuming file's project root:

  - Add ProjectModules::getCompileCommand(PathRef) to query the project's
    underlying CDB (applying CommandMangler if set).
  - In ModulesBuilder, fall back to MDB.getCompileCommand() when
    getCDB().getCompileCommand() returns std::nullopt.
  - Fall back to CDB.getProjectInfo(RequiredSource) when resolving the
    module cache root and GC target for out-of-tree module units.
  - Guard Mangler in OverlayCDB::getProjectModules() with `if (Mangler)` to
    avoid calling an empty llvm::unique_function.
---
 .../clangd/GlobalCompilationDatabase.cpp      |  11 +-
 clang-tools-extra/clangd/ModulesBuilder.cpp   |  71 ++++--
 clang-tools-extra/clangd/ProjectModules.cpp   |  27 +++
 clang-tools-extra/clangd/ProjectModules.h     |   8 +
 .../GlobalCompilationDatabaseTests.cpp        |  18 ++
 .../unittests/PrerequisiteModulesTest.cpp     | 223 ++++++++++++++++++
 6 files changed, 335 insertions(+), 23 deletions(-)

diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp 
b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index b7bb7f426d6150..553236c7d33603 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -856,10 +856,13 @@ OverlayCDB::getProjectModules(PathRef File) const {
     log("Failed to get compilation Database for {0}", File);
     return {};
   }
-  MDB->setCommandMangler([&Mangler = Mangler](tooling::CompileCommand &Command,
-                                              PathRef CommandPath) {
-    Mangler(Command, CommandPath);
-  });
+  if (Mangler) {
+    MDB->setCommandMangler(
+        [&Mangler = Mangler](tooling::CompileCommand &Command,
+                             PathRef CommandPath) {
+          Mangler(Command, CommandPath);
+        });
+  }
   return MDB;
 }
 
diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp 
b/clang-tools-extra/clangd/ModulesBuilder.cpp
index 72bb5b836d2012..f75f34f6eb1dac 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -94,17 +94,33 @@ std::string normalizePathForCache(PathRef Path) {
   return maybeCaseFoldPath(Normalized);
 }
 
+/// Returns the project source root owning \p ModuleUnitFileName, falling back
+/// to the consuming translation unit's (\p RequiredSource) project root when
+/// the module unit resides outside the workspace tree (e.g. system 
`std.cppm`).
+std::optional<std::string>
+getModuleProjectRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
+                     const GlobalCompilationDatabase &CDB) {
+  if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
+      PI && !PI->SourceRoot.empty())
+    return PI->SourceRoot;
+  if (auto PI = CDB.getProjectInfo(RequiredSource);
+      PI && !PI->SourceRoot.empty())
+    return PI->SourceRoot;
+  return std::nullopt;
+}
+
 /// Returns the root directory used for persistent module cache storage.
-/// Prefer a project-local cache so different clangd sessions working on the
+/// Prefer a project-local cache (resolved from the module unit or the
+/// consuming translation unit) so different clangd sessions working on the
 /// same source tree can reuse BMIs. Fall back to the user cache directory, and
 /// finally to a non-ephemeral temp directory when no better cache root exists.
 llvm::SmallString<256>
-getModuleCacheRoot(PathRef ModuleUnitFileName,
+getModuleCacheRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
                    const GlobalCompilationDatabase &CDB) {
   llvm::SmallString<256> Result;
-  if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
-      PI && !PI->SourceRoot.empty()) {
-    Result = PI->SourceRoot;
+  if (auto ProjectRoot =
+          getModuleProjectRoot(RequiredSource, ModuleUnitFileName, CDB)) {
+    Result = *ProjectRoot;
     llvm::sys::path::append(Result, ".cache", "clangd", "modules");
     return Result;
   }
@@ -123,9 +139,10 @@ getModuleCacheRoot(PathRef ModuleUnitFileName,
 /// module cache. Placing locks beside the cache ensures all builders sharing
 /// the cache also synchronize through the same lock namespace.
 llvm::SmallString<256>
-getModuleCacheLocksDirectory(PathRef ModuleUnitFileName,
+getModuleCacheLocksDirectory(PathRef RequiredSource, PathRef 
ModuleUnitFileName,
                              const GlobalCompilationDatabase &CDB) {
-  llvm::SmallString<256> Result = getModuleCacheRoot(ModuleUnitFileName, CDB);
+  llvm::SmallString<256> Result =
+      getModuleCacheRoot(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result, ".locks");
   return Result;
 }
@@ -162,10 +179,11 @@ std::string getCompileCommandStringHash(const 
tooling::CompileCommand &Cmd) {
 /// basename alongside the source-hash, and the command-hash keeps incompatible
 /// command lines apart.
 llvm::SmallString<256>
-getModuleFilesDirectory(PathRef ModuleUnitFileName,
+getModuleFilesDirectory(PathRef RequiredSource, PathRef ModuleUnitFileName,
                         const tooling::CompileCommand &Cmd,
                         const GlobalCompilationDatabase &CDB) {
-  llvm::SmallString<256> Result = getModuleCacheRoot(ModuleUnitFileName, CDB);
+  llvm::SmallString<256> Result =
+      getModuleCacheRoot(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result,
                           getModuleUnitSourceDirectoryName(ModuleUnitFileName),
                           getCompileCommandStringHash(Cmd));
@@ -175,10 +193,10 @@ getModuleFilesDirectory(PathRef ModuleUnitFileName,
 /// Returns the lock file path guarding publication of BMIs for a module unit
 /// source. Builders targeting the same source-hash serialize through this 
path.
 llvm::SmallString<256>
-getModuleSourceHashLockPath(PathRef ModuleUnitFileName,
+getModuleSourceHashLockPath(PathRef RequiredSource, PathRef ModuleUnitFileName,
                             const GlobalCompilationDatabase &CDB) {
   llvm::SmallString<256> Result =
-      getModuleCacheLocksDirectory(ModuleUnitFileName, CDB);
+      getModuleCacheLocksDirectory(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result,
                           getModuleUnitSourcePathHash(ModuleUnitFileName));
   return Result;
@@ -269,10 +287,11 @@ llvm::Error ensureLockAnchorFileExists(PathRef LockPath) {
 class ScopedModuleSourceLock {
 public:
   static llvm::Expected<ScopedModuleSourceLock>
-  acquire(PathRef ModuleUnitFileName, const GlobalCompilationDatabase &CDB) {
+  acquire(PathRef RequiredSource, PathRef ModuleUnitFileName,
+          const GlobalCompilationDatabase &CDB) {
     constexpr auto LockWaitInterval = std::chrono::seconds(10);
     llvm::SmallString<256> LockPath =
-        getModuleSourceHashLockPath(ModuleUnitFileName, CDB);
+        getModuleSourceHashLockPath(RequiredSource, ModuleUnitFileName, CDB);
     if (llvm::Error Err = ensureLockAnchorFileExists(LockPath))
       return std::move(Err);
 
@@ -966,6 +985,11 @@ class CachingProjectModules : public ProjectModules {
     return MDB->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return MDB->getCompileCommand(File);
+  }
+
   std::string getSourceForModuleName(llvm::StringRef ModuleName,
                                      PathRef RequiredSrcFile) override {
     auto ModuleState = MDB->getModuleNameState(ModuleName);
@@ -1152,6 +1176,7 @@ class ModulesBuilder::ModulesBuilderImpl {
   /// Try to get prebuilt module files from the compilation database.
   void getPrebuiltModuleFile(StringRef ModuleName, PathRef ModuleUnitFileName,
                              const ThreadsafeFS &TFS,
+                             const CachingProjectModules &MDB,
                              ReusablePrerequisiteModules &BuiltModuleFiles);
 
   /// Runs GC once for the cache root owning a project root.
@@ -1190,11 +1215,14 @@ void ModulesBuilder::ModulesBuilderImpl::
 
 void ModulesBuilder::ModulesBuilderImpl::getPrebuiltModuleFile(
     StringRef ModuleName, PathRef ModuleUnitFileName, const ThreadsafeFS &TFS,
+    const CachingProjectModules &MDB,
     ReusablePrerequisiteModules &BuiltModuleFiles) {
   if (!ReusePrebuiltModuleFile)
     return;
 
   auto Cmd = getCDB().getCompileCommand(ModuleUnitFileName);
+  if (!Cmd)
+    Cmd = MDB.getCompileCommand(ModuleUnitFileName);
   if (!Cmd)
     return;
 
@@ -1257,7 +1285,8 @@ llvm::Error 
ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
   /// Try to get prebuilt module files from the compilation database first. 
This
   /// helps to avoid building the module files that are already built by the
   /// compiler.
-  getPrebuiltModuleFile(ModuleName, ModuleUnitFileName, TFS, BuiltModuleFiles);
+  getPrebuiltModuleFile(ModuleName, ModuleUnitFileName, TFS, MDB,
+                        BuiltModuleFiles);
 
   // Get Required modules in topological order.
   auto ReqModuleNames = getAllRequiredModules(RequiredSource, MDB, ModuleName);
@@ -1268,20 +1297,24 @@ llvm::Error 
ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
     std::string ReqFileName =
         MDB.getSourceForModuleName(ReqModuleName, RequiredSource);
     auto Cmd = getCDB().getCompileCommand(ReqFileName);
+    if (!Cmd)
+      Cmd = MDB.getCompileCommand(ReqFileName);
     if (!Cmd)
       return llvm::createStringError(
           llvm::formatv("No compile command for {0}", ReqFileName));
-    if (auto PI = getCDB().getProjectInfo(ReqFileName);
-        PI && !PI->SourceRoot.empty())
-      garbageCollectModuleCacheForProjectRoot(PI->SourceRoot);
+    if (auto ProjectRoot =
+            getModuleProjectRoot(RequiredSource, ReqFileName, getCDB()))
+      garbageCollectModuleCacheForProjectRoot(*ProjectRoot);
 
     const std::string CommandHash = getCompileCommandStringHash(*Cmd);
     const std::string PublishedModuleFilePath = getPublishedModuleFilePath(
-        ReqModuleName, getModuleFilesDirectory(ReqFileName, *Cmd, getCDB()));
+        ReqModuleName,
+        getModuleFilesDirectory(RequiredSource, ReqFileName, *Cmd, getCDB()));
 
     // Keep the source-scoped lock while probing and validating cached BMIs so
     // stale-file replacement and final publication stay serialized.
-    auto SourceLock = ScopedModuleSourceLock::acquire(ReqFileName, getCDB());
+    auto SourceLock =
+        ScopedModuleSourceLock::acquire(RequiredSource, ReqFileName, getCDB());
     if (!SourceLock)
       return SourceLock.takeError();
 
diff --git a/clang-tools-extra/clangd/ProjectModules.cpp 
b/clang-tools-extra/clangd/ProjectModules.cpp
index 4ea38f4177fe9b..4b57d220bc7a6c 100644
--- a/clang-tools-extra/clangd/ProjectModules.cpp
+++ b/clang-tools-extra/clangd/ProjectModules.cpp
@@ -196,6 +196,14 @@ class ModuleDependencyScanner {
   getRequiredModules(PathRef File,
                      const ProjectModules::CommandMangler &Mangler);
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef FilePath,
+                    const ProjectModules::CommandMangler &Mangler) const {
+    if (!CDB)
+      return std::nullopt;
+    return getCompileCommandForFile(*CDB, FilePath, Mangler);
+  }
+
 private:
   std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
 
@@ -344,6 +352,11 @@ class ScanningAllProjectModules : public ProjectModules {
     return ModuleNameState::Unknown;
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return Scanner.getCompileCommand(File, Mangler);
+  }
+
 private:
   ModuleDependencyScanner Scanner;
   CommandMangler Mangler;
@@ -429,6 +442,13 @@ class CompileCommandsProjectModules : public 
ProjectModules {
     return SourceIt->second;
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    if (!CDB)
+      return std::nullopt;
+    return getCompileCommandForFile(*CDB, File, Mangler);
+  }
+
   void setCommandMangler(CommandMangler Mangler) override {
     this->Mangler = std::move(Mangler);
     ProducerCommandsIndexed = false;
@@ -567,6 +587,13 @@ class CompoundProjectModules : public ProjectModules {
     return Scanning->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    if (auto Cmd = CompileCommands->getCompileCommand(File))
+      return Cmd;
+    return Scanning->getCompileCommand(File);
+  }
+
   void setCommandMangler(CommandMangler Mangler) override {
     this->Mangler = std::move(Mangler);
     auto ForwardMangler = [this](tooling::CompileCommand &Command,
diff --git a/clang-tools-extra/clangd/ProjectModules.h 
b/clang-tools-extra/clangd/ProjectModules.h
index fbf658bafe1500..76e43f6bac7fc5 100644
--- a/clang-tools-extra/clangd/ProjectModules.h
+++ b/clang-tools-extra/clangd/ProjectModules.h
@@ -15,6 +15,7 @@
 #include "clang/Tooling/CompilationDatabase.h"
 
 #include <memory>
+#include <optional>
 
 namespace clang {
 namespace clangd {
@@ -55,6 +56,13 @@ class ProjectModules {
     return ModuleNameState::Unknown;
   }
 
+  /// Returns a compile command for \p File from this project's underlying
+  /// compilation database, with any configured CommandMangler applied.
+  virtual std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const {
+    return std::nullopt;
+  }
+
   virtual void setCommandMangler(CommandMangler Mangler) {}
 
   virtual ~ProjectModules() = default;
diff --git 
a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp 
b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
index 39ab1446e980b1..c380582f994f5c 100644
--- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
+++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
@@ -14,6 +14,7 @@
 #include "support/Path.h"
 #include "support/ThreadsafeFS.h"
 #include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -92,6 +93,15 @@ class OverlayCDBTest : public ::testing::Test {
     std::optional<ProjectInfo> getProjectInfo(PathRef File) const override {
       return ProjectInfo{testRoot()};
     }
+
+    std::unique_ptr<ProjectModules>
+    getProjectModules(PathRef File) const override {
+      MockFS TFS;
+      std::string Error;
+      auto JSONCDB = tooling::JSONCompilationDatabase::loadFromBuffer(
+          testRoot(), "[]", Error, tooling::JSONCommandLineSyntax::AutoDetect);
+      return clang::clangd::getProjectModules(std::move(JSONCDB), TFS);
+    }
   };
 
 protected:
@@ -179,6 +189,14 @@ TEST_F(OverlayCDBTest, Adjustments) {
                                            "-DFallback", "-DAdjust_baz.cc"));
 }
 
+TEST_F(OverlayCDBTest, GetProjectModulesWithoutMangler) {
+  OverlayCDB CDB(Base.get());
+  auto MDB = CDB.getProjectModules(testPath("foo.cc"));
+  ASSERT_NE(MDB, nullptr);
+  // Invoking ProjectModules queries must not crash when Mangler is empty.
+  EXPECT_EQ(MDB->getCompileCommand(testPath("foo.cc")), std::nullopt);
+}
+
 TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
   SmallString<1024> Path;
   int FD;
diff --git a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp 
b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
index e878e2e0fa637b..8472821cf13412 100644
--- a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
+++ b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
@@ -14,6 +14,7 @@
 #include "Annotations.h"
 #include "CodeComplete.h"
 #include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
 #include "ModulesBuilder.h"
 #include "Preamble.h"
 #include "ProjectModules.h"
@@ -68,6 +69,11 @@ class GlobalScanningCounterProjectModules : public 
ProjectModules {
     return Underlying->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return Underlying->getCompileCommand(File);
+  }
+
 private:
   std::unique_ptr<ProjectModules> Underlying;
   std::atomic<unsigned> &Count;
@@ -1649,6 +1655,223 @@ export int BValue = MValue;
   EXPECT_TRUE(StringRef(HSB.PrebuiltModuleFiles["M"]).contains("M.cppm-"));
 }
 
+TEST_F(PrerequisiteModulesTests,
+       OutOfTreeModuleUnitResolvedViaConsumerCDBAndCachedInProject) {
+  SmallString<256> ProjectDir(TestDir);
+  llvm::sys::path::append(ProjectDir, "project");
+  SmallString<256> BuildDir(ProjectDir);
+  llvm::sys::path::append(BuildDir, "build");
+  SmallString<256> ExternalDir(TestDir);
+  llvm::sys::path::append(ExternalDir, "external");
+
+  ASSERT_FALSE(llvm::sys::fs::create_directories(BuildDir));
+  ASSERT_FALSE(llvm::sys::fs::create_directories(ExternalDir));
+
+  SmallString<256> ExtModPath(ExternalDir);
+  llvm::sys::path::append(ExtModPath, "M.cppm");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(ExtModPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "export module M;\nexport int extVal = 42;\n";
+  }
+
+  SmallString<256> MainPath(ProjectDir);
+  llvm::sys::path::append(MainPath, "main.cpp");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(MainPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "import M;\nint main() { return extVal; }\n";
+  }
+
+  SmallString<256> MPcmPath(BuildDir);
+  llvm::sys::path::append(MPcmPath, "M.pcm");
+
+  SmallString<256> CDBPath(BuildDir);
+  llvm::sys::path::append(CDBPath, "compile_commands.json");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(CDBPath, EC);
+    ASSERT_FALSE(EC);
+    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+    std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath);
+    std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath);
+    std::string SlashPcm = llvm::sys::path::convert_to_slash(MPcmPath);
+    OS << llvm::formatv(
+        R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile -o {3} {1}",
+    "file": "{1}"
+  },
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 -fmodule-file=M={3} -c {2}",
+    "file": "{2}"
+  }
+])json",
+        SlashBuild, SlashExtMod, SlashMain, SlashPcm);
+  }
+
+  DirectoryBasedGlobalCompilationDatabase DirCDB(FS);
+  OverlayCDB CDB(&DirCDB);
+
+  // Out-of-tree module unit has no ancestor CDB of its own.
+  EXPECT_FALSE(CDB.getCompileCommand(ExtModPath));
+
+  ModulesBuilder Builder(CDB);
+  auto Info = Builder.buildPrerequisiteModulesFor(MainPath, FS);
+  ASSERT_TRUE(Info);
+
+  HeaderSearchOptions HS(ProjectDir);
+  Info->adjustHeaderSearchOptions(HS);
+  ASSERT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
+  std::string BuiltPCMPath = HS.PrebuiltModuleFiles["M"];
+  EXPECT_TRUE(llvm::sys::fs::exists(BuiltPCMPath));
+
+  // Because the out-of-tree module unit has no project root, its persistent 
BMI
+  // cache must fall back to the consuming translation unit's project root.
+  SmallString<256> ExpectedCacheRoot(ProjectDir);
+  llvm::sys::path::append(ExpectedCacheRoot, ".cache", "clangd", "modules");
+  EXPECT_TRUE(StringRef(BuiltPCMPath).starts_with(ExpectedCacheRoot))
+      << "Actual BMI path: " << BuiltPCMPath
+      << "\nExpected prefix: " << ExpectedCacheRoot;
+
+  ParseInputs MainInputs = getInputs("project/main.cpp", CDB);
+  MainInputs.ModulesManager = &Builder;
+  auto CI = buildCompilerInvocation(MainInputs, DiagConsumer);
+  ASSERT_TRUE(CI);
+  EXPECT_TRUE(Info->canReuse(*CI, FS.view(ProjectDir)));
+
+  // Also verify getPrebuiltModuleFile() when an out-of-tree module unit
+  // itself consumes a prebuilt BMI via -fmodule-file= in 
compile_commands.json.
+  SmallString<256> DepModPath(ExternalDir);
+  llvm::sys::path::append(DepModPath, "Dep.cppm");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(DepModPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "export module Dep;\nexport int depVal = 7;\n";
+  }
+
+  SmallString<256> DepPcmPath(BuildDir);
+  llvm::sys::path::append(DepPcmPath, "Dep.pcm");
+
+  // Build Dep.pcm once so a valid prebuilt BMI exists on disk.
+  {
+    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+    std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(CDBPath, EC);
+    ASSERT_FALSE(EC);
+    OS << llvm::formatv(
+        R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile {1}",
+    "file": "{1}"
+  }
+])json",
+        SlashBuild, SlashDepMod);
+  }
+  {
+    DirectoryBasedGlobalCompilationDatabase SeedDirCDB(FS);
+    OverlayCDB SeedCDB(&SeedDirCDB);
+    ModulesBuilder SeedBuilder(SeedCDB);
+    // Build a temporary consumer of Dep to produce a valid BMI on disk.
+    SmallString<256> SeedMainPath(ProjectDir);
+    llvm::sys::path::append(SeedMainPath, "seed.cpp");
+    {
+      std::error_code EC;
+      llvm::raw_fd_ostream OS(SeedMainPath, EC);
+      ASSERT_FALSE(EC);
+      OS << "import Dep;\n";
+    }
+    {
+      std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+      std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
+      std::string SlashSeed = llvm::sys::path::convert_to_slash(SeedMainPath);
+      std::error_code EC;
+      llvm::raw_fd_ostream OS(CDBPath, EC);
+      ASSERT_FALSE(EC);
+      OS << llvm::formatv(
+          R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile {1}",
+    "file": "{1}"
+  },
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 -c {2}",
+    "file": "{2}"
+  }
+])json",
+          SlashBuild, SlashDepMod, SlashSeed);
+    }
+    auto SeedInfo = SeedBuilder.buildPrerequisiteModulesFor(SeedMainPath, FS);
+    ASSERT_TRUE(SeedInfo);
+    HeaderSearchOptions SeedHS(ProjectDir);
+    SeedInfo->adjustHeaderSearchOptions(SeedHS);
+    ASSERT_EQ(SeedHS.PrebuiltModuleFiles.count("Dep"), 1u);
+    ASSERT_FALSE(llvm::sys::fs::copy_file(SeedHS.PrebuiltModuleFiles["Dep"],
+                                          DepPcmPath));
+  }
+
+  // Now update external/M.cppm to import Dep and pass -fmodule-file=Dep=... on
+  // M.cppm's compile command in compile_commands.json.
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(ExtModPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "export module M;\nimport Dep;\nexport int extVal = depVal + 35;\n";
+  }
+  {
+    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+    std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
+    std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath);
+    std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath);
+    std::string SlashDepPcm = llvm::sys::path::convert_to_slash(DepPcmPath);
+    std::string SlashMPcm = llvm::sys::path::convert_to_slash(MPcmPath);
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(CDBPath, EC);
+    ASSERT_FALSE(EC);
+    OS << llvm::formatv(
+        R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile -o {4} {1}",
+    "file": "{1}"
+  },
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 -fmodule-file=Dep={4} --precompile -o {5} 
{2}",
+    "file": "{2}"
+  },
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 -fmodule-file=M={5} -c {3}",
+    "file": "{3}"
+  }
+])json",
+        SlashBuild, SlashDepMod, SlashExtMod, SlashMain, SlashDepPcm,
+        SlashMPcm);
+  }
+
+  DirectoryBasedGlobalCompilationDatabase DirCDB2(FS);
+  OverlayCDB CDB2(&DirCDB2);
+  ModulesBuilder BuilderWithPrebuilt(CDB2);
+  auto PrebuiltInfo =
+      BuilderWithPrebuilt.buildPrerequisiteModulesFor(MainPath, FS);
+  ASSERT_TRUE(PrebuiltInfo);
+
+  HeaderSearchOptions PrebuiltHS(ProjectDir);
+  PrebuiltInfo->adjustHeaderSearchOptions(PrebuiltHS);
+  ASSERT_EQ(PrebuiltHS.PrebuiltModuleFiles.count("Dep"), 1u);
+  EXPECT_EQ(PrebuiltHS.PrebuiltModuleFiles["Dep"], std::string(DepPcmPath));
+}
+
 TEST_F(PrerequisiteModulesTests, ModuleImportThroughInclude) {
   MockDirectoryCompilationDatabase CDB(TestDir, FS);
 

>From 7a38852f6a5fec0c901a9061027af56209ff37b0 Mon Sep 17 00:00:00 2001
From: atharva-potdar <[email protected]>
Date: Sat, 14 Nov 2026 15:20:53 +0530
Subject: [PATCH 2/3] [clangd] Prefer project CDB over heuristic commands for
 module units

The fallback to the ProjectModules CDB only applied when the global
CDB returned no compile command. If an unrelated compile_commands.json
exists above an out-of-tree module unit, the global CDB returns an
interpolated (heuristic) command instead, which shadowed the project's
exact entry.

Add getModuleUnitCompileCommand(), which uses the global CDB's command
only when it is exact, and otherwise prefers the project CDB's.

Also split the out-of-tree module unit test into smaller tests, using
a shared helper for the project layout, and add a test for the
heuristic-command case.
---
 clang-tools-extra/clangd/ModulesBuilder.cpp   |  24 +-
 .../unittests/PrerequisiteModulesTest.cpp     | 291 ++++++------------
 2 files changed, 113 insertions(+), 202 deletions(-)

diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp 
b/clang-tools-extra/clangd/ModulesBuilder.cpp
index f75f34f6eb1dac..5fb6f720b5160a 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -109,6 +109,22 @@ getModuleProjectRoot(PathRef RequiredSource, PathRef 
ModuleUnitFileName,
   return std::nullopt;
 }
 
+/// Returns the compile command for a module unit. The global CDB wins only
+/// when it has an exact entry. If it has none, or only a heuristic guess
+/// (e.g. an unrelated ancestor compile_commands.json), prefer the consuming
+/// project's CDB, then fall back to whatever the global CDB gave us.
+std::optional<tooling::CompileCommand>
+getModuleUnitCompileCommand(PathRef ModuleUnitFileName,
+                            const GlobalCompilationDatabase &CDB,
+                            const ProjectModules &MDB) {
+  auto Cmd = CDB.getCompileCommand(ModuleUnitFileName);
+  if (Cmd && Cmd->Heuristic.empty())
+    return Cmd;
+  if (auto ProjectCmd = MDB.getCompileCommand(ModuleUnitFileName))
+    return ProjectCmd;
+  return Cmd;
+}
+
 /// Returns the root directory used for persistent module cache storage.
 /// Prefer a project-local cache (resolved from the module unit or the
 /// consuming translation unit) so different clangd sessions working on the
@@ -1220,9 +1236,7 @@ void 
ModulesBuilder::ModulesBuilderImpl::getPrebuiltModuleFile(
   if (!ReusePrebuiltModuleFile)
     return;
 
-  auto Cmd = getCDB().getCompileCommand(ModuleUnitFileName);
-  if (!Cmd)
-    Cmd = MDB.getCompileCommand(ModuleUnitFileName);
+  auto Cmd = getModuleUnitCompileCommand(ModuleUnitFileName, getCDB(), MDB);
   if (!Cmd)
     return;
 
@@ -1296,9 +1310,7 @@ llvm::Error 
ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
 
     std::string ReqFileName =
         MDB.getSourceForModuleName(ReqModuleName, RequiredSource);
-    auto Cmd = getCDB().getCompileCommand(ReqFileName);
-    if (!Cmd)
-      Cmd = MDB.getCompileCommand(ReqFileName);
+    auto Cmd = getModuleUnitCompileCommand(ReqFileName, getCDB(), MDB);
     if (!Cmd)
       return llvm::createStringError(
           llvm::formatv("No compile command for {0}", ReqFileName));
diff --git a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp 
b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
index 8472821cf13412..ba8f0be3669cc7 100644
--- a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
+++ b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
@@ -1655,221 +1655,120 @@ export int BValue = MValue;
   EXPECT_TRUE(StringRef(HSB.PrebuiltModuleFiles["M"]).contains("M.cppm-"));
 }
 
-TEST_F(PrerequisiteModulesTests,
-       OutOfTreeModuleUnitResolvedViaConsumerCDBAndCachedInProject) {
-  SmallString<256> ProjectDir(TestDir);
-  llvm::sys::path::append(ProjectDir, "project");
-  SmallString<256> BuildDir(ProjectDir);
-  llvm::sys::path::append(BuildDir, "build");
-  SmallString<256> ExternalDir(TestDir);
-  llvm::sys::path::append(ExternalDir, "external");
-
-  ASSERT_FALSE(llvm::sys::fs::create_directories(BuildDir));
-  ASSERT_FALSE(llvm::sys::fs::create_directories(ExternalDir));
-
-  SmallString<256> ExtModPath(ExternalDir);
-  llvm::sys::path::append(ExtModPath, "M.cppm");
-  {
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(ExtModPath, EC);
-    ASSERT_FALSE(EC);
-    OS << "export module M;\nexport int extVal = 42;\n";
-  }
+namespace {
 
-  SmallString<256> MainPath(ProjectDir);
-  llvm::sys::path::append(MainPath, "main.cpp");
-  {
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(MainPath, EC);
-    ASSERT_FALSE(EC);
-    OS << "import M;\nint main() { return extVal; }\n";
-  }
+std::string slash(llvm::StringRef P) {
+  return llvm::sys::path::convert_to_slash(P);
+}
 
-  SmallString<256> MPcmPath(BuildDir);
-  llvm::sys::path::append(MPcmPath, "M.pcm");
+void writeFile(llvm::StringRef Path, llvm::StringRef Contents) {
+  ASSERT_FALSE(
+      llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Path)));
+  std::error_code EC;
+  llvm::raw_fd_ostream OS(Path, EC);
+  ASSERT_FALSE(EC);
+  OS << Contents;
+}
 
-  SmallString<256> CDBPath(BuildDir);
-  llvm::sys::path::append(CDBPath, "compile_commands.json");
-  {
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(CDBPath, EC);
-    ASSERT_FALSE(EC);
-    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
-    std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath);
-    std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath);
-    std::string SlashPcm = llvm::sys::path::convert_to_slash(MPcmPath);
-    OS << llvm::formatv(
-        R"json([
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 --precompile -o {3} {1}",
-    "file": "{1}"
-  },
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 -fmodule-file=M={3} -c {2}",
-    "file": "{2}"
-  }
-])json",
-        SlashBuild, SlashExtMod, SlashMain, SlashPcm);
+std::string cdbEntry(llvm::StringRef Dir, llvm::StringRef Command,
+                     llvm::StringRef File) {
+  return "{\"directory\":\"" + slash(Dir) + "\",\"command\":\"" +
+         Command.str() + "\",\"file\":\"" + slash(File) + "\"}";
+}
+
+// <Root>/project/{main.cpp,build/compile_commands.json}
+// <Root>/external/M.cppm   (not covered by any ancestor CDB)
+struct OutOfTreeProject {
+  llvm::SmallString<256> Project, Build, ExtMod, Main;
+
+  explicit OutOfTreeProject(llvm::StringRef Root) {
+    Project = Root;
+    llvm::sys::path::append(Project, "project");
+    Build = Project;
+    llvm::sys::path::append(Build, "build");
+    Main = Project;
+    llvm::sys::path::append(Main, "main.cpp");
+    ExtMod = Root;
+    llvm::sys::path::append(ExtMod, "external", "M.cppm");
+
+    writeFile(ExtMod, "export module M;\nexport int extVal = 42;\n");
+    writeFile(Main, "import M;\nint main() { return extVal; }\n");
+
+    llvm::SmallString<256> Pcm(Build);
+    llvm::sys::path::append(Pcm, "M.pcm");
+    llvm::SmallString<256> CDB(Build);
+    llvm::sys::path::append(CDB, "compile_commands.json");
+    writeFile(CDB, "[" +
+                       cdbEntry(Build,
+                                "clang++ -std=c++20 --precompile -o " +
+                                    slash(Pcm) + " " + slash(ExtMod),
+                                ExtMod) +
+                       "," +
+                       cdbEntry(Build,
+                                "clang++ -std=c++20 -fmodule-file=M=" +
+                                    slash(Pcm) + " -c " + slash(Main),
+                                Main) +
+                       "]");
   }
+};
 
+} // namespace
+
+TEST_F(PrerequisiteModulesTests, OutOfTreeModuleUnitCachedInConsumerProject) {
+  OutOfTreeProject P(TestDir);
   DirectoryBasedGlobalCompilationDatabase DirCDB(FS);
   OverlayCDB CDB(&DirCDB);
 
-  // Out-of-tree module unit has no ancestor CDB of its own.
-  EXPECT_FALSE(CDB.getCompileCommand(ExtModPath));
+  // Precondition: the module unit has no ancestor CDB of its own.
+  ASSERT_FALSE(CDB.getCompileCommand(P.ExtMod));
 
   ModulesBuilder Builder(CDB);
-  auto Info = Builder.buildPrerequisiteModulesFor(MainPath, FS);
+  auto Info = Builder.buildPrerequisiteModulesFor(P.Main, FS);
   ASSERT_TRUE(Info);
 
-  HeaderSearchOptions HS(ProjectDir);
+  HeaderSearchOptions HS(P.Project);
   Info->adjustHeaderSearchOptions(HS);
   ASSERT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
-  std::string BuiltPCMPath = HS.PrebuiltModuleFiles["M"];
-  EXPECT_TRUE(llvm::sys::fs::exists(BuiltPCMPath));
-
-  // Because the out-of-tree module unit has no project root, its persistent 
BMI
-  // cache must fall back to the consuming translation unit's project root.
-  SmallString<256> ExpectedCacheRoot(ProjectDir);
-  llvm::sys::path::append(ExpectedCacheRoot, ".cache", "clangd", "modules");
-  EXPECT_TRUE(StringRef(BuiltPCMPath).starts_with(ExpectedCacheRoot))
-      << "Actual BMI path: " << BuiltPCMPath
-      << "\nExpected prefix: " << ExpectedCacheRoot;
-
-  ParseInputs MainInputs = getInputs("project/main.cpp", CDB);
-  MainInputs.ModulesManager = &Builder;
-  auto CI = buildCompilerInvocation(MainInputs, DiagConsumer);
-  ASSERT_TRUE(CI);
-  EXPECT_TRUE(Info->canReuse(*CI, FS.view(ProjectDir)));
 
-  // Also verify getPrebuiltModuleFile() when an out-of-tree module unit
-  // itself consumes a prebuilt BMI via -fmodule-file= in 
compile_commands.json.
-  SmallString<256> DepModPath(ExternalDir);
-  llvm::sys::path::append(DepModPath, "Dep.cppm");
-  {
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(DepModPath, EC);
-    ASSERT_FALSE(EC);
-    OS << "export module Dep;\nexport int depVal = 7;\n";
-  }
+  llvm::SmallString<256> ExpectedRoot(P.Project);
+  llvm::sys::path::append(ExpectedRoot, ".cache", "clangd", "modules");
+  EXPECT_TRUE(StringRef(HS.PrebuiltModuleFiles["M"]).starts_with(ExpectedRoot))
+      << "Actual: " << HS.PrebuiltModuleFiles["M"];
+}
 
-  SmallString<256> DepPcmPath(BuildDir);
-  llvm::sys::path::append(DepPcmPath, "Dep.pcm");
+// An unrelated ancestor CDB makes the global CDB return an interpolated
+// (heuristic) command for the module unit. It must not shadow the exact entry
+// from the consuming project's CDB.
+TEST_F(PrerequisiteModulesTests,
+       OutOfTreeModuleUnitIgnoresHeuristicAncestorCommand) {
+  OutOfTreeProject P(TestDir);
+
+  llvm::SmallString<256> StrayDir(llvm::sys::path::parent_path(P.ExtMod));
+  llvm::SmallString<256> Unrelated(StrayDir);
+  llvm::sys::path::append(Unrelated, "unrelated.cpp");
+  llvm::SmallString<256> StrayCDB(StrayDir);
+  llvm::sys::path::append(StrayCDB, "compile_commands.json");
+  // -std=c++11 cannot build a module, so using this command would fail.
+  writeFile(StrayCDB,
+            "[" +
+                cdbEntry(StrayDir, "clang++ -std=c++11 -c " + slash(Unrelated),
+                         Unrelated) +
+                "]");
 
-  // Build Dep.pcm once so a valid prebuilt BMI exists on disk.
-  {
-    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
-    std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(CDBPath, EC);
-    ASSERT_FALSE(EC);
-    OS << llvm::formatv(
-        R"json([
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 --precompile {1}",
-    "file": "{1}"
-  }
-])json",
-        SlashBuild, SlashDepMod);
-  }
-  {
-    DirectoryBasedGlobalCompilationDatabase SeedDirCDB(FS);
-    OverlayCDB SeedCDB(&SeedDirCDB);
-    ModulesBuilder SeedBuilder(SeedCDB);
-    // Build a temporary consumer of Dep to produce a valid BMI on disk.
-    SmallString<256> SeedMainPath(ProjectDir);
-    llvm::sys::path::append(SeedMainPath, "seed.cpp");
-    {
-      std::error_code EC;
-      llvm::raw_fd_ostream OS(SeedMainPath, EC);
-      ASSERT_FALSE(EC);
-      OS << "import Dep;\n";
-    }
-    {
-      std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
-      std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
-      std::string SlashSeed = llvm::sys::path::convert_to_slash(SeedMainPath);
-      std::error_code EC;
-      llvm::raw_fd_ostream OS(CDBPath, EC);
-      ASSERT_FALSE(EC);
-      OS << llvm::formatv(
-          R"json([
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 --precompile {1}",
-    "file": "{1}"
-  },
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 -c {2}",
-    "file": "{2}"
-  }
-])json",
-          SlashBuild, SlashDepMod, SlashSeed);
-    }
-    auto SeedInfo = SeedBuilder.buildPrerequisiteModulesFor(SeedMainPath, FS);
-    ASSERT_TRUE(SeedInfo);
-    HeaderSearchOptions SeedHS(ProjectDir);
-    SeedInfo->adjustHeaderSearchOptions(SeedHS);
-    ASSERT_EQ(SeedHS.PrebuiltModuleFiles.count("Dep"), 1u);
-    ASSERT_FALSE(llvm::sys::fs::copy_file(SeedHS.PrebuiltModuleFiles["Dep"],
-                                          DepPcmPath));
-  }
+  DirectoryBasedGlobalCompilationDatabase DirCDB(FS);
+  OverlayCDB CDB(&DirCDB);
 
-  // Now update external/M.cppm to import Dep and pass -fmodule-file=Dep=... on
-  // M.cppm's compile command in compile_commands.json.
-  {
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(ExtModPath, EC);
-    ASSERT_FALSE(EC);
-    OS << "export module M;\nimport Dep;\nexport int extVal = depVal + 35;\n";
-  }
-  {
-    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
-    std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
-    std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath);
-    std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath);
-    std::string SlashDepPcm = llvm::sys::path::convert_to_slash(DepPcmPath);
-    std::string SlashMPcm = llvm::sys::path::convert_to_slash(MPcmPath);
-    std::error_code EC;
-    llvm::raw_fd_ostream OS(CDBPath, EC);
-    ASSERT_FALSE(EC);
-    OS << llvm::formatv(
-        R"json([
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 --precompile -o {4} {1}",
-    "file": "{1}"
-  },
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 -fmodule-file=Dep={4} --precompile -o {5} 
{2}",
-    "file": "{2}"
-  },
-  {
-    "directory": "{0}",
-    "command": "clang++ -std=c++20 -fmodule-file=M={5} -c {3}",
-    "file": "{3}"
-  }
-])json",
-        SlashBuild, SlashDepMod, SlashExtMod, SlashMain, SlashDepPcm,
-        SlashMPcm);
-  }
+  auto Heuristic = CDB.getCompileCommand(P.ExtMod);
+  ASSERT_TRUE(Heuristic);
+  ASSERT_FALSE(Heuristic->Heuristic.empty());
 
-  DirectoryBasedGlobalCompilationDatabase DirCDB2(FS);
-  OverlayCDB CDB2(&DirCDB2);
-  ModulesBuilder BuilderWithPrebuilt(CDB2);
-  auto PrebuiltInfo =
-      BuilderWithPrebuilt.buildPrerequisiteModulesFor(MainPath, FS);
-  ASSERT_TRUE(PrebuiltInfo);
-
-  HeaderSearchOptions PrebuiltHS(ProjectDir);
-  PrebuiltInfo->adjustHeaderSearchOptions(PrebuiltHS);
-  ASSERT_EQ(PrebuiltHS.PrebuiltModuleFiles.count("Dep"), 1u);
-  EXPECT_EQ(PrebuiltHS.PrebuiltModuleFiles["Dep"], std::string(DepPcmPath));
+  ModulesBuilder Builder(CDB);
+  auto Info = Builder.buildPrerequisiteModulesFor(P.Main, FS);
+  ASSERT_TRUE(Info);
+
+  HeaderSearchOptions HS(P.Project);
+  Info->adjustHeaderSearchOptions(HS);
+  EXPECT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
 }
 
 TEST_F(PrerequisiteModulesTests, ModuleImportThroughInclude) {

>From a70883a877b04ee300796d27c31f3ebe76438ac5 Mon Sep 17 00:00:00 2001
From: atharva-potdar <[email protected]>
Date: Sun, 15 Nov 2026 09:20:36 +0530
Subject: [PATCH 3/3] [clangd] Don't let a heuristic ancestor CDB pick the
 module cache root

getModuleProjectRoot trusted the module unit's own project info even when
the global CDB only had an interpolated command for it, so an unrelated
ancestor compile_commands.json could redirect the module cache away from
the consuming project. Use the module unit's root only when its command
is exact; otherwise fall back to the consuming file's root.

Also assert the cache location in
OutOfTreeModuleUnitIgnoresHeuristicAncestorCommand.
---
 clang-tools-extra/clangd/ModulesBuilder.cpp    | 18 ++++++++++++------
 .../unittests/PrerequisiteModulesTest.cpp      |  7 ++++++-
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp 
b/clang-tools-extra/clangd/ModulesBuilder.cpp
index 5fb6f720b5160a..644d25d791ae70 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -94,15 +94,21 @@ std::string normalizePathForCache(PathRef Path) {
   return maybeCaseFoldPath(Normalized);
 }
 
-/// Returns the project source root owning \p ModuleUnitFileName, falling back
-/// to the consuming translation unit's (\p RequiredSource) project root when
-/// the module unit resides outside the workspace tree (e.g. system 
`std.cppm`).
+/// Returns the project source root used for the module cache of
+/// \p ModuleUnitFileName. The module unit's own project root is used only when
+/// the global CDB has an exact entry for it. If it has none, or only an
+/// interpolated (heuristic) command, as with an unrelated ancestor
+/// compile_commands.json or a system `std.cppm`, fall back to the consuming
+/// translation unit's (\p RequiredSource) project root.
 std::optional<std::string>
 getModuleProjectRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
                      const GlobalCompilationDatabase &CDB) {
-  if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
-      PI && !PI->SourceRoot.empty())
-    return PI->SourceRoot;
+  auto Cmd = CDB.getCompileCommand(ModuleUnitFileName);
+  if (Cmd && Cmd->Heuristic.empty()) {
+    if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
+        PI && !PI->SourceRoot.empty())
+      return PI->SourceRoot;
+  }
   if (auto PI = CDB.getProjectInfo(RequiredSource);
       PI && !PI->SourceRoot.empty())
     return PI->SourceRoot;
diff --git a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp 
b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
index ba8f0be3669cc7..ddfe70b625f898 100644
--- a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
+++ b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
@@ -1768,7 +1768,12 @@ TEST_F(PrerequisiteModulesTests,
 
   HeaderSearchOptions HS(P.Project);
   Info->adjustHeaderSearchOptions(HS);
-  EXPECT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
+  ASSERT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
+
+  llvm::SmallString<256> ExpectedRoot(P.Project);
+  llvm::sys::path::append(ExpectedRoot, ".cache", "clangd", "modules");
+  EXPECT_TRUE(StringRef(HS.PrebuiltModuleFiles["M"]).starts_with(ExpectedRoot))
+      << "Actual: " << HS.PrebuiltModuleFiles["M"];
 }
 
 TEST_F(PrerequisiteModulesTests, ModuleImportThroughInclude) {

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

Reply via email to