Skip to content

Commit 5394078

Browse files
fix: normalize native engine import paths to remove . and .. segments
The Rust resolve_import function produced paths like src/./db.js because Path::join preserves . segments from relative imports. This caused import edges to be lost for native engine users. Fix by normalizing the resolved PathBuf immediately after join using components().collect(), and upgrading normalize_path to also clean . / .. segments. Remove the temporary path.normalize() JS workaround since the Rust side now returns clean paths directly.
1 parent bf1a16b commit 5394078

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

crates/codegraph-core/src/import_resolution.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use std::path::{Path, PathBuf};
22

33
use crate::types::{AliasMapping, ImportResolutionInput, PathAliases, ResolvedImport};
44

5-
/// Normalize a path to use forward slashes (cross-platform consistency).
5+
/// Normalize a path to use forward slashes and clean `.` / `..` segments
6+
/// (cross-platform consistency).
67
fn normalize_path(p: &str) -> String {
7-
p.replace('\\', "/")
8+
let cleaned: PathBuf = Path::new(p).components().collect();
9+
cleaned.display().to_string().replace('\\', "/")
810
}
911

1012
/// Try resolving via path aliases (tsconfig/jsconfig paths).
@@ -69,10 +71,10 @@ pub fn resolve_import_path(
6971
return import_source.to_string();
7072
}
7173

72-
// Relative import
74+
// Relative import — normalize immediately to remove `.` / `..` segments
7375
let dir = Path::new(from_file).parent().unwrap_or(Path::new(""));
74-
let resolved = dir.join(import_source);
75-
let resolved_str = resolved.display().to_string();
76+
let resolved: PathBuf = dir.join(import_source).components().collect();
77+
let resolved_str = resolved.display().to_string().replace('\\', "/");
7678

7779
// .js → .ts remap
7880
if resolved_str.ends_with(".js") {

src/resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function resolveViaAlias(importSource, aliases, _rootDir) {
3131
}
3232
}
3333

34-
for (const [pattern, targets] of Object.entries(aliases.paths)) {
34+
for (const [pattern, targets] of Object.entries(aliases.paths || {})) {
3535
const prefix = pattern.replace(/\*$/, '');
3636
if (!importSource.startsWith(prefix)) continue;
3737
const rest = importSource.slice(prefix.length);

0 commit comments

Comments
 (0)