From a9b45cc01afa726bf9acd7f30ad3235593a258e2 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 16:26:08 +0900 Subject: fix(autoload): expand the dirsep placeholder in target-dir patterns AutoloadGenerator.php builds the target-dir pattern by quoting the path with a marker in place of the separators, then replacing the quoted marker with [\\/]. The port kept that shape, but preg_quote() here deliberately leaves < and > alone so the regex crate does not read \< as a word boundary, so the replacement never matched and the pattern came out as the literal {^MainFoo}. A root package's target-dir was therefore never stripped from files, classmap or exclude-from-classmap, and dumping one failed outright. Split on the separators and quote each segment instead, so the marker never passes through preg_quote() at all. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/autoload/autoload_generator.rs | 35 ++++++++++------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'crates/shirabe/src/autoload/autoload_generator.rs') diff --git a/crates/shirabe/src/autoload/autoload_generator.rs b/crates/shirabe/src/autoload/autoload_generator.rs index 0f7fe22a..91df1749 100644 --- a/crates/shirabe/src/autoload/autoload_generator.rs +++ b/crates/shirabe/src/autoload/autoload_generator.rs @@ -1896,17 +1896,21 @@ class ComposerStaticInit{} { // remove target-dir from file paths of the root package if is_root { - let target_dir = str_replace( - "\\", - "[\\\\/]", - &preg_quote( - &str_replace_multi( - &package.get_target_dir().unwrap_or_default(), - &[("/", ""), ("\\", "")], - ), - None, - ), - ); + // Regex pattern compatibility: + // PHP swaps every path separator for a `` marker, runs + // `preg_quote` over the whole target dir and rewrites the quoted + // `\` into `[\\/]`. `preg_quote` emits `<` and `>` + // unescaped here, because the `regex` crate reads `\<`/`\>` as word + // boundaries, so the marker is not recoverable from the quoted + // string. Quote each separator-delimited segment on its own and join + // the segments with the same character class. + let target_dir = package + .get_target_dir() + .unwrap_or_default() + .split(['/', '\\']) + .map(|segment| preg_quote(segment, None)) + .collect::>() + .join("[\\\\/]"); path_str = ltrim( &Preg::replace( format!("{{^{}}}", target_dir), @@ -2522,12 +2526,3 @@ fn find_top_level_arrow(entry: &str) -> Option { } None } - -// Helper used by parse_autoloads_type for chained string substitutions. -fn str_replace_multi(input: &str, pairs: &[(&str, &str)]) -> String { - let mut s = input.to_string(); - for (from, to) in pairs { - s = str_replace(from, to, &s); - } - s -} -- cgit v1.3.1-4-g156e