Skip to content
This repository was archived by the owner on Jun 12, 2022. It is now read-only.

Commit 1b9ea41

Browse files
committed
Fix resolution of .luaurc config files
1 parent dd6f1f6 commit 1b9ea41

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

main.cpp

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,11 @@ struct CliConfigResolver : Luau::ConfigResolver
396396
{
397397
Luau::Config defaultConfig;
398398

399+
std::optional<SourceNodePtr> sourceMapRoot;
400+
std::optional<std::filesystem::path> stdinFilepath;
401+
399402
mutable std::unordered_map<std::string, Luau::Config> configCache;
400-
mutable std::vector<std::pair<std::string, std::string>> configErrors;
403+
mutable std::vector<std::pair<std::filesystem::path, std::string>> configErrors;
401404

402405
CliConfigResolver()
403406
{
@@ -406,23 +409,36 @@ struct CliConfigResolver : Luau::ConfigResolver
406409

407410
const Luau::Config& getConfig(const Luau::ModuleName& name) const override
408411
{
409-
std::optional<std::string> path = getParentPath(name);
410-
if (!path)
412+
std::optional<std::filesystem::path> realPath;
413+
if (name == "-")
414+
{
415+
// Handle stdin
416+
realPath = stdinFilepath;
417+
}
418+
else if (isManagedModule(name))
419+
{
420+
if (sourceMapRoot.has_value())
421+
realPath = RojoResolver::resolveRequireToRealPath(name, *sourceMapRoot);
422+
}
423+
else
424+
{
425+
realPath = name;
426+
}
427+
428+
if (!realPath.has_value() || !realPath->has_parent_path())
411429
return defaultConfig;
412430

413-
return readConfigRec(*path);
431+
return readConfigRec(realPath->parent_path());
414432
}
415433

416-
const Luau::Config& readConfigRec(const std::string& path) const
434+
const Luau::Config& readConfigRec(const std::filesystem::path& path) const
417435
{
418-
auto it = configCache.find(path);
436+
auto it = configCache.find(path.generic_string());
419437
if (it != configCache.end())
420438
return it->second;
421439

422-
std::optional<std::string> parent = getParentPath(path);
423-
Luau::Config result = parent ? readConfigRec(*parent) : defaultConfig;
424-
425-
std::string configPath = joinPaths(path, Luau::kConfigName);
440+
Luau::Config result = path.has_parent_path() ? readConfigRec(path.parent_path()) : defaultConfig;
441+
auto configPath = path / Luau::kConfigName;
426442

427443
if (std::optional<std::string> contents = readFile(configPath))
428444
{
@@ -431,7 +447,7 @@ struct CliConfigResolver : Luau::ConfigResolver
431447
configErrors.push_back({configPath, *error});
432448
}
433449

434-
return configCache[path] = result;
450+
return configCache[path.generic_string()] = result;
435451
}
436452
};
437453

@@ -729,33 +745,34 @@ int main(int argc, char** argv)
729745
Luau::FrontendOptions frontendOptions;
730746
frontendOptions.retainFullTypeGraphs = annotate;
731747

748+
CliConfigResolver configResolver;
749+
configResolver.stdinFilepath = stdinFilepath;
750+
732751
CliFileResolver fileResolver;
733752
fileResolver.excludeVirtualPath = excludeVirtualPath;
734753
fileResolver.stdinFilepath = stdinFilepath;
735-
if (sourcemapPath.has_value())
754+
if (sourcemapPath.has_value() || projectPath.has_value())
736755
{
737-
auto sourceMap = RojoResolver::parseSourceMap(sourcemapPath.value());
738-
if (sourceMap)
756+
std::optional<ResolvedSourceMap> sourceMap;
757+
if (sourcemapPath)
739758
{
740-
fileResolver.sourceMap = sourceMap.value();
741-
fileResolver.sourceMapRegistered = true;
742-
if (dumpMap)
743-
dumpSourceMap(*fileResolver.sourceMap.root, 0);
759+
sourceMap = RojoResolver::parseSourceMap(sourcemapPath.value());
744760
}
745-
}
746-
else if (projectPath.has_value())
747-
{
748-
auto sourceMap = RojoResolver::parseProjectFile(projectPath.value());
749-
if (sourceMap)
761+
else if (projectPath)
762+
{
763+
sourceMap = RojoResolver::parseProjectFile(projectPath.value());
764+
}
765+
766+
if (sourceMap.has_value())
750767
{
751768
fileResolver.sourceMap = sourceMap.value();
752769
fileResolver.sourceMapRegistered = true;
753770
if (dumpMap)
754771
dumpSourceMap(*fileResolver.sourceMap.root, 0);
772+
configResolver.sourceMapRoot = sourceMap->root;
755773
}
756774
}
757775

758-
CliConfigResolver configResolver;
759776
Luau::Frontend frontend(&fileResolver, &configResolver, frontendOptions);
760777
Luau::registerBuiltinTypes(frontend.typeChecker);
761778

@@ -876,8 +893,8 @@ int main(int argc, char** argv)
876893
{
877894
failed += int(configResolver.configErrors.size());
878895

879-
for (const auto& pair : configResolver.configErrors)
880-
fprintf(stderr, "%s: %s\n", pair.first.c_str(), pair.second.c_str());
896+
for (const auto& [path, err] : configResolver.configErrors)
897+
fprintf(stderr, "%s: %s\n", path.generic_string().c_str(), err.c_str());
881898
}
882899

883900
if (format == ReportFormat::Luacheck)

0 commit comments

Comments
 (0)