Skip to content

[clangd] fix asserts on incompatible paths in CDB #148019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dsseng
Copy link

@dsseng dsseng commented Jul 10, 2025

A compile_commands.json with Windows paths would make clangd assert on a
POSIX system, and vice versa.

Make path traversal functions skip paths of the incompatible format.

Could not invent a test for this one, should be trivial enough.

Fixes #146798

A compile_commands.json with Windows paths would make clangd assert on a
POSIX system, and vice versa.

Make path traversal functions skip paths of the incompatible format.

Could not invent a test for this one, should be trivial enough.

Fixes llvm#146798
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-clangd

Author: Dmitrii Sharshakov (dsseng)

Changes

A compile_commands.json with Windows paths would make clangd assert on a
POSIX system, and vice versa.

Make path traversal functions skip paths of the incompatible format.

Could not invent a test for this one, should be trivial enough.

Fixes #146798


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

1 Files Affected:

  • (modified) clang-tools-extra/clangd/GlobalCompilationDatabase.cpp (+6)
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index 7c0eb9651feaa..4d934d268fdfd 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -47,6 +47,9 @@ namespace {
 // deepest directory and going up to root. Stops whenever action succeeds.
 void actOnAllParentDirectories(PathRef FileName,
                                llvm::function_ref<bool(PathRef)> Action) {
+  // Skip non-native paths which we cannot traverse or otherwise use.
+  if (!llvm::sys::path::is_absolute(FileName, llvm::sys::path::Style::native))
+    return;
   for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path);
        Path = absoluteParent(Path))
     ;
@@ -680,6 +683,9 @@ class DirectoryBasedGlobalCompilationDatabase::BroadcastThread::Filter {
         SearchPaths[I].setPointer(&Dirs[*Parent.Opts.CompileCommandsDir]);
         continue;
       }
+      // Skip non-native paths which we cannot traverse or otherwise use.
+      if (!llvm::sys::path::is_absolute(AllFiles[I], llvm::sys::path::Style::native))
+        continue;
       if (ExitEarly()) // loading config may be slow
         return Filtered;
       WithContext WithProvidedContent(Parent.Opts.ContextProvider(AllFiles[I]));

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Dmitrii Sharshakov (dsseng)

Changes

A compile_commands.json with Windows paths would make clangd assert on a
POSIX system, and vice versa.

Make path traversal functions skip paths of the incompatible format.

Could not invent a test for this one, should be trivial enough.

Fixes #146798


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

1 Files Affected:

  • (modified) clang-tools-extra/clangd/GlobalCompilationDatabase.cpp (+6)
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index 7c0eb9651feaa..4d934d268fdfd 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -47,6 +47,9 @@ namespace {
 // deepest directory and going up to root. Stops whenever action succeeds.
 void actOnAllParentDirectories(PathRef FileName,
                                llvm::function_ref<bool(PathRef)> Action) {
+  // Skip non-native paths which we cannot traverse or otherwise use.
+  if (!llvm::sys::path::is_absolute(FileName, llvm::sys::path::Style::native))
+    return;
   for (auto Path = absoluteParent(FileName); !Path.empty() && !Action(Path);
        Path = absoluteParent(Path))
     ;
@@ -680,6 +683,9 @@ class DirectoryBasedGlobalCompilationDatabase::BroadcastThread::Filter {
         SearchPaths[I].setPointer(&Dirs[*Parent.Opts.CompileCommandsDir]);
         continue;
       }
+      // Skip non-native paths which we cannot traverse or otherwise use.
+      if (!llvm::sys::path::is_absolute(AllFiles[I], llvm::sys::path::Style::native))
+        continue;
       if (ExitEarly()) // loading config may be slow
         return Filtered;
       WithContext WithProvidedContent(Parent.Opts.ContextProvider(AllFiles[I]));

@dsseng
Copy link
Author

dsseng commented Jul 10, 2025

CC: @kadircet @HighCommander4

Sorry if I'm wrong with the choice of reviewers

Copy link
Author

@dsseng dsseng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I identified all the places where traversal with attempts to find parent path is done, but it does not assert now when running Helix in a provided sample project

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
View the diff from clang-format here.
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index 4d934d268..dd1aa2a1a 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -684,7 +684,8 @@ public:
         continue;
       }
       // Skip non-native paths which we cannot traverse or otherwise use.
-      if (!llvm::sys::path::is_absolute(AllFiles[I], llvm::sys::path::Style::native))
+      if (!llvm::sys::path::is_absolute(AllFiles[I],
+                                        llvm::sys::path::Style::native))
         continue;
       if (ExitEarly()) // loading config may be slow
         return Filtered;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clangd] Crashing when running on Mac and Windows paths are found in compile_commands.json, and vice versa
2 participants