@@ -974,12 +974,7 @@ async fn analyze_ecmascript_module_internal(
974974 origin,
975975 compile_time_info,
976976 var_graph : & var_graph,
977- allow_project_root_tracing : !source
978- . ident ( )
979- . path ( )
980- . await ?
981- . path
982- . contains ( "/node_modules/" ) ,
977+ allow_project_root_tracing : !source. ident ( ) . path ( ) . await ?. is_in_node_modules ( ) ,
983978 fun_args_values : Default :: default ( ) ,
984979 var_cache : Default :: default ( ) ,
985980 first_import_meta : true ,
@@ -1742,15 +1737,21 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
17421737 return Ok ( ( ) ) ;
17431738 }
17441739
1745- let get_traced_project_dir = async || {
1740+ let get_traced_project_dir = async || -> Result < FileSystemPath > {
17461741 // readFileSync("./foo") should always be relative to the project root, but this is
1747- // dangerous inside of node_modules as it can cause a lot of false positives in the
1748- // tracing, if some package does `path.join(dynamic)`, it would include everything from
1749- // the project root as well.
1750- if allow_project_root_tracing {
1751- compile_time_info. environment ( ) . cwd ( ) . owned ( ) . await
1742+ // dangerous inside of node_modules as it can cause a lot of false positives in the tracing,
1743+ // if some package does `path.join(dynamic)`, it would include everything from the project
1744+ // root as well.
1745+ //
1746+ // Also, when there's no cwd set (i.e. in a tracing-specific module context, as we shouldn't
1747+ // assume a `process.cwd()` for all of node_modules), fallback to the source file directory.
1748+ // This still allows relative file accesses, just not from the project root.
1749+ if allow_project_root_tracing
1750+ && let Some ( cwd) = compile_time_info. environment ( ) . cwd ( ) . owned ( ) . await ?
1751+ {
1752+ Ok ( cwd)
17521753 } else {
1753- Ok ( Some ( source. ident ( ) . path ( ) . await ?. parent ( ) ) )
1754+ Ok ( source. ident ( ) . path ( ) . await ?. parent ( ) )
17541755 }
17551756 } ;
17561757
@@ -1982,18 +1983,16 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
19821983 return Ok ( ( ) ) ;
19831984 }
19841985 }
1985- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
1986- analysis. add_reference (
1987- FileSourceReference :: new (
1988- context_dir,
1989- Pattern :: new ( pat) ,
1990- collect_affecting_sources,
1991- make_issue_source ( ) ,
1992- )
1993- . to_resolved ( )
1994- . await ?,
1995- ) ;
1996- }
1986+ analysis. add_reference (
1987+ FileSourceReference :: new (
1988+ get_traced_project_dir ( ) . await ?,
1989+ Pattern :: new ( pat) ,
1990+ collect_affecting_sources,
1991+ make_issue_source ( ) ,
1992+ )
1993+ . to_resolved ( )
1994+ . await ?,
1995+ ) ;
19971996 return Ok ( ( ) ) ;
19981997 }
19991998 let ( args, hints) = explain_args ( & args) ;
@@ -2038,13 +2037,15 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
20382037 return Ok ( ( ) ) ;
20392038 }
20402039 }
2041- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
2042- analysis. add_reference (
2043- DirAssetReference :: new ( context_dir, Pattern :: new ( pat) , make_issue_source ( ) )
2044- . to_resolved ( )
2045- . await ?,
2046- ) ;
2047- }
2040+ analysis. add_reference (
2041+ DirAssetReference :: new (
2042+ get_traced_project_dir ( ) . await ?,
2043+ Pattern :: new ( pat) ,
2044+ make_issue_source ( ) ,
2045+ )
2046+ . to_resolved ( )
2047+ . await ?,
2048+ ) ;
20482049 return Ok ( ( ) ) ;
20492050 }
20502051
@@ -2080,13 +2081,15 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
20802081 return Ok ( ( ) ) ;
20812082 }
20822083 }
2083- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
2084- analysis. add_reference (
2085- DirAssetReference :: new ( context_dir, Pattern :: new ( pat) , make_issue_source ( ) )
2086- . to_resolved ( )
2087- . await ?,
2088- ) ;
2089- }
2084+ analysis. add_reference (
2085+ DirAssetReference :: new (
2086+ get_traced_project_dir ( ) . await ?,
2087+ Pattern :: new ( pat) ,
2088+ make_issue_source ( ) ,
2089+ )
2090+ . to_resolved ( )
2091+ . await ?,
2092+ ) ;
20902093 return Ok ( ( ) ) ;
20912094 }
20922095 JsValue :: WellKnownFunction ( WellKnownFunctionKind :: ChildProcessSpawnMethod ( name) )
@@ -2130,12 +2133,10 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
21302133 if dynamic {
21312134 show_dynamic_warning = true ;
21322135 }
2133- if ( !dynamic || !ignore_dynamic_requests)
2134- && let Some ( context_dir) = get_traced_project_dir ( ) . await ?
2135- {
2136+ if !dynamic || !ignore_dynamic_requests {
21362137 analysis. add_reference (
21372138 FileSourceReference :: new (
2138- context_dir ,
2139+ get_traced_project_dir ( ) . await ? ,
21392140 Pattern :: new ( pat) ,
21402141 collect_affecting_sources,
21412142 IssueSource :: from_swc_offsets (
@@ -2371,17 +2372,15 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
23712372 . await ?;
23722373 js_value_to_pattern ( & linked_func_call)
23732374 } ;
2374- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
2375- analysis. add_reference (
2376- DirAssetReference :: new (
2377- context_dir,
2378- Pattern :: new ( abs_pattern) ,
2379- make_issue_source ( ) ,
2380- )
2381- . to_resolved ( )
2382- . await ?,
2383- ) ;
2384- }
2375+ analysis. add_reference (
2376+ DirAssetReference :: new (
2377+ get_traced_project_dir ( ) . await ?,
2378+ Pattern :: new ( abs_pattern) ,
2379+ make_issue_source ( ) ,
2380+ )
2381+ . to_resolved ( )
2382+ . await ?,
2383+ ) ;
23852384 return Ok ( ( ) ) ;
23862385 }
23872386 }
@@ -2440,17 +2439,15 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
24402439 . await ?;
24412440 js_value_to_pattern ( & linked_func_call)
24422441 } ;
2443- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
2444- analysis. add_reference (
2445- DirAssetReference :: new (
2446- context_dir,
2447- Pattern :: new ( abs_pattern) ,
2448- make_issue_source ( ) ,
2449- )
2450- . to_resolved ( )
2451- . await ?,
2452- ) ;
2453- }
2442+ analysis. add_reference (
2443+ DirAssetReference :: new (
2444+ get_traced_project_dir ( ) . await ?,
2445+ Pattern :: new ( abs_pattern) ,
2446+ make_issue_source ( ) ,
2447+ )
2448+ . to_resolved ( )
2449+ . await ?,
2450+ ) ;
24542451 return Ok ( ( ) ) ;
24552452 }
24562453 let ( args, hints) = explain_args ( & args) ;
@@ -2498,33 +2495,32 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
24982495 if args. len ( ) == 2
24992496 && let Some ( JsValue :: Object { parts, .. } ) = args. get ( 1 )
25002497 {
2501- if let Some ( context_dir) = get_traced_project_dir ( ) . await ? {
2502- let resolved_dirs = parts
2503- . iter ( )
2504- . filter_map ( |object_part| match object_part {
2505- ObjectPart :: KeyValue (
2506- JsValue :: Constant ( key) ,
2507- JsValue :: Array { items : dirs, .. } ,
2508- ) if key. as_str ( ) == Some ( "includeDirs" ) => {
2509- Some ( dirs. iter ( ) . filter_map ( |dir| dir. as_str ( ) ) )
2510- }
2511- _ => None ,
2512- } )
2513- . flatten ( )
2514- . map ( |dir| {
2515- DirAssetReference :: new (
2516- context_dir. clone ( ) ,
2517- Pattern :: new ( Pattern :: Constant ( dir. into ( ) ) ) ,
2518- make_issue_source ( ) ,
2519- )
2520- . to_resolved ( )
2521- } )
2522- . try_join ( )
2523- . await ?;
2498+ let context_dir = get_traced_project_dir ( ) . await ?;
2499+ let resolved_dirs = parts
2500+ . iter ( )
2501+ . filter_map ( |object_part| match object_part {
2502+ ObjectPart :: KeyValue (
2503+ JsValue :: Constant ( key) ,
2504+ JsValue :: Array { items : dirs, .. } ,
2505+ ) if key. as_str ( ) == Some ( "includeDirs" ) => {
2506+ Some ( dirs. iter ( ) . filter_map ( |dir| dir. as_str ( ) ) )
2507+ }
2508+ _ => None ,
2509+ } )
2510+ . flatten ( )
2511+ . map ( |dir| {
2512+ DirAssetReference :: new (
2513+ context_dir. clone ( ) ,
2514+ Pattern :: new ( Pattern :: Constant ( dir. into ( ) ) ) ,
2515+ make_issue_source ( ) ,
2516+ )
2517+ . to_resolved ( )
2518+ } )
2519+ . try_join ( )
2520+ . await ?;
25242521
2525- for resolved_dir_ref in resolved_dirs {
2526- analysis. add_reference ( resolved_dir_ref) ;
2527- }
2522+ for resolved_dir_ref in resolved_dirs {
2523+ analysis. add_reference ( resolved_dir_ref) ;
25282524 }
25292525
25302526 return Ok ( ( ) ) ;
0 commit comments