Index symlinks by replacing tryGetRealName with getName+normalization

Fix #639
This commit is contained in:
Fangrui Song 2020-07-03 18:52:54 -07:00
parent 468258d641
commit 8e9ad7d2c7
3 changed files with 8 additions and 9 deletions

View File

@ -17,13 +17,13 @@
#include <llvm/Support/Path.h> #include <llvm/Support/Path.h>
using namespace clang; using namespace clang;
using namespace llvm;
namespace ccls { namespace ccls {
std::string pathFromFileEntry(const FileEntry &file) { std::string pathFromFileEntry(const FileEntry &file) {
StringRef name = file.tryGetRealPathName(); SmallString<128> path(file.getName());
if (name.empty()) sys::path::remove_dots(path, /*remove_dot_dot=*/true);
name = file.getName(); std::string ret(path);
std::string ret = normalizePath(name);
// Resolve symlinks outside of workspace folders, e.g. /usr/include/c++/7.3.0 // Resolve symlinks outside of workspace folders, e.g. /usr/include/c++/7.3.0
return normalizeFolder(ret) ? ret : realPath(ret); return normalizeFolder(ret) ? ret : realPath(ret);
} }

View File

@ -348,17 +348,16 @@ void do_initialize(MessageHandler *m, InitializeParam &param,
std::string path = wf.uri.getPath(); std::string path = wf.uri.getPath();
ensureEndsInSlash(path); ensureEndsInSlash(path);
std::string real = realPath(path) + '/'; std::string real = realPath(path) + '/';
workspaceFolders.emplace_back(path, path == real ? "" : real); workspaceFolders.emplace_back(path, real);
} }
if (workspaceFolders.empty()) { if (workspaceFolders.empty()) {
std::string real = realPath(project_path) + '/'; std::string real = realPath(project_path) + '/';
workspaceFolders.emplace_back(project_path, workspaceFolders.emplace_back(project_path, real);
project_path == real ? "" : real);
} }
std::sort(workspaceFolders.begin(), workspaceFolders.end(), std::sort(workspaceFolders.begin(), workspaceFolders.end(),
[](auto &l, auto &r) { return l.first.size() > r.first.size(); }); [](auto &l, auto &r) { return l.first.size() > r.first.size(); });
for (auto &[folder, real] : workspaceFolders) for (auto &[folder, real] : workspaceFolders)
if (real.empty()) if (real == folder)
LOG_S(INFO) << "workspace folder: " << folder; LOG_S(INFO) << "workspace folder: " << folder;
else else
LOG_S(INFO) << "workspace folder: " << folder << " -> " << real; LOG_S(INFO) << "workspace folder: " << folder << " -> " << real;

View File

@ -139,7 +139,7 @@ std::string realPath(const std::string &path) {
bool normalizeFolder(std::string &path) { bool normalizeFolder(std::string &path) {
for (auto &[root, real] : g_config->workspaceFolders) for (auto &[root, real] : g_config->workspaceFolders)
if (real.size() && llvm::StringRef(path).startswith(real)) { if (StringRef(path).startswith(real)) {
path = root + path.substr(real.size()); path = root + path.substr(real.size());
return true; return true;
} }