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

Duncan Ogilvie via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 29 08:49:33 PST 2025


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

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.

>From 7c929d3fa47612e48ecc390a183c481c7087a6d6 Mon Sep 17 00:00:00 2001
From: Duncan Ogilvie <mr.exodia.tpodt at gmail.com>
Date: Sat, 29 Nov 2025 17:07:37 +0100
Subject: [PATCH] Add .gitignore file in .cache/clangd/index

---
 .../clangd/index/BackgroundIndexStorage.cpp        | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

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());
+        }
+      }
     }
   }
 



More information about the cfe-commits mailing list