@@ -10,8 +10,23 @@ use swc_coverage_instrument::{
1010} ;
1111
1212use tracing_subscriber:: fmt:: format:: FmtSpan ;
13+ use typed_path:: Utf8TypedPath ;
1314use wax:: Pattern ;
1415
16+ /// Normalize a file path to use forward slashes for consistent glob matching
17+ fn normalize_path_for_glob_matching ( path : & str ) -> String {
18+ let typed_path = Utf8TypedPath :: derive ( path) ;
19+ if typed_path. is_windows ( ) {
20+ typed_path. with_unix_encoding ( ) . to_string ( )
21+ } else if path. contains ( '\\' ) {
22+ // Fallback: if the path contains backslashes but wasn't detected as Windows,
23+ // still normalize it by replacing backslashes with forward slashes
24+ path. replace ( '\\' , "/" )
25+ } else {
26+ path. to_string ( )
27+ }
28+ }
29+
1530fn initialize_instrumentation_log ( log_options : & InstrumentLogOptions ) {
1631 let log_level = match log_options. level . as_deref ( ) {
1732 Some ( "error" ) => Some ( tracing:: Level :: ERROR ) ,
@@ -64,7 +79,8 @@ pub fn process(program: Program, metadata: TransformPluginProgramMetadata) -> Pr
6479 if let Some ( exclude) = & instrument_options. unstable_exclude {
6580 match wax:: any ( exclude. iter ( ) . map ( |s| s. as_ref ( ) ) . collect :: < Vec < & str > > ( ) ) {
6681 Ok ( p) => {
67- if p. is_match ( filename) {
82+ let normalized_filename = normalize_path_for_glob_matching ( filename) ;
83+ if p. is_match ( normalized_filename. as_str ( ) ) {
6884 return program;
6985 }
7086 }
@@ -86,3 +102,41 @@ pub fn process(program: Program, metadata: TransformPluginProgramMetadata) -> Pr
86102
87103 program. apply ( & mut visit_mut_pass ( visitor) )
88104}
105+
106+ #[ cfg( test) ]
107+ mod tests {
108+ use super :: * ;
109+
110+ #[ test]
111+ fn test_normalize_path_for_glob_matching ( ) {
112+ // Test Windows paths are normalized to Unix-style
113+ let result = normalize_path_for_glob_matching ( r"C:\Users\project\test\index.test.ts" ) ;
114+ println ! ( "Windows path result: {}" , result) ;
115+ // The typed-path crate converts Windows paths to Unix format, but may strip the drive letter
116+ // The important thing is that backslashes are converted to forward slashes
117+ assert ! ( result. contains( "/Users/project/test/index.test.ts" ) ) ;
118+
119+ // Test mixed separators are normalized
120+ let result = normalize_path_for_glob_matching ( r"C:\Users/project\test/file.js" ) ;
121+ println ! ( "Mixed separators result: {}" , result) ;
122+ assert ! ( result. contains( "/Users/project/test/file.js" ) ) ;
123+
124+ // Test Unix paths remain unchanged
125+ assert_eq ! (
126+ normalize_path_for_glob_matching( "/home/user/project/src/utils/helper.js" ) ,
127+ "/home/user/project/src/utils/helper.js"
128+ ) ;
129+
130+ // Test relative Unix paths remain unchanged
131+ assert_eq ! (
132+ normalize_path_for_glob_matching( "src/components/Button.tsx" ) ,
133+ "src/components/Button.tsx"
134+ ) ;
135+
136+ // Test that backslashes are converted to forward slashes
137+ let windows_path = r"project\src\test\file.ts" ;
138+ let result = normalize_path_for_glob_matching ( windows_path) ;
139+ println ! ( "Relative Windows path result: {}" , result) ;
140+ assert ! ( result. contains( "project/src/test/file.ts" ) ) ;
141+ }
142+ }
0 commit comments