Skip to content

Commit 2694c51

Browse files
committed
fix(parsing): every module derivation segments on path components
- the trait-default derivation segments on Normal components; splitting path text on MAIN_SEPARATOR left foreign separators inside a segment (rust, c, cpp inherit the default) - go and csharp derive packages/namespaces from segments in both rules and fallback arms ('/' and '.' joins, conventions unchanged); unrooted out-of-tree files keep the legacy text emission - lua and gdscript derive from segments; '.'-joined modules and res:// resource paths are byte-identical on unix - typescript pipeline fixtures serialize resolver JSON via serde_json; hand-formatted native path text parses as invalid escape sequences and the rules load fails - per-language segmentation locks pin native-text derivation for the trait default (via rust), go, csharp, lua, gdscript
1 parent ba2249c commit 2694c51

7 files changed

Lines changed: 254 additions & 104 deletions

File tree

‎src/parsing/csharp/behavior.rs‎

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,20 @@ impl LanguageBehavior for CSharpBehavior {
184184
let canon_root =
185185
root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
186186

187-
if let Ok(relative) = canon_file.strip_prefix(&canon_root) {
188-
// Get directory path (C# namespaces follow folder structure)
189-
let relative_str = relative.to_str()?;
190-
let path_without_ext =
191-
strip_extension(relative_str, extensions);
192-
let dir_path = if let Some(parent) =
193-
Path::new(path_without_ext).parent()
194-
{
195-
parent.to_str().unwrap_or("")
196-
} else {
197-
""
198-
};
199-
200-
// Combine baseUrl with relative directory (dots for C#)
201-
if dir_path.is_empty() {
187+
if let Some(mut segments) =
188+
crate::parsing::paths::relative_segments(
189+
&canon_file,
190+
&canon_root,
191+
)
192+
{
193+
// C# namespaces follow folder
194+
// structure: the stem never
195+
// contributes
196+
segments.pop();
197+
if segments.is_empty() {
202198
return Some(base_url.clone());
203-
} else {
204-
let namespace_suffix =
205-
dir_path.replace(['/', '\\'], ".");
206-
return Some(format!("{base_url}.{namespace_suffix}"));
207199
}
200+
return Some(format!("{base_url}.{}", segments.join(".")));
208201
}
209202
}
210203
}
@@ -221,14 +214,37 @@ impl LanguageBehavior for CSharpBehavior {
221214
return cached_result;
222215
}
223216

224-
// Fallback: directory-based path (original behavior)
225-
let relative_path = file_path
217+
// Fallback: directory-based namespace (original behavior)
218+
let stripped = file_path
226219
.strip_prefix(project_root)
227220
.ok()
228-
.or_else(|| file_path.strip_prefix("./").ok())
229-
.unwrap_or(file_path);
221+
.or_else(|| file_path.strip_prefix("./").ok());
222+
223+
if let Some(relative_path) = stripped {
224+
let mut segments: Vec<String> = Vec::new();
225+
for component in relative_path.components() {
226+
match component {
227+
std::path::Component::Normal(seg) => {
228+
segments.push(seg.to_string_lossy().into_owned())
229+
}
230+
std::path::Component::CurDir => {}
231+
_ => return None,
232+
}
233+
}
234+
for root_name in ["src", "lib"] {
235+
while segments.len() > 1 && segments[0] == root_name {
236+
segments.remove(0);
237+
}
238+
}
239+
if let Some(last) = segments.pop() {
240+
segments.push(strip_extension(&last, extensions).to_string());
241+
}
242+
return Some(segments.join("."));
243+
}
230244

231-
let path = relative_path.to_str()?;
245+
// Out-of-tree without provider rules: unrooted files carry no
246+
// meaningful namespace; legacy text emission preserved.
247+
let path = file_path.to_str()?;
232248
let path_without_prefix = path
233249
.trim_start_matches("./")
234250
.trim_start_matches("src/")
@@ -358,3 +374,27 @@ impl LanguageBehavior for CSharpBehavior {
358374
|| symbol_module_path.starts_with(&format!("{import_path}."))
359375
}
360376
}
377+
378+
#[cfg(test)]
379+
mod tests {
380+
use super::*;
381+
382+
// Namespace derivation must segment on path components, not path
383+
// text: native separators otherwise survive into the namespace.
384+
#[test]
385+
fn fallback_namespace_segmentation_is_separator_agnostic() {
386+
let behavior = CSharpBehavior::new();
387+
let dir = tempfile::tempdir().unwrap();
388+
let root = dir.path();
389+
390+
assert_eq!(
391+
behavior.module_path_from_file(
392+
&root.join("src").join("Models").join("User.cs"),
393+
root,
394+
&["cs"]
395+
),
396+
Some("Models.User".to_string()),
397+
"fallback segmentation is component-wise on every platform"
398+
);
399+
}
400+
}

