[clang-tools-extra] Add .gitignore file in .cache/clangd/index (PR #170003)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 29 08:50:20 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangd

Author: Duncan Ogilvie (mrexodia)

<details>
<summary>Changes</summary>

This solves a common issue where users have to manually add the `.cache/clangd/index/` folder to their `.gitignore`. I got this idea from [ruff](https://github.com/astral-sh/ruff), which creates `.ruff_cache/.gitignore` and it would greatly improve the user experience for everyone without requiring per-computer configurations and without any significant cost.

---
Full diff: https://github.com/llvm/llvm-project/pull/170003.diff


1 Files Affected:

- (modified) clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp (+14) 


``````````diff
diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a95..1dff0b55f82cd 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -45,6 +45,20 @@ class DiskBackedIndexStorage : public BackgroundIndexStorage {
     if (EC != OK) {
       elog("Failed to create directory {0} for index storage: {1}",
            DiskShardRoot, EC.message());
+    } else {
+      // Create a .gitignore file in the directory to ignore all files.
+      llvm::SmallString<128> GitignorePath(DiskShardRoot);
+      llvm::sys::path::append(GitignorePath, ".gitignore");
+      if (!llvm::sys::fs::exists(GitignorePath)) {
+        llvm::raw_fd_ostream GitignoreFile(GitignorePath, EC,
+                                          llvm::sys::fs::OF_None);
+        if (EC == OK) {
+          GitignoreFile << "*\n";
+        } else {
+          elog("Failed to create .gitignore in {0}: {1}", DiskShardRoot,
+              EC.message());
+        }
+      }
     }
   }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/170003


More information about the cfe-commits mailing list