Skip to content

Commit cb4373a

Browse files
committed
Fix: Alias not found when the required Luau file is not present in the same path as .luaurc
Search for the config file by traversing directories backward up to the root (where the main file is located) to gather the configurations. The root of .luaurc is considered the directory of the main file, not the current working directory, similar to how Luau runs. This is because it is easier to manage. In the future, there might be an option to customize this behavior.
1 parent 3ca7709 commit cb4373a

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/luaumb.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <map>
55
#include <queue>
6+
#include <stack>
67
#include <filesystem>
78
#include <optional>
89
#include <fstream>
@@ -51,34 +52,42 @@ int main(int argc, char** argv) {
5152
return 1;
5253
}
5354

54-
const std::string config_file_relative_path = module_path.relative.parent_path().string();
55+
std::filesystem::path relative_path = module_path.relative.parent_path();
5556

56-
if (configs.find(config_file_relative_path) == configs.end()) {
57-
Luau::Config config;
57+
if (configs.find(relative_path.string()) == configs.end()) {
58+
std::stack<std::pair<const std::string, std::optional<std::string>>> config_stack;
59+
60+
while (true) {
61+
if ((configs.find(relative_path.string()) != configs.end())) {
62+
Luau::Config config = configs[relative_path.string()];
63+
64+
while (!config_stack.empty()) {
65+
const auto [path, config_file] = config_stack.top();
66+
config_stack.pop();
67+
68+
if (config_file) {
69+
Luau::ConfigOptions config_option = {false, std::optional<Luau::ConfigOptions::AliasOptions>({path, true})};
70+
std::optional<std::string> error = Luau::parseConfig(*config_file, config, config_option);
71+
72+
if (error) throw std::runtime_error("Error parsing config: " + *error + "\n File: " + module_path.path.string());
73+
} else {
74+
config = Luau::Config(config);
75+
}
76+
77+
configs[path] = config;
78+
}
5879

59-
std::filesystem::path relative = module_path.relative.parent_path().parent_path();
60-
while (relative.string() != "/") {
61-
const auto it = configs.find(relative.string());
62-
if (it != configs.end()) {
63-
config = Luau::Config(it->second);
6480
break;
65-
} else {
66-
relative = relative.parent_path();
6781
}
68-
}
6982

70-
const std::filesystem::path config_file_path = module_path.path.parent_path() / Luau::kConfigName;
71-
if (std::optional<std::string> config_file = readFile(config_file_path.string())) {
72-
Luau::ConfigOptions config_option = {false, std::optional<Luau::ConfigOptions::AliasOptions>({config_file_relative_path, true})};
73-
std::optional<std::string> error = Luau::parseConfig(*config_file, config, config_option);
83+
config_stack.push({relative_path.string(), readFile((module_path.root / relative_path.relative_path() / Luau::kConfigName).string())});
7484

75-
if (error) throw std::runtime_error("Error parsing config: " + *error + "\n File: " + module_path.path.string());
85+
if (relative_path.string() == "/") configs["/"] = Luau::Config();
86+
else relative_path = relative_path.parent_path();
7687
}
77-
78-
configs[config_file_relative_path] = config;
7988
}
8089

81-
const Luau::Config& config = configs[config_file_relative_path];
90+
const Luau::Config& config = configs[module_path.relative.parent_path().string()];
8291

8392
RequireFunctionLocalizerResult result = require_function_localizer(*file);
8493
errors.insert(errors.end(), result.parse_errors.begin(), result.parse_errors.end());

0 commit comments

Comments
 (0)