Skip to content

[clangd] [Modules] Fix to correctly handle module dependencies #142828

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

Merged
merged 2 commits into from
Jun 6, 2025

Conversation

fleeting-xx
Copy link
Contributor

This is a re-application of #142090 without the unit test changes. A subsequent PR will follow that adds a unit test for module dependencies.

Changes

  • Fix dangling string references in the return value of getAllRequiredModules()
  • Change a couple of calls in getOrBuildModuleFile() to use the loop variable instead of the ModuleName parameter.

@ChuanqiXu9 for review

- Fix dangling string references in the return value of
  getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
  variable instead of the ModuleName parameter.
@llvmbot
Copy link
Member

llvmbot commented Jun 4, 2025

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

@llvm/pr-subscribers-clangd

Author: None (fleeting-xx)

Changes

This is a re-application of llvm/llvm-project#142090 without the unit test changes. A subsequent PR will follow that adds a unit test for module dependencies.

Changes

  • Fix dangling string references in the return value of getAllRequiredModules()
  • Change a couple of calls in getOrBuildModuleFile() to use the loop variable instead of the ModuleName parameter.

@ChuanqiXu9 for review


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

1 Files Affected:

  • (modified) clang-tools-extra/clangd/ModulesBuilder.cpp (+12-10)
diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp
index bf77f43bd28bb..d88aa01aad05d 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -430,10 +430,10 @@ class CachingProjectModules : public ProjectModules {
 /// Collect the directly and indirectly required module names for \param
 /// ModuleName in topological order. The \param ModuleName is guaranteed to
 /// be the last element in \param ModuleNames.
-llvm::SmallVector<StringRef> getAllRequiredModules(PathRef RequiredSource,
-                                                   CachingProjectModules &MDB,
-                                                   StringRef ModuleName) {
-  llvm::SmallVector<llvm::StringRef> ModuleNames;
+llvm::SmallVector<std::string> getAllRequiredModules(PathRef RequiredSource,
+                                                     CachingProjectModules &MDB,
+                                                     StringRef ModuleName) {
+  llvm::SmallVector<std::string> ModuleNames;
   llvm::StringSet<> ModuleNamesSet;
 
   auto VisitDeps = [&](StringRef ModuleName, auto Visitor) -> void {
@@ -444,7 +444,7 @@ llvm::SmallVector<StringRef> getAllRequiredModules(PathRef RequiredSource,
       if (ModuleNamesSet.insert(RequiredModuleName).second)
         Visitor(RequiredModuleName, Visitor);
 
-    ModuleNames.push_back(ModuleName);
+    ModuleNames.push_back(ModuleName.str());
   };
   VisitDeps(ModuleName, VisitDeps);
 
@@ -494,13 +494,13 @@ llvm::Error ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
   // Get Required modules in topological order.
   auto ReqModuleNames = getAllRequiredModules(RequiredSource, MDB, ModuleName);
   for (llvm::StringRef ReqModuleName : ReqModuleNames) {
-    if (BuiltModuleFiles.isModuleUnitBuilt(ModuleName))
+    if (BuiltModuleFiles.isModuleUnitBuilt(ReqModuleName))
       continue;
 
     if (auto Cached = Cache.getModule(ReqModuleName)) {
       if (IsModuleFileUpToDate(Cached->getModuleFilePath(), BuiltModuleFiles,
                                TFS.view(std::nullopt))) {
-        log("Reusing module {0} from {1}", ModuleName,
+        log("Reusing module {0} from {1}", ReqModuleName,
             Cached->getModuleFilePath());
         BuiltModuleFiles.addModuleFile(std::move(Cached));
         continue;
@@ -508,14 +508,16 @@ llvm::Error ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
       Cache.remove(ReqModuleName);
     }
 
+    std::string ReqFileName =
+        MDB.getSourceForModuleName(ReqModuleName, RequiredSource);
     llvm::Expected<ModuleFile> MF = buildModuleFile(
-        ModuleName, ModuleUnitFileName, getCDB(), TFS, BuiltModuleFiles);
+        ReqModuleName, ReqFileName, getCDB(), TFS, BuiltModuleFiles);
     if (llvm::Error Err = MF.takeError())
       return Err;
 
-    log("Built module {0} to {1}", ModuleName, MF->getModuleFilePath());
+    log("Built module {0} to {1}", ReqModuleName, MF->getModuleFilePath());
     auto BuiltModuleFile = std::make_shared<const ModuleFile>(std::move(*MF));
-    Cache.add(ModuleName, BuiltModuleFile);
+    Cache.add(ReqModuleName, BuiltModuleFile);
     BuiltModuleFiles.addModuleFile(std::move(BuiltModuleFile));
   }
 

@ChuanqiXu9
Copy link
Member

I think you should add the test for clang-tools-extra/clangd/test/module_dependencies.test in the original patch and disable it for Windows.

@fleeting-xx
Copy link
Contributor Author

Added the test but disabled it for host=aarch64-pc-windows-msvc instead of system-windows. That will disable it specifically for the platform/build server it wasn't working on.

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

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

LGTM then. Thanks.

BTW, clangd prefer unittests than lit test. So you'd better to use unittests next time.

@ChuanqiXu9 ChuanqiXu9 merged commit 93b0bf6 into llvm:main Jun 6, 2025
11 checks passed
Comment on lines +11 to +13
# RUN: sed -e "s|DIR|%/t|g" %t/compile_commands.json.tmpl > %t/compile_commands.json.tmp
# RUN: sed -e "s|CLANG_CC|%clang|g" %t/compile_commands.json.tmp > %t/compile_commands.json
# RUN: sed -e "s|DIR|%/t|g" %t/definition.jsonrpc.tmpl > %t/definition.jsonrpc.tmp
Copy link
Contributor

Choose a reason for hiding this comment

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

Using sed for paths like this isn't portable, and I'm seeing failures on some internal windows bots because of it. The UNSUPPORTED here doesn't seem sufficient.

Notably, since on windows paths will include backslashes, a number of paths will be interpreted as having escape sequences, so we end up with either an error from the sed command saying we can't use that escape sequence there, or some kind of garbage in the replacement text.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@bogner in case @fleeting-xx doesn't respond in time, would you like to fix these failures in windows by updating the UNSUPPORTED clause properly? I hope we can get this in 20.x. Thanks in ahead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@fleeting-xx I suspect the right medium to long term solution here would be to convert these tests to unit tests, as @ChuanqiXu9 suggested. This would avoid having to mess around with the config file using sed since all of that set up could be done programatically.

Thanks @zmodem for the short term fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I missed this. I'll see about getting these moved to unit tests.

@ChuanqiXu9
Copy link
Member

@fleeting-xx BTW, I think this worth to be backported to 20.x. But we need to fix the above problem first.

zmodem added a commit that referenced this pull request Jun 10, 2025
The test fails (sometimes); see discussion on #142828
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 10, 2025
@ChuanqiXu9 ChuanqiXu9 added this to the LLVM 20.X Release milestone Jun 11, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jun 11, 2025
@ChuanqiXu9
Copy link
Member

/cherry-pick 93b0bf6 5471d93

@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2025

Failed to cherry-pick: 93b0bf6

https://github.com/llvm/llvm-project/actions/runs/15574087990

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

ChuanqiXu9 pushed a commit to ChuanqiXu9/llvm-project that referenced this pull request Jun 11, 2025
…142828)

This is a re-application of llvm#142090 without the unit
test changes. A subsequent PR will follow that adds a unit test for
module dependencies.

- Fix dangling string references in the return value of
getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
variable instead of the ModuleName parameter.

@ChuanqiXu9 for review
ChuanqiXu9 pushed a commit to ChuanqiXu9/llvm-project that referenced this pull request Jun 11, 2025
The test fails (sometimes); see discussion on llvm#142828
@ChuanqiXu9
Copy link
Member

FWIW, I sent #143647

ChuanqiXu9 pushed a commit to ChuanqiXu9/llvm-project that referenced this pull request Jun 11, 2025
…142828)

This is a re-application of llvm#142090 without the unit
test changes. A subsequent PR will follow that adds a unit test for
module dependencies.

- Fix dangling string references in the return value of
getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
variable instead of the ModuleName parameter.
ChuanqiXu9 pushed a commit to ChuanqiXu9/llvm-project that referenced this pull request Jun 11, 2025
The test fails (sometimes); see discussion on llvm#142828
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…142828)

This is a re-application of llvm#142090 without the unit
test changes. A subsequent PR will follow that adds a unit test for
module dependencies.

### Changes
- Fix dangling string references in the return value of
getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
variable instead of the ModuleName parameter.

@ChuanqiXu9 for review
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
The test fails (sometimes); see discussion on llvm#142828
@tstellar tstellar moved this from Needs Triage to Needs Fix in LLVM Release Status Jun 11, 2025
@tstellar tstellar moved this from Needs Fix to Done in LLVM Release Status Jun 12, 2025
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…142828)

This is a re-application of llvm#142090 without the unit
test changes. A subsequent PR will follow that adds a unit test for
module dependencies.

### Changes
- Fix dangling string references in the return value of
getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
variable instead of the ModuleName parameter.

@ChuanqiXu9 for review
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Jun 12, 2025
…142828)

This is a re-application of llvm#142090 without the unit
test changes. A subsequent PR will follow that adds a unit test for
module dependencies.

- Fix dangling string references in the return value of
getAllRequiredModules()
- Change a couple of calls in getOrBuildModuleFile() to use the loop
variable instead of the ModuleName parameter.
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Jun 12, 2025
The test fails (sometimes); see discussion on llvm#142828
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 12, 2025
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
The test fails (sometimes); see discussion on llvm#142828
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

5 participants