‎src/parsing/gdscript/behavior.rs‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,23 @@ impl LanguageBehavior for GdscriptBehavior {
192192
extensions: &[&str],
193193
) -> Option<String> {
194194
let relative = file_path.strip_prefix(project_root).ok()?;
195-
let path = relative.to_string_lossy().replace('\\', "/");
196195

197-
// Strip file extension using the provided extensions list
198-
let path_without_ext = strip_extension(&path, extensions);
199-
200-
let normalized = path_without_ext.trim_start_matches('/');
196+
let mut segments: Vec<String> = Vec::new();
197+
for component in relative.components() {
198+
match component {
199+
std::path::Component::Normal(seg) => {
200+
segments.push(seg.to_string_lossy().into_owned())
201+
}
202+
std::path::Component::CurDir => {}
203+
_ => return None,
204+
}
205+
}
206+
if let Some(last) = segments.pop() {
207+
segments.push(strip_extension(&last, extensions).to_string());
208+
}
201209

202-
Some(format!("res://{normalized}"))
210+
// res:// resource paths are '/'-joined on every platform
211+
Some(format!("res://{}", segments.join("/")))
203212
}
204213

205214
fn get_language(&self) -> Language {
@@ -328,3 +337,23 @@ impl LanguageBehavior for GdscriptBehavior {
328337
true
329338
}
330339
}
340+
341+
#[cfg(test)]
342+
mod tests {
343+
use super::*;
344+
345+
// res:// derivation must segment on path components, not path
346+
// text: resource paths are '/'-joined on every platform.
347+
#[test]
348+
fn module_segmentation_is_separator_agnostic() {
349+
let behavior = GdscriptBehavior::new();
350+
let dir = tempfile::tempdir().unwrap();
351+
let root = dir.path();
352+
353+
assert_eq!(
354+
behavior.module_path_from_file(&root.join("scripts").join("player.gd"), root, &["gd"]),
355+
Some("res://scripts/player".to_string()),
356+
"resource-path segmentation is component-wise on every platform"
357+
);
358+
}
359+
}

