Skip to content

Commit 59a0e45

Browse files
committed
[FIX] missing ImportError on Import statement without from or asname
1 parent ccb4d7f commit 59a0e45

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

server/src/core/import_resolver.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use super::odoo::SyncOdoo;
2121
use super::symbols::symbol::Symbol;
2222

2323
pub struct ImportResult {
24-
pub name: OYarn,
24+
pub name: OYarn, //the last imported element
25+
pub var_name: OYarn, // the effective symbol name (asname, or first part in a import A.B.C)
2526
pub found: bool,
2627
pub symbol: Rc<RefCell<Symbol>>,
2728
pub file_tree: Vec<OYarn>, //contains only the first part of a Tree
@@ -108,7 +109,8 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
108109
let mut result = vec![];
109110
for alias in name_aliases {
110111
result.push(ImportResult{
111-
name: OYarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
112+
name: OYarn::from(alias.name.as_ref().to_string()),
113+
var_name: OYarn::from(alias.asname.as_ref().unwrap_or(&alias.name).to_string()),
112114
found: false,
113115
symbol: fallback_sym.as_ref().unwrap().clone(),
114116
file_tree: file_tree.clone(),
@@ -163,8 +165,8 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
163165
// In all "from X import A" case, it simply means search for A
164166
// But in "import A.B.C", it means search for A only, and import B.C
165167
// If user typed import A.B.C as D, we will search for A.B.C to link it to symbol D,
166-
result[name_index as usize].name = name.split(".").map(|s| oyarn!("{}", s)).next().unwrap();
167-
result[name_index as usize].found = true;
168+
result[name_index as usize].var_name = name.split(".").map(|s| oyarn!("{}", s)).next().unwrap();
169+
//result[name_index as usize].found = true; //even if found at this stage, we want to check everything anyway for diagnostics. But if found, we'll keep this symbol as imported
168170
result[name_index as usize].symbol = next_symbol.as_ref().unwrap().clone();
169171
}
170172
if !name_middle_part.is_empty() {
@@ -197,15 +199,19 @@ pub fn resolve_import_stmt(session: &mut SessionInfo, source_file_symbol: &Rc<Re
197199
//TODO what if multiple values?
198200
let ns = next_symbol.as_ref().unwrap().borrow().get_symbol(&(vec![], name_last_name), u32::MAX).get(0).cloned();
199201
last_symbol = ns;
200-
if alias.asname.is_some() && last_symbol.is_none() {
201-
result[name_index as usize].symbol = fallback_sym.as_ref().unwrap_or(&source_root).clone();
202+
if last_symbol.is_none() {
203+
if alias.asname.is_some() {
204+
result[name_index as usize].symbol = fallback_sym.as_ref().unwrap_or(&source_root).clone();
205+
}
202206
continue;
203207
}
204208
}
205209
// we found it ! store the result if not already done
206-
if alias.asname.is_some() && result[name_index as usize].found == false {
210+
if result[name_index as usize].found == false {
207211
result[name_index as usize].found = true;
208-
result[name_index as usize].symbol = last_symbol.as_ref().unwrap().clone();
212+
if alias.asname.is_some() {
213+
result[name_index as usize].symbol = last_symbol.as_ref().unwrap().clone();
214+
}
209215
}
210216
} else {
211217
//everything is ok, let's store the result if not already done

server/src/core/python_arch_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl PythonArchEval {
392392
&mut Some(&mut self.diagnostics));
393393

394394
for _import_result in import_results.iter() {
395-
let variable = self.sym_stack.last().unwrap().borrow().get_positioned_symbol(&_import_result.name, &_import_result.range);
395+
let variable = self.sym_stack.last().unwrap().borrow().get_positioned_symbol(&_import_result.var_name, &_import_result.range);
396396
let Some(variable) = variable.clone() else {
397397
continue;
398398
};

0 commit comments

Comments
 (0)