Skip to content

Commit 85cc799

Browse files
committed
Fix module names no longer resolving to virtual paths
And add tests for it. Also fix an exception not getting thrown / caught properly Fixes #734 Fixes #735
1 parent c04866b commit 85cc799

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Fixed a regression in 1.32.2 breaking resolution of virtual paths from real paths, particularly around `script` and relative usages of it. ([#734](https://github.com/JohnnyMorganz/luau-lsp/issues/734), [#735](https://github.com/JohnnyMorganz/luau-lsp/issues/735))
12+
913
## [1.32.2] - 2024-08-10
1014

1115
### Changed

src/Workspace.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ bool WorkspaceFolder::isIgnoredFile(const std::filesystem::path& path, const std
140140
// We want to test globs against a relative path to workspace, since that's what makes most sense
141141
auto relativeFsPath = path.lexically_relative(rootUri.fsPath());
142142
if (relativeFsPath == std::filesystem::path())
143-
throw new JsonRpcException(lsp::ErrorCode::InternalError, "isIgnoredFile failed: relative path is default-constructed");
143+
throw JsonRpcException(lsp::ErrorCode::InternalError, "isIgnoredFile failed: relative path is default-constructed");
144144
auto relativePathString = relativeFsPath.generic_string(); // HACK: we convert to generic string so we get '/' separators
145145

146146
auto config = givenConfig ? *givenConfig : client->getConfiguration(rootUri);
@@ -160,7 +160,7 @@ bool WorkspaceFolder::isIgnoredFileForAutoImports(const std::filesystem::path& p
160160
// We want to test globs against a relative path to workspace, since that's what makes most sense
161161
auto relativeFsPath = path.lexically_relative(rootUri.fsPath());
162162
if (relativeFsPath == std::filesystem::path())
163-
throw new JsonRpcException(lsp::ErrorCode::InternalError, "isIgnoredFileForAutoImports failed: relative path is default-constructed");
163+
throw JsonRpcException(lsp::ErrorCode::InternalError, "isIgnoredFileForAutoImports failed: relative path is default-constructed");
164164
auto relativePathString = relativeFsPath.generic_string(); // HACK: we convert to generic string so we get '/' separators
165165

166166
auto config = givenConfig ? *givenConfig : client->getConfiguration(rootUri);

src/platform/roblox/RobloxSourcemap.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ std::optional<SourceNodePtr> RobloxPlatform::getSourceNodeFromRealPath(const std
477477
auto canonicalName = std::filesystem::weakly_canonical(name, ec);
478478
if (ec.value() != 0)
479479
canonicalName = name;
480+
// URI-ify the file path so that its normalised (in particular, the drive letter)
481+
canonicalName = Uri::parse(Uri::file(canonicalName).toString()).fsPath();
480482
auto strName = canonicalName.generic_string();
481483
if (realPathsToSourceNodes.find(strName) == realPathsToSourceNodes.end())
482484
return std::nullopt;

tests/Sourcemap.test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,62 @@ TEST_CASE_FIXTURE(Fixture, "relative_and_absolute_types_are_consistent")
374374
CHECK((absoluteTy == relativeTy));
375375
}
376376

377+
TEST_CASE_FIXTURE(Fixture, "get_virtual_module_name_from_real_path")
378+
{
379+
#ifdef _WIN32
380+
workspace.rootUri = Uri::parse("file:///c%3A/Users/Development/project");
381+
workspace.fileResolver.rootUri = Uri::parse("file:///c%3A/Users/Development/project");
382+
loadSourcemap(R"(
383+
{
384+
"name": "Game",
385+
"className": "DataModel",
386+
"children": [{"name": "MainScript", "className": "ModuleScript", "filePaths": ["Foo\\Test.luau"]}]
387+
}
388+
)");
389+
#else
390+
workspace.rootUri = Uri::parse("/home/project");
391+
workspace.fileResolver.rootUri = Uri::parse("/home/project");
392+
loadSourcemap(R"(
393+
{
394+
"name": "Game",
395+
"className": "DataModel",
396+
"children": [{"name": "MainScript", "className": "ModuleScript", "filePaths": ["Foo/Test.luau"]}]
397+
}
398+
)");
399+
#endif
400+
401+
auto uri = Uri::file(workspace.rootUri.fsPath() / "Foo" / "Test.luau");
402+
403+
CHECK_EQ(workspace.fileResolver.getModuleName(uri), "game/MainScript");
404+
}
405+
406+
TEST_CASE_FIXTURE(Fixture, "get_real_path_from_virtual_name")
407+
{
408+
#ifdef _WIN32
409+
workspace.rootUri = Uri::parse("file:///c%3A/Users/Development/project");
410+
workspace.fileResolver.rootUri = Uri::parse("file:///c%3A/Users/Development/project");
411+
loadSourcemap(R"(
412+
{
413+
"name": "Game",
414+
"className": "DataModel",
415+
"children": [{"name": "MainScript", "className": "ModuleScript", "filePaths": ["Foo\\Test.luau"]}]
416+
}
417+
)");
418+
#else
419+
workspace.rootUri = Uri::parse("/home/project");
420+
workspace.fileResolver.rootUri = Uri::parse("/home/project");
421+
loadSourcemap(R"(
422+
{
423+
"name": "Game",
424+
"className": "DataModel",
425+
"children": [{"name": "MainScript", "className": "ModuleScript", "filePaths": ["Foo/Test.luau"]}]
426+
}
427+
)");
428+
#endif
429+
430+
CHECK_EQ(workspace.platform->resolveToRealPath("game/MainScript"), workspace.rootUri.fsPath() / "Foo" / "Test.luau");
431+
}
432+
377433
TEST_CASE_FIXTURE(Fixture, "sourcemap_path_is_normalised_to_match_root_uri_subchild_with_lower_case_drive_letter")
378434
{
379435
#ifdef _WIN32

0 commit comments

Comments
 (0)