‎src/parsing/go/behavior.rs‎

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,19 @@ impl LanguageBehavior for GoBehavior {
168168
let canon_root =
169169
root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
170170

171-
if let Ok(relative) = canon_file.strip_prefix(&canon_root) {
172-
// Get directory path (Go packages are directories)
173-
let relative_str = relative.to_str()?;
174-
let path_without_ext =
175-
strip_extension(relative_str, extensions);
176-
let dir_path = if let Some(parent) =
177-
Path::new(path_without_ext).parent()
178-
{
179-
parent.to_str().unwrap_or("")
180-
} else {
181-
""
182-
};
183-
184-
// Combine baseUrl with relative directory
185-
if dir_path.is_empty() {
171+
if let Some(mut segments) =
172+
crate::parsing::paths::relative_segments(
173+
&canon_file,
174+
&canon_root,
175+
)
176+
{
177+
// Go packages are directories: the
178+
// stem never contributes
179+
segments.pop();
180+
if segments.is_empty() {
186181
return Some(base_url.clone());
187-
} else {
188-
return Some(format!("{base_url}/{dir_path}"));
189182
}
183+
return Some(format!("{base_url}/{}", segments.join("/")));
190184
}
191185
}
192186
}
@@ -203,14 +197,31 @@ impl LanguageBehavior for GoBehavior {
203197
return cached_result;
204198
}
205199

206-
// Fallback: directory-based path (original behavior)
207-
let relative_path = file_path
200+
// Fallback: directory-based package (original behavior)
201+
let stripped = file_path
208202
.strip_prefix(project_root)
209203
.ok()
210-
.or_else(|| file_path.strip_prefix("./").ok())
211-
.unwrap_or(file_path);
204+
.or_else(|| file_path.strip_prefix("./").ok());
205+
206+
if let Some(relative_path) = stripped {
207+
let mut segments = Vec::new();
208+
for component in relative_path.components() {
209+
match component {
210+
std::path::Component::Normal(seg) => segments.push(seg.to_string_lossy()),
211+
_ => return None,
212+
}
213+
}
214+
// Go packages are directories: the stem never contributes
215+
segments.pop();
216+
if segments.is_empty() {
217+
return Some(".".to_string());
218+
}
219+
return Some(segments.join("/"));
220+
}
212221

213-
let path = relative_path.to_str()?;
222+
// Out-of-tree without provider rules: unrooted files carry no
223+
// meaningful package; legacy text emission preserved.
224+
let path = file_path.to_str()?;
214225
let path_clean = path.trim_start_matches("./");
215226
let module_path = strip_extension(path_clean, extensions);
216227

@@ -498,6 +509,26 @@ mod tests {
498509
use crate::parsing::registry::LanguageId;
499510
use std::path::Path;
500511

512+
// Package derivation must segment on path components, not path
513+
// text: native separators otherwise survive into the package path.
514+
#[test]
515+
fn fallback_package_segmentation_is_separator_agnostic() {
516+
let behavior = GoBehavior::new();
517+
let dir = tempfile::tempdir().unwrap();
518+
let root = dir.path();
519+
520+
assert_eq!(
521+
behavior.module_path_from_file(&root.join("pkg").join("util.go"), root, &["go"]),
522+
Some("pkg".to_string()),
523+
"package is the '/'-joined directory on every platform"
524+
);
525+
assert_eq!(
526+
behavior.module_path_from_file(&root.join("main.go"), root, &["go"]),
527+
Some(".".to_string()),
528+
"root files land in the '.' package on every platform"
529+
);
530+
}
531+
501532
#[test]
502533
fn test_module_separator() {
503534
let behavior = GoBehavior::new();

‎src/parsing/language_behavior.rs‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,27 @@ pub trait LanguageBehavior: Send + Sync {
235235
// Step 1: Get relative path from workspace root
236236
let relative_path = file_path.strip_prefix(workspace_root).ok()?;
237237

238-
// Step 2: Strip source root directories (OS-agnostic)
238+
// Step 2: Strip source root directories (component-wise)
239239
let path_without_src = strip_source_root(relative_path, self.source_roots());
240240

241-
// Step 3: Strip extension using passed extensions list
242-
let path_str = path_without_src.to_str()?;
243-
let path_without_ext = strip_extension(path_str, extensions);
244-
245-
// Step 4: Split into components using OS path separator
246-
let components: Vec<&str> = path_without_ext
247-
.split(std::path::MAIN_SEPARATOR)
248-
.filter(|s| !s.is_empty())
249-
.collect();
241+
// Steps 3-4: segments via the OS path parser, extension
242+
// stripped from the stem. Splitting path TEXT on a separator
243+
// literal leaves foreign separators inside a segment.
244+
let mut segments: Vec<String> = Vec::new();
245+
for component in path_without_src.components() {
246+
match component {
247+
std::path::Component::Normal(seg) => {
248+
segments.push(seg.to_string_lossy().into_owned())
249+
}
250+
_ => return None,
251+
}
252+
}
253+
if let Some(last) = segments.pop() {
254+
segments.push(strip_extension(&last, extensions).to_string());
255+
}
250256

251257
// Step 5: Format using language-specific rules
258+
let components: Vec<&str> = segments.iter().map(String::as_str).collect();
252259
self.format_path_as_module(&components)
253260
}
254261

‎src/parsing/lua/behavior.rs‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,22 @@ impl LanguageBehavior for LuaBehavior {
7777
file_path
7878
};
7979

80-
let path = relative_path.to_str()?;
81-
let path_clean = path.trim_start_matches("./");
82-
let module_path = strip_extension(path_clean, extensions);
83-
84-
// Convert path separators to dots (Lua module convention)
85-
let module_path = module_path.replace(['/', '\\'], ".");
80+
let mut segments: Vec<String> = Vec::new();
81+
for component in relative_path.components() {
82+
match component {
83+
std::path::Component::Normal(seg) => {
84+
segments.push(seg.to_string_lossy().into_owned())
85+
}
86+
std::path::Component::CurDir => {}
87+
_ => return None,
88+
}
89+
}
90+
if let Some(last) = segments.pop() {
91+
segments.push(strip_extension(&last, extensions).to_string());
92+
}
8693

94+
// Join with dots (Lua module convention)
95+
let module_path = segments.join(".");
8796
if module_path.is_empty() {
8897
Some(".".to_string())
8998
} else {
@@ -252,6 +261,25 @@ mod tests {
252261
use crate::Visibility;
253262
use tempfile::TempDir;
254263

264+
// Module derivation must segment on path components, not path
265+
// text: native separators otherwise survive into the module path.
266+
#[test]
267+
fn fallback_module_segmentation_is_separator_agnostic() {
268+
let behavior = LuaBehavior::new();
269+
let dir = TempDir::new().unwrap();
270+
let root = dir.path();
271+
272+
assert_eq!(
273+
behavior.module_path_from_file(
274+
&root.join("a").join("b").join("core.lua"),
275+
root,
276+
&["lua"]
277+
),
278+
Some("a.b.core".to_string()),
279+
"fallback segmentation is component-wise on every platform"
280+
);
281+
}
282+
255283
#[test]
256284
fn test_module_separator() {
257285
let behavior = LuaBehavior::new();

‎src/parsing/rust/behavior.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,26 @@ impl LanguageBehavior for RustBehavior {
528528
mod tests {
529529
use super::*;
530530

531+
// Pins the trait-default derivation rust inherits: segmentation is
532+
// component-wise, so foreign separators in path text never survive
533+
// into a module path (the `crate::widget/paint` class).
534+
#[test]
535+
fn default_module_segmentation_is_separator_agnostic() {
536+
let behavior = RustBehavior::new();
537+
let dir = tempfile::tempdir().unwrap();
538+
let root = dir.path();
539+
540+
assert_eq!(
541+
behavior.module_path_from_file(
542+
&root.join("src").join("widget").join("paint.rs"),
543+
root,
544+
&["rs"]
545+
),
546+
Some("crate::widget::paint".to_string()),
547+
"trait-default segmentation is component-wise on every platform"
548+
);
549+
}
550+
531551
#[test]
532552
fn test_format_module_path() {
533553
let behavior = RustBehavior::new();

0 commit comments

Comments
 (0)