Skip to content

Commit

Permalink
Fix calling the open_import_fallback for non existing files
Browse files Browse the repository at this point in the history
(Note: This code path is not run for wasm because dounce::canonicalize
is not used there, but it was used for the test)
  • Loading branch information
ogoffart committed Jul 24, 2023
1 parent edee63c commit f1f5a86
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions internal/compiler/typeloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,30 @@ impl TypeLoader {
(path, None)
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
borrowed_state.diag.push_error(
format!(
"Cannot find requested import \"{file_to_import}\" in the include search path",
),
&import_token,
);
return None;
// We will load using the `open_import_fallback`
// Simplify the path to remove the ".."
let mut path = import_token
.as_ref()
.and_then(|tok| tok.source_file().map(|s| s.path()))
.and_then(|p| p.parent())
.map_or(PathBuf::new(), |p| p.into());
for c in Path::new(file_to_import).components() {
use std::path::Component::*;
match c {
RootDir => path = PathBuf::new(),
CurDir => {}
ParentDir
if matches!(
path.components().last(),
Some(Normal(_) | RootDir)
) =>
{
path.pop();
}
Prefix(_) | ParentDir | Normal(_) => path.push(c),
}
}
(path, None)
}
Err(err) => {
borrowed_state.diag.push_error(
Expand Down Expand Up @@ -394,6 +411,15 @@ impl TypeLoader {
.await;
true
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
state.borrow_mut().diag.push_error(
format!(
"Cannot find requested import \"{file_to_import}\" in the include search path",
),
&import_token,
);
return None;
}
Err(err) => {
state.borrow_mut().diag.push_error(
format!("Error reading requested import \"{}\": {}", path_canon.display(), err),
Expand Down Expand Up @@ -738,7 +764,7 @@ fn test_load_from_callback_ok() {
compiler_config.open_import_fallback = Some(Rc::new(move |path| {
let ok_ = ok_.clone();
Box::pin(async move {
assert_eq!(path, "../FooBar.slint");
assert_eq!(path.replace('\\', "/"), "../FooBar.slint");
assert!(!ok_.get());
ok_.set(true);
Some(Ok("export XX := Rectangle {} ".to_owned()))
Expand All @@ -749,7 +775,7 @@ fn test_load_from_callback_ok() {
let doc_node = crate::parser::parse(
r#"
/* ... */
import { XX } from "../FooBar.slint";
import { XX } from "../Ab/.././FooBar.slint";
X := XX {}
"#
.into(),
Expand Down

0 comments on commit f1f5a86

Please sign in to comment.