@@ -1154,37 +1154,6 @@ async fn type_exists(
11541154 } )
11551155}
11561156
1157- async fn any_exists (
1158- fs_path : FileSystemPath ,
1159- refs : & mut Vec < ResolvedVc < Box < dyn Source > > > ,
1160- ) -> Result < Option < ( FileSystemEntryType , FileSystemPath ) > > {
1161- let result = fs_path. realpath_with_links ( ) . owned ( ) . await ?;
1162- refs. extend (
1163- result
1164- . symlinks
1165- . into_iter ( )
1166- . map ( |path| async move {
1167- Ok ( ResolvedVc :: upcast (
1168- FileSource :: new ( path) . to_resolved ( ) . await ?,
1169- ) )
1170- } )
1171- . try_join ( )
1172- . await ?,
1173- ) ;
1174- let path = result. path ;
1175- let ty = * path. get_type ( ) . await ?;
1176- Ok (
1177- if matches ! (
1178- ty,
1179- FileSystemEntryType :: NotFound | FileSystemEntryType :: Error
1180- ) {
1181- None
1182- } else {
1183- Some ( ( ty, path) )
1184- } ,
1185- )
1186- }
1187-
11881157#[ turbo_tasks:: value( shared) ]
11891158enum ExportsFieldResult {
11901159 Some ( #[ turbo_tasks( debug_ignore, trace_ignore) ] ExportsField ) ,
@@ -1362,11 +1331,12 @@ pub async fn find_context_file_or_package_key(
13621331
13631332#[ derive( Clone , PartialEq , Eq , Serialize , Deserialize , TraceRawVcs , Debug , NonLocalValue ) ]
13641333enum FindPackageItem {
1365- PackageDirectory ( FileSystemPath ) ,
1366- PackageFile ( FileSystemPath ) ,
1334+ PackageDirectory { name : RcStr , dir : FileSystemPath } ,
1335+ PackageFile { name : RcStr , file : FileSystemPath } ,
13671336}
13681337
13691338#[ turbo_tasks:: value]
1339+ #[ derive( Debug ) ]
13701340struct FindPackageResult {
13711341 packages : Vec < FindPackageItem > ,
13721342 affecting_sources : Vec < ResolvedVc < Box < dyn Source > > > ,
@@ -1375,12 +1345,23 @@ struct FindPackageResult {
13751345#[ turbo_tasks:: function]
13761346async fn find_package (
13771347 lookup_path : FileSystemPath ,
1378- package_name : RcStr ,
1348+ package_name : Pattern ,
13791349 options : Vc < ResolveModulesOptions > ,
13801350) -> Result < Vc < FindPackageResult > > {
13811351 let mut packages = vec ! [ ] ;
13821352 let mut affecting_sources = vec ! [ ] ;
13831353 let options = options. await ?;
1354+ let package_name_cell = Pattern :: new ( package_name) ;
1355+
1356+ fn get_package_name ( basepath : & FileSystemPath , package_dir : & FileSystemPath ) -> Result < RcStr > {
1357+ let name = basepath. get_relative_path_to ( package_dir) . unwrap ( ) ;
1358+ if let Some ( name) = name. strip_prefix ( "./" ) {
1359+ Ok ( name. into ( ) )
1360+ } else {
1361+ bail ! ( "Package directory {package_dir} is not inside the lookup path {basepath}" ) ;
1362+ }
1363+ }
1364+
13841365 for resolve_modules in & options. modules {
13851366 match resolve_modules {
13861367 ResolveModules :: Nested ( root_vc, names) => {
@@ -1392,11 +1373,18 @@ async fn find_package(
13921373 for name in names. iter ( ) {
13931374 let fs_path = lookup_path. join ( name) ?;
13941375 if let Some ( fs_path) = dir_exists ( fs_path, & mut affecting_sources) . await ? {
1395- let fs_path = fs_path. join ( & package_name. clone ( ) ) ?;
1396- if let Some ( fs_path) =
1397- dir_exists ( fs_path. clone ( ) , & mut affecting_sources) . await ?
1398- {
1399- packages. push ( FindPackageItem :: PackageDirectory ( fs_path) ) ;
1376+ // TODO this is wrong, it should only match match 1 or 2 levels deep
1377+ // TODO affecting_sources are not getting added?
1378+ let matches =
1379+ read_matches ( fs_path. clone ( ) , rcstr ! ( "" ) , true , package_name_cell)
1380+ . await ?;
1381+ for m in matches {
1382+ if let PatternMatch :: Directory ( _, package_dir) = m {
1383+ packages. push ( FindPackageItem :: PackageDirectory {
1384+ name : get_package_name ( & fs_path, package_dir) ?,
1385+ dir : package_dir. clone ( ) ,
1386+ } ) ;
1387+ }
14001388 }
14011389 }
14021390 }
@@ -1413,28 +1401,56 @@ async fn find_package(
14131401 excluded_extensions,
14141402 } => {
14151403 let excluded_extensions = excluded_extensions. await ?;
1416- let package_dir = dir. join ( & package_name) ?;
1417- if let Some ( ( ty, package_dir) ) =
1418- any_exists ( package_dir. clone ( ) , & mut affecting_sources) . await ?
1419- {
1420- match ty {
1421- FileSystemEntryType :: Directory => {
1422- packages. push ( FindPackageItem :: PackageDirectory ( package_dir. clone ( ) ) ) ;
1404+ // TODO this is wrong, it should only match match 1 or 2 levels deep
1405+ // TODO affecting_sources are not getting added?
1406+ let matches =
1407+ read_matches ( dir. clone ( ) , rcstr ! ( "" ) , true , package_name_cell) . await ?;
1408+ for m in matches {
1409+ match m {
1410+ PatternMatch :: Directory ( _, package_dir) => {
1411+ let name = get_package_name ( dir, package_dir) ?;
1412+ packages. push ( FindPackageItem :: PackageDirectory {
1413+ name : name. clone ( ) ,
1414+ dir : package_dir. clone ( ) ,
1415+ } ) ;
1416+
1417+ for extension in & options. extensions {
1418+ if excluded_extensions. contains ( extension) {
1419+ continue ;
1420+ }
1421+ let package_file = package_dir. append ( & extension. clone ( ) ) ?;
1422+ if let Some ( package_file) =
1423+ exists ( package_file, & mut affecting_sources) . await ?
1424+ {
1425+ packages. push ( FindPackageItem :: PackageFile {
1426+ name : name. clone ( ) ,
1427+ file : package_file,
1428+ } ) ;
1429+ }
1430+ }
14231431 }
1424- FileSystemEntryType :: File => {
1425- packages. push ( FindPackageItem :: PackageFile ( package_dir. clone ( ) ) ) ;
1432+ PatternMatch :: File ( _, package_file) => {
1433+ let name = get_package_name ( dir, package_file) ?;
1434+ packages. push ( FindPackageItem :: PackageFile {
1435+ name : name. clone ( ) ,
1436+ file : package_file. clone ( ) ,
1437+ } ) ;
1438+
1439+ for extension in & options. extensions {
1440+ if excluded_extensions. contains ( extension) {
1441+ continue ;
1442+ }
1443+ let package_file = package_file. append ( & extension. clone ( ) ) ?;
1444+ if let Some ( package_file) =
1445+ exists ( package_file, & mut affecting_sources) . await ?
1446+ {
1447+ packages. push ( FindPackageItem :: PackageFile {
1448+ name : name. clone ( ) ,
1449+ file : package_file,
1450+ } ) ;
1451+ }
1452+ }
14261453 }
1427- _ => { }
1428- }
1429- }
1430- for extension in & options. extensions {
1431- if excluded_extensions. contains ( extension) {
1432- continue ;
1433- }
1434- let package_file = package_dir. append ( & extension. clone ( ) ) ?;
1435- if let Some ( package_file) = exists ( package_file, & mut affecting_sources) . await ?
1436- {
1437- packages. push ( FindPackageItem :: PackageFile ( package_file) ) ;
14381454 }
14391455 }
14401456 }
@@ -2512,7 +2528,7 @@ async fn resolve_module_request(
25122528 request : Vc < Request > ,
25132529 options : Vc < ResolveOptions > ,
25142530 options_value : & ResolveOptions ,
2515- module : & RcStr ,
2531+ module : & Pattern ,
25162532 path : & Pattern ,
25172533 query : RcStr ,
25182534 fragment : RcStr ,
@@ -2523,7 +2539,7 @@ async fn resolve_module_request(
25232539 options,
25242540 options_value,
25252541 |_| {
2526- let full_pattern = Pattern :: concat ( [ module. clone ( ) . into ( ) , path. clone ( ) ] ) ;
2542+ let full_pattern = Pattern :: concat ( [ module. clone ( ) , path. clone ( ) ] ) ;
25272543 full_pattern. into_string ( )
25282544 } ,
25292545 query. clone ( ) ,
@@ -2534,12 +2550,14 @@ async fn resolve_module_request(
25342550 return Ok ( result) ;
25352551 }
25362552
2553+ let mut results = vec ! [ ] ;
2554+
25372555 // Self references, if the nearest package.json has the name of the requested
25382556 // module. This should match only using the exports field and no other
25392557 // fields/fallbacks.
25402558 if let FindSelfReferencePackageResult :: Found { name, package_path } =
25412559 & * find_self_reference ( lookup_path. clone ( ) ) . await ?
2542- && module == name
2560+ && module. is_match ( name)
25432561 {
25442562 let result = resolve_into_package (
25452563 path. clone ( ) ,
@@ -2566,46 +2584,47 @@ async fn resolve_module_request(
25662584 ) ) ;
25672585 }
25682586
2569- let mut results = vec ! [ ] ;
2570-
25712587 // There may be more than one package with the same name. For instance, in a
25722588 // TypeScript project, `compilerOptions.baseUrl` can declare a path where to
25732589 // resolve packages. A request to "foo/bar" might resolve to either
25742590 // "[baseUrl]/foo/bar" or "[baseUrl]/node_modules/foo/bar", and we'll need to
25752591 // try both.
25762592 for item in & result. packages {
25772593 match item {
2578- FindPackageItem :: PackageDirectory ( package_path) => {
2579- results. push ( resolve_into_package (
2580- path. clone ( ) ,
2581- package_path. clone ( ) ,
2582- query. clone ( ) ,
2583- fragment. clone ( ) ,
2584- options,
2585- ) ) ;
2594+ FindPackageItem :: PackageDirectory { name, dir } => {
2595+ results. push (
2596+ resolve_into_package (
2597+ path. clone ( ) ,
2598+ dir. clone ( ) ,
2599+ query. clone ( ) ,
2600+ fragment. clone ( ) ,
2601+ options,
2602+ )
2603+ . with_replaced_request_key ( rcstr ! ( "." ) , RequestKey :: new ( name. clone ( ) ) ) ,
2604+ ) ;
25862605 }
2587- FindPackageItem :: PackageFile ( package_path ) => {
2606+ FindPackageItem :: PackageFile { name , file } => {
25882607 if path. is_match ( "" ) {
25892608 let resolved = resolved (
25902609 RequestKey :: new ( rcstr ! ( "." ) ) ,
2591- package_path . clone ( ) ,
2610+ file . clone ( ) ,
25922611 lookup_path. clone ( ) ,
25932612 request,
25942613 options_value,
25952614 options,
25962615 query. clone ( ) ,
25972616 fragment. clone ( ) ,
25982617 )
2599- . await ?;
2618+ . await ?
2619+ . with_replaced_request_key ( rcstr ! ( "." ) , RequestKey :: new ( name. clone ( ) ) ) ;
26002620 results. push ( resolved)
26012621 }
26022622 }
26032623 }
26042624 }
26052625
26062626 let module_result =
2607- merge_results_with_affecting_sources ( results, result. affecting_sources . clone ( ) )
2608- . with_replaced_request_key ( rcstr ! ( "." ) , RequestKey :: new ( module. clone ( ) ) ) ;
2627+ merge_results_with_affecting_sources ( results, result. affecting_sources . clone ( ) ) ;
26092628
26102629 if options_value. prefer_relative {
26112630 let module_prefix: RcStr = format ! ( "./{module}" ) . into ( ) ;
@@ -2623,8 +2642,9 @@ async fn resolve_module_request(
26232642 options,
26242643 ) )
26252644 . await ?;
2626- let relative_result = relative_result
2627- . with_replaced_request_key ( module_prefix, RequestKey :: new ( module. clone ( ) ) ) ;
2645+ // TODO request key
2646+ // let relative_result = relative_result
2647+ // .with_replaced_request_key(module_prefix, RequestKey::new(module.clone()));
26282648
26292649 Ok ( merge_results ( vec ! [ relative_result, module_result] ) )
26302650 } else {
0 commit comments