Skip to content

Commit 0124cc3

Browse files
committed
[FIX] server: change str.starts_with with PathBuf::starts_with
This change avoid matching "dir1" as a subidrectory of "dir" as it starts with the same string. PathBuf will match on path components instead
1 parent 808dfc7 commit 0124cc3

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

server/src/core/entry_point.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl EntryPointMgr {
232232

233233
pub fn remove_entries_with_path(&mut self, path: &String) {
234234
for entry in self.iter_all() {
235-
if entry.borrow().path.starts_with(path) { //delete any entrypoint that would be in a subdirectory too
235+
if PathBuf::from(entry.borrow().path.clone()).starts_with(path) { //delete any entrypoint that would be in a subdirectory too
236236
entry.borrow_mut().to_delete = true;
237237
}
238238
}
@@ -289,14 +289,14 @@ impl EntryPointMgr {
289289

290290
/// Transform the path of an addon to the odoo relative path.
291291
/// Otherwise, return the path as is.
292-
pub fn transform_addon_path(&self, path: &str) -> String {
292+
pub fn transform_addon_path(&self, path: &PathBuf) -> String {
293293
for entry in self.addons_entry_points.iter() {
294294
if entry.borrow().is_valid_for(path) {
295-
let path_str = path.to_string();
295+
let path_str = path.sanitize();
296296
return path_str.replace(&entry.borrow().path, entry.borrow().addon_to_odoo_path.as_ref().unwrap());
297297
}
298298
}
299-
path.to_string()
299+
path.sanitize()
300300
}
301301
}
302302

@@ -336,7 +336,7 @@ impl EntryPoint {
336336
res
337337
}
338338

339-
pub fn is_valid_for(&self, path: &str) -> bool {
339+
pub fn is_valid_for(&self, path: &PathBuf) -> bool {
340340
path.starts_with(&self.path)
341341
}
342342

server/src/core/file_mgr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl FileMgr {
412412

413413
pub fn delete_path(session: &mut SessionInfo, uri: &String) {
414414
//delete all files that are the uri or in subdirectory
415-
let matching_keys: Vec<String> = session.sync_odoo.get_file_mgr().borrow_mut().files.keys().filter(|k| k.starts_with(uri)).cloned().collect();
415+
let matching_keys: Vec<String> = session.sync_odoo.get_file_mgr().borrow_mut().files.keys().filter(|k| PathBuf::from(k).starts_with(uri)).cloned().collect();
416416
for key in matching_keys {
417417
let to_del = session.sync_odoo.get_file_mgr().borrow_mut().files.remove(&key);
418418
if let Some(to_del) = to_del {

server/src/core/import_resolver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tracing::{error, info};
44
use std::collections::{HashMap, HashSet};
55
use std::rc::Rc;
66
use std::cell::RefCell;
7-
use std::path::Path;
7+
use std::path::{Path, PathBuf};
88

99
use ruff_text_size::TextRange;
1010
use ruff_python_ast::{Alias, Identifier};
@@ -286,10 +286,10 @@ fn _get_or_create_symbol(session: &mut SessionInfo, for_entry: &Rc<RefCell<Entry
286286
let mut found = false;
287287
let entry_point_mgr = session.sync_odoo.entry_point_mgr.clone();
288288
let entry_point_mgr = entry_point_mgr.borrow();
289-
let from_path = session.sync_odoo.entry_point_mgr.borrow().transform_addon_path(from_path);
290-
let from_path = from_path.as_str();
289+
let from_path = session.sync_odoo.entry_point_mgr.borrow().transform_addon_path(&PathBuf::from(from_path));
290+
let from_path = PathBuf::from(from_path);
291291
for entry in entry_point_mgr.iter_for_import(for_entry) {
292-
if ((entry.borrow().is_public() && (level.is_none() || level.unwrap() == 0)) || entry.borrow().is_valid_for(from_path)) && entry.borrow().addon_to_odoo_path.is_none() {
292+
if ((entry.borrow().is_public() && (level.is_none() || level.unwrap() == 0)) || entry.borrow().is_valid_for(&from_path)) && entry.borrow().addon_to_odoo_path.is_none() {
293293
let entry_point = entry.borrow().get_symbol();
294294
if let Some(entry_point) = entry_point {
295295
let mut next_symbol = entry_point.borrow().get_symbol(&(vec![branch.clone()], vec![]), u32::MAX);

server/src/core/odoo.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl SyncOdoo {
435435
//find which entrypoint to use
436436
for entry in self.entry_point_mgr.borrow().iter_all() {
437437
let entry_point = entry.borrow();
438-
if entry_point.is_public() || from_path.starts_with(&entry_point.path) {
438+
if entry_point.is_public() || PathBuf::from(from_path).starts_with(&entry_point.path) {
439439
let symbols = entry_point.root.borrow().get_symbol(&(entry_point.addon_to_odoo_tree.as_ref().unwrap_or(&entry_point.tree).iter().chain(&tree.0).map(|x| x.clone()).collect(), tree.1.clone()), position);
440440
if !symbols.is_empty() {
441441
return symbols;
@@ -767,7 +767,7 @@ impl SyncOdoo {
767767
let mut parents = vec![];
768768
let ep_mgr = session.sync_odoo.entry_point_mgr.clone();
769769
for entry in ep_mgr.borrow().iter_all() {
770-
if entry.borrow().is_valid_for(path.sanitize().as_str()) {
770+
if entry.borrow().is_valid_for(path) {
771771
let tree = entry.borrow().get_tree_for_entry(path);
772772
let path_symbol = entry.borrow().root.borrow().get_symbol(&tree, u32::MAX);
773773
if path_symbol.is_empty() {
@@ -801,7 +801,7 @@ impl SyncOdoo {
801801
pub fn get_symbol_of_opened_file(session: &mut SessionInfo, path: &PathBuf) -> Option<Rc<RefCell<Symbol>>> {
802802
let path_in_tree = path.to_tree_path();
803803
for entry in session.sync_odoo.entry_point_mgr.borrow().iter_for_import(session.sync_odoo.entry_point_mgr.borrow().main_entry_point.as_ref().unwrap()) {
804-
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path.as_os_str().to_str().unwrap()) {
804+
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path) {
805805
let tree = entry.borrow().get_tree_for_entry(path);
806806
let path_symbol = entry.borrow().root.borrow().get_symbol(&tree, u32::MAX);
807807
if path_symbol.is_empty() {
@@ -838,7 +838,7 @@ impl SyncOdoo {
838838
*/
839839
pub fn path_to_main_entry_tree(&self, path: &PathBuf) -> Option<Tree> {
840840
for entry in self.entry_point_mgr.borrow().iter_main() {
841-
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path.sanitize().as_str()) {
841+
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path) {
842842
let tree = entry.borrow().get_tree_for_entry(path);
843843
return Some(tree);
844844
}
@@ -1338,7 +1338,7 @@ impl Odoo {
13381338
}
13391339
}
13401340
for entry in ep_mgr.borrow().iter_all_but_main() {
1341-
if entry.borrow().is_valid_for(path.as_str()) {
1341+
if entry.borrow().is_valid_for(&PathBuf::from(path)) {
13421342
let tree = entry.borrow().get_tree_for_entry(&PathBuf::from(path.clone()));
13431343
entry.borrow_mut().search_symbols_to_rebuild(session, &tree);
13441344
}

server/src/core/symbols/namespace_symbol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use byteyarn::Yarn;
22
use weak_table::PtrWeakHashSet;
33

4-
use std::{cell::RefCell, collections::HashMap, rc::{Rc, Weak}};
4+
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::{Rc, Weak}};
55

66
use crate::constants::{BuildSteps, OYarn};
77

@@ -53,7 +53,7 @@ impl NamespaceSymbol {
5353
let mut best_length: i32 = -1;
5454
let mut index = 0;
5555
while index < self.directories.len() {
56-
if file.borrow().paths()[0].starts_with(&self.directories[index].path) && self.directories[index].path.len() as i32 > best_length {
56+
if PathBuf::from(&file.borrow().paths()[0]).starts_with(&self.directories[index].path) && self.directories[index].path.len() as i32 > best_length {
5757
best_index = index as i32;
5858
best_length = self.directories[index].path.len() as i32;
5959
}

0 commit comments

Comments
 (0)