[clang-tools-extra] Add .gitignore file in .cache/clangd/index (PR #170003)
Duncan Ogilvie via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 13 03:10:43 PST 2025
https://github.com/mrexodia updated https://github.com/llvm/llvm-project/pull/170003
>From fcaec12dc6f2b6be02ed80d3dfea1574a7a852e2 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 | 9 +++++++++
clang-tools-extra/clangd/test/background-index.test | 4 ++++
clang-tools-extra/docs/ReleaseNotes.rst | 2 ++
3 files changed, 15 insertions(+)
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 42f35daa942e7..b4358da700e35 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -130,6 +130,8 @@ Objective-C
Miscellaneous
^^^^^^^^^^^^^
+- Add wildcard ``.gitignore`` file to the clangd index directory.
+
Improvements to clang-doc
-------------------------
More information about the cfe-commits
mailing list