Skip to content

Commit 770fa09

Browse files
committed
fix(import solver, error): the error message about non existing files while importing didn't show the correct path to the file
1 parent b257b21 commit 770fa09

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

include/Ark/Compiler/Package/ImportSolver.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ namespace Ark::internal
4848
[[nodiscard]] const Node& ast() const noexcept override;
4949

5050
private:
51+
struct ImportWithSource
52+
{
53+
std::filesystem::path file;
54+
Import import;
55+
};
56+
5157
unsigned m_debug_level;
5258
std::vector<std::filesystem::path> m_libenv;
5359
std::filesystem::path m_root; ///< Folder were the entry file is
5460
Node m_ast;
55-
std::stack<Import> m_imports;
61+
std::stack<ImportWithSource> m_imports;
5662
std::unordered_map<std::string, Package> m_packages; ///< Package name to package AST & data mapping
5763
std::vector<std::string> m_imported; ///< List of imports, in the order they were found and parsed
5864

@@ -67,11 +73,11 @@ namespace Ark::internal
6773
* @brief Parse a given file and returns a list of its imports.
6874
* The AST is parsed and stored in m_modules[import.prefix]
6975
*
70-
* @param base_path path to the file containing the import
76+
* @param source path to the file containing the import
7177
* @param import current import directive
72-
* @return std::vector<Import> imports found in the processed file
78+
* @return std::vector<ImportWithSource> imports found in the processed file
7379
*/
74-
std::vector<Import> parseImport(const std::filesystem::path& base_path, const Import& import);
80+
std::vector<ImportWithSource> parseImport(const std::filesystem::path& source, const Import& import);
7581

7682
/**
7783
* @brief Search for an import file, using the root file path

src/arkreactor/Compiler/Package/ImportSolver.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ namespace Ark::internal
1616

1717
ImportSolver& ImportSolver::setup(const std::filesystem::path& root, const std::vector<Import>& origin_imports)
1818
{
19-
if (is_directory(root))
20-
m_root = root;
21-
else
22-
m_root = root.parent_path();
19+
m_root = root.parent_path();
2320

2421
for (const auto& origin_import : std::ranges::reverse_view(origin_imports))
25-
m_imports.push(origin_import);
22+
m_imports.push({ root, origin_import });
2623

2724
return *this;
2825
}
@@ -33,27 +30,27 @@ namespace Ark::internal
3330

3431
while (!m_imports.empty())
3532
{
36-
Import import = m_imports.top();
37-
m_logger.debug("Importing {}", import.toPackageString());
33+
ImportWithSource source = m_imports.top();
34+
m_logger.debug("Importing {}", source.import.toPackageString());
3835

3936
// Remove the top element to process the other imports
4037
// It needs to be removed first because we might be adding
4138
// other imports later and don't want to pop THEM
4239
m_imports.pop();
43-
const auto package = import.toPackageString();
40+
const auto package = source.import.toPackageString();
4441

4542
if (m_packages.contains(package))
4643
{
4744
// merge the definition, so that we can generate valid Full Qualified Names in the name & scope resolver
48-
m_packages[package].import.with_prefix |= import.with_prefix;
49-
m_packages[package].import.is_glob |= import.is_glob;
50-
for (auto&& symbol : import.symbols)
45+
m_packages[package].import.with_prefix |= source.import.with_prefix;
46+
m_packages[package].import.is_glob |= source.import.is_glob;
47+
for (auto&& symbol : source.import.symbols)
5148
m_packages[package].import.symbols.push_back(symbol);
5249
}
5350
else
5451
{
5552
// NOTE: since the "file" (=root) argument doesn't change between all calls, we could get rid of it
56-
std::vector<Import> temp = parseImport(m_root, import);
53+
std::vector<ImportWithSource> temp = parseImport(source.file, source.import);
5754
for (auto& additional_import : std::ranges::reverse_view(temp))
5855
m_imports.push(additional_import);
5956
}
@@ -138,11 +135,11 @@ namespace Ark::internal
138135
return m_ast;
139136
}
140137

141-
std::vector<Import> ImportSolver::parseImport(const std::filesystem::path& base_path, const Import& import)
138+
std::vector<ImportSolver::ImportWithSource> ImportSolver::parseImport(const std::filesystem::path& source, const Import& import)
142139
{
143-
m_logger.traceStart(fmt::format("parseImport {}", base_path.string()));
140+
m_logger.traceStart(fmt::format("parseImport {}", source.string()));
144141

145-
const auto path = findFile(base_path, import);
142+
const auto path = findFile(source, import);
146143
if (path.extension() == ".arkm") // Nothing to import in case of modules
147144
{
148145
// Creating an import node that will stay there when visiting the AST and
@@ -151,9 +148,11 @@ namespace Ark::internal
151148
module_node.push_back(Node(Keyword::Import));
152149

153150
auto package_node = Node(NodeType::List);
154-
std::ranges::transform(import.package, std::back_inserter(package_node.list()), [](const std::string& stem) {
155-
return Node(NodeType::String, stem);
156-
});
151+
std::ranges::transform(
152+
import.package,
153+
std::back_inserter(package_node.list()), [](const std::string& stem) {
154+
return Node(NodeType::String, stem);
155+
});
157156
module_node.push_back(package_node);
158157
// empty symbols list
159158
module_node.push_back(Node(NodeType::List));
@@ -177,7 +176,15 @@ namespace Ark::internal
177176
};
178177

179178
m_logger.traceEnd();
180-
return parser.imports();
179+
180+
auto imports = parser.imports();
181+
std::vector<ImportWithSource> output;
182+
std::ranges::transform(
183+
imports,
184+
std::back_inserter(output), [&path](const Import& i) {
185+
return ImportWithSource { path, i };
186+
});
187+
return output;
181188
}
182189

183190
std::optional<std::filesystem::path> testExtensions(const std::filesystem::path& folder, const std::string& package_path)
@@ -205,7 +212,7 @@ namespace Ark::internal
205212
// fallback, we couldn't find the file
206213
throw CodeError(
207214
fmt::format("While processing file {}, couldn't import {}: file not found",
208-
file.generic_string(), import.toPackageString()),
215+
file.filename().string(), import.toPackageString()),
209216
CodeErrorContext(
210217
file.generic_string(),
211218
import.line,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(import error__this_file_will_never_exist)
2+
3+
(print 1)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
At (import error__this_file_will_never_exist) @ 1:42
2+
1 | (import error__this_file_will_never_exist)
3+
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
2 |
5+
3 | (print 1)
6+
While processing file import_not_found.ark, couldn't import error__this_file_will_never_exist: file not found

0 commit comments

Comments
 (0)