luau/CLI/Require.cpp

304 lines
9.2 KiB
C++
Raw Normal View History

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Require.h"
#include "FileUtils.h"
#include "Luau/Common.h"
#include "Luau/Config.h"
#include <algorithm>
#include <array>
#include <utility>
2024-11-15 19:37:29 +00:00
static constexpr char kRequireErrorGeneric[] = "error requiring module";
RequireResolver::RequireResolver(std::string path, RequireContext& requireContext, CacheManager& cacheManager, ErrorHandler& errorHandler)
: pathToResolve(std::move(path))
2024-11-15 19:37:29 +00:00
, requireContext(requireContext)
, cacheManager(cacheManager)
, errorHandler(errorHandler)
{
2024-11-15 19:37:29 +00:00
}
2024-11-15 19:37:29 +00:00
RequireResolver::ResolvedRequire RequireResolver::resolveRequire(std::function<void(const ModuleStatus)> completionCallback)
{
if (isRequireResolved)
{
errorHandler.reportError("require statement has already been resolved");
return ResolvedRequire{ModuleStatus::ErrorReported};
}
2024-11-15 19:37:29 +00:00
if (!initialize())
return ResolvedRequire{ModuleStatus::ErrorReported};
2024-11-15 19:37:29 +00:00
resolvedRequire.status = findModule();
2023-12-15 20:52:08 +00:00
2024-11-15 19:37:29 +00:00
if (completionCallback)
completionCallback(resolvedRequire.status);
2024-10-04 17:42:22 +01:00
2024-11-15 19:37:29 +00:00
isRequireResolved = true;
return resolvedRequire;
}
2024-11-15 19:37:29 +00:00
static bool hasValidPrefix(std::string_view path)
{
2024-11-15 19:37:29 +00:00
return path.compare(0, 2, "./") == 0 || path.compare(0, 3, "../") == 0 || path.compare(0, 1, "@") == 0;
}
2024-11-15 19:37:29 +00:00
static bool isPathAmbiguous(const std::string& path)
{
2024-11-15 19:37:29 +00:00
bool found = false;
for (const char* suffix : {".luau", ".lua"})
{
if (isFile(path + suffix))
{
if (found)
return true;
else
found = true;
}
}
if (isDirectory(path) && found)
return true;
2024-11-15 19:37:29 +00:00
return false;
}
bool RequireResolver::initialize()
{
if (!requireContext.isRequireAllowed())
{
errorHandler.reportError("require is not supported in this context");
return false;
}
2024-11-15 19:37:29 +00:00
if (isAbsolutePath(pathToResolve))
{
errorHandler.reportError("cannot require an absolute path");
return false;
}
std::replace(pathToResolve.begin(), pathToResolve.end(), '\\', '/');
if (!hasValidPrefix(pathToResolve))
{
errorHandler.reportError("require path must start with a valid prefix: ./, ../, or @");
return false;
}
return substituteAliasIfPresent(pathToResolve);
}
2024-11-15 19:37:29 +00:00
RequireResolver::ModuleStatus RequireResolver::findModule()
{
2024-11-15 19:37:29 +00:00
if (!resolveAndStoreDefaultPaths())
return ModuleStatus::ErrorReported;
2024-10-04 17:42:22 +01:00
2024-11-15 19:37:29 +00:00
if (isPathAmbiguous(resolvedRequire.absolutePath))
{
errorHandler.reportError("require path could not be resolved to a unique file");
return ModuleStatus::ErrorReported;
}
2024-11-15 19:37:29 +00:00
static constexpr std::array<const char*, 4> possibleSuffixes = {".luau", ".lua", "/init.luau", "/init.lua"};
size_t unsuffixedAbsolutePathSize = resolvedRequire.absolutePath.size();
for (const char* possibleSuffix : possibleSuffixes)
{
2024-11-15 19:37:29 +00:00
resolvedRequire.absolutePath += possibleSuffix;
2024-11-15 19:37:29 +00:00
if (cacheManager.isCached(resolvedRequire.absolutePath))
return ModuleStatus::Cached;
// Try to read the matching file
2024-11-15 19:37:29 +00:00
if (std::optional<std::string> source = readFile(resolvedRequire.absolutePath))
{
2024-11-15 19:37:29 +00:00
resolvedRequire.identifier = requireContext.createNewIdentifer(resolvedRequire.identifier + possibleSuffix);
resolvedRequire.sourceCode = *source;
return ModuleStatus::FileRead;
}
2024-11-15 19:37:29 +00:00
resolvedRequire.absolutePath.resize(unsuffixedAbsolutePathSize); // truncate to remove suffix
}
2024-11-15 19:37:29 +00:00
if (hasFileExtension(resolvedRequire.absolutePath, {".luau", ".lua"}) && isFile(resolvedRequire.absolutePath))
2024-10-04 17:42:22 +01:00
{
2024-11-15 19:37:29 +00:00
errorHandler.reportError("error requiring module: consider removing the file extension");
return ModuleStatus::ErrorReported;
2024-10-04 17:42:22 +01:00
}
2024-11-15 19:37:29 +00:00
errorHandler.reportError(kRequireErrorGeneric);
return ModuleStatus::ErrorReported;
}
2024-11-15 19:37:29 +00:00
bool RequireResolver::resolveAndStoreDefaultPaths()
{
if (!isAbsolutePath(pathToResolve))
{
2024-11-15 19:37:29 +00:00
std::string identifierContext = getRequiringContextRelative();
std::optional<std::string> absolutePathContext = getRequiringContextAbsolute();
if (!absolutePathContext)
2024-11-15 19:37:29 +00:00
return false;
// resolvePath automatically sanitizes/normalizes the paths
2024-11-15 19:37:29 +00:00
resolvedRequire.identifier = resolvePath(pathToResolve, identifierContext);
resolvedRequire.absolutePath = resolvePath(pathToResolve, *absolutePathContext);
}
else
{
// Here we must explicitly sanitize, as the path is taken as is
2024-11-15 19:37:29 +00:00
std::string sanitizedPath = normalizePath(pathToResolve);
resolvedRequire.identifier = sanitizedPath;
resolvedRequire.absolutePath = std::move(sanitizedPath);
}
2024-11-15 19:37:29 +00:00
return true;
}
std::optional<std::string> RequireResolver::getRequiringContextAbsolute()
{
std::string requiringFile;
2024-11-15 19:37:29 +00:00
if (isAbsolutePath(requireContext.getPath()))
{
// We already have an absolute path for the requiring file
2024-11-15 19:37:29 +00:00
requiringFile = requireContext.getPath();
}
else
{
// Requiring file's stored path is relative to the CWD, must make absolute
std::optional<std::string> cwd = getCurrentWorkingDirectory();
if (!cwd)
2024-11-15 19:37:29 +00:00
{
errorHandler.reportError("could not determine current working directory");
return std::nullopt;
2024-11-15 19:37:29 +00:00
}
2024-11-15 19:37:29 +00:00
if (requireContext.isStdin())
{
// Require statement is being executed from REPL input prompt
// The requiring context is the pseudo-file "stdin" in the CWD
requiringFile = joinPaths(*cwd, "stdin");
}
else
{
// Require statement is being executed in a file, must resolve relative to CWD
2024-11-15 19:37:29 +00:00
requiringFile = resolvePath(requireContext.getPath(), joinPaths(*cwd, "stdin"));
}
}
std::replace(requiringFile.begin(), requiringFile.end(), '\\', '/');
return requiringFile;
}
std::string RequireResolver::getRequiringContextRelative()
{
2024-11-15 19:37:29 +00:00
return requireContext.isStdin() ? "" : requireContext.getPath();
}
2024-11-15 19:37:29 +00:00
bool RequireResolver::substituteAliasIfPresent(std::string& path)
{
2023-12-15 20:52:08 +00:00
if (path.size() < 1 || path[0] != '@')
2024-11-15 19:37:29 +00:00
return true;
2024-09-13 18:14:29 +01:00
// To ignore the '@' alias prefix when processing the alias
const size_t aliasStartPos = 1;
// If a directory separator was found, the length of the alias is the
// distance between the start of the alias and the separator. Otherwise,
// the whole string after the alias symbol is the alias.
size_t aliasLen = path.find_first_of("\\/");
if (aliasLen != std::string::npos)
aliasLen -= aliasStartPos;
const std::string potentialAlias = path.substr(aliasStartPos, aliasLen);
// Not worth searching when potentialAlias cannot be an alias
if (!Luau::isValidAlias(potentialAlias))
{
2024-11-15 19:37:29 +00:00
errorHandler.reportError("@" + potentialAlias + " is not a valid alias");
return false;
2023-12-15 20:52:08 +00:00
}
2024-11-15 19:37:29 +00:00
if (std::optional<std::string> alias = getAlias(potentialAlias))
2023-12-15 20:52:08 +00:00
{
2024-11-15 19:37:29 +00:00
path = *alias + path.substr(potentialAlias.size() + 1);
return true;
}
2024-11-15 19:37:29 +00:00
errorHandler.reportError("@" + potentialAlias + " is not a valid alias");
return false;
}
std::optional<std::string> RequireResolver::getAlias(std::string alias)
{
2024-08-02 00:25:12 +01:00
std::transform(
alias.begin(),
alias.end(),
alias.begin(),
[](unsigned char c)
{
return ('A' <= c && c <= 'Z') ? (c + ('a' - 'A')) : c;
}
);
while (!config.aliases.contains(alias) && !isConfigFullyResolved)
{
2024-11-15 19:37:29 +00:00
if (!parseNextConfig())
return std::nullopt; // error parsing config
}
if (!config.aliases.contains(alias) && isConfigFullyResolved)
return std::nullopt; // could not find alias
const Luau::Config::AliasInfo& aliasInfo = config.aliases[alias];
return resolvePath(aliasInfo.value, aliasInfo.configLocation);
}
2024-11-15 19:37:29 +00:00
bool RequireResolver::parseNextConfig()
{
if (isConfigFullyResolved)
2024-11-15 19:37:29 +00:00
return true; // no config files left to parse
std::optional<std::string> directory;
if (lastSearchedDir.empty())
{
std::optional<std::string> requiringFile = getRequiringContextAbsolute();
if (!requiringFile)
2024-11-15 19:37:29 +00:00
return false;
directory = getParentPath(*requiringFile);
}
else
directory = getParentPath(lastSearchedDir);
if (directory)
{
lastSearchedDir = *directory;
2024-11-15 19:37:29 +00:00
if (!parseConfigInDirectory(*directory))
return false;
}
else
isConfigFullyResolved = true;
2024-11-15 19:37:29 +00:00
return true;
}
2024-11-15 19:37:29 +00:00
bool RequireResolver::parseConfigInDirectory(const std::string& directory)
{
std::string configPath = joinPaths(directory, Luau::kConfigName);
Luau::ConfigOptions::AliasOptions aliasOpts;
aliasOpts.configLocation = configPath;
aliasOpts.overwriteAliases = false;
Luau::ConfigOptions opts;
opts.aliasOptions = std::move(aliasOpts);
if (std::optional<std::string> contents = readFile(configPath))
{
std::optional<std::string> error = Luau::parseConfig(*contents, config, opts);
if (error)
2024-11-15 19:37:29 +00:00
{
errorHandler.reportError("error parsing " + configPath + "(" + *error + ")");
return false;
}
}
2024-11-15 19:37:29 +00:00
return true;
}