Author: Duncan Ogilvie
Date: 2025-12-15T10:18:46+01:00
New Revision: 5785b4a4fb29f4e3e38de628d5be8fc8770d81fd

URL: 
https://github.com/llvm/llvm-project/commit/5785b4a4fb29f4e3e38de628d5be8fc8770d81fd
DIFF: 
https://github.com/llvm/llvm-project/commit/5785b4a4fb29f4e3e38de628d5be8fc8770d81fd.diff

LOG: Add .gitignore file in .cache/clangd/index (#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.

Added: 
    

Modified: 
    clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
    clang-tools-extra/clangd/test/background-index.test
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a95..470be79590863 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -45,7 +45,16 @@ class DiskBackedIndexStorage : public BackgroundIndexStorage 
{
     if (EC != OK) {
       elog("Failed to create directory {0} for index storage: {1}",
            DiskShardRoot, EC.message());
+      return;
     }
+    // Create a .gitignore file in the directory to ignore all files.
+    llvm::SmallString<128> GitignorePath(DiskShardRoot);
+    llvm::sys::path::append(GitignorePath, ".gitignore");
+    auto Error = llvm::writeToOutput(GitignorePath, [](llvm::raw_ostream &OS) {
+      OS << "# This file is autogenerated by clangd.\n*\n";
+      return llvm::Error::success();
+    });
+    llvm::consumeError(std::move(Error));
   }
 
   std::unique_ptr<IndexFileIn>

diff  --git a/clang-tools-extra/clangd/test/background-index.test 
b/clang-tools-extra/clangd/test/background-index.test
index 1983f0957dccf..b42cd8e648437 100644
--- a/clang-tools-extra/clangd/test/background-index.test
+++ b/clang-tools-extra/clangd/test/background-index.test
@@ -18,6 +18,10 @@
 # RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
 # RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
+# Test that the the newly created index directories also have a .gitignore 
file present.
+# RUN: ls %/t/.cache/clangd/index/.gitignore
+# RUN: ls %/t/sub_dir/.cache/clangd/index/.gitignore
+
 # Test the index is read from disk: delete code and restart clangd.
 # RUN: rm %/t/foo.cpp
 # RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index e89fdcc205864..d2b7642bdf01e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -135,6 +135,8 @@ Objective-C
 Miscellaneous
 ^^^^^^^^^^^^^
 
+- Add wildcard ``.gitignore`` file to the clangd index directory.
+
 Improvements to clang-doc
 -------------------------
 


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

Reply via email to