@@ -15,8 +15,10 @@ use rustc_hash::FxHasher;
1515use tokio:: sync:: OnceCell as OnceLock ;
1616
1717use crate :: {
18- context:: ResolveContext as Ctx , package_json:: PackageJson , path:: PathUtil , FileMetadata ,
19- FileSystem , ResolveError , ResolveOptions , TsConfig ,
18+ context:: ResolveContext as Ctx ,
19+ package_json:: { off_to_location, PackageJson } ,
20+ path:: PathUtil ,
21+ FileMetadata , FileSystem , JSONError , ResolveError , ResolveOptions , TsConfig ,
2022} ;
2123
2224#[ derive( Default ) ]
@@ -311,27 +313,40 @@ impl CachedPathImpl {
311313 . package_json
312314 . get_or_try_init ( || async {
313315 let package_json_path = self . path . join ( "package.json" ) ;
314- let Ok ( package_json_string) = fs. read_to_string ( & package_json_path) . await else {
316+ let Ok ( package_json_string) = fs. read ( & package_json_path) . await else {
315317 return Ok ( None ) ;
316318 } ;
317319 let real_path = if options. symlinks {
318320 self . realpath ( fs) . await ?. join ( "package.json" )
319321 } else {
320322 package_json_path. clone ( )
321323 } ;
322- PackageJson :: parse ( package_json_path. clone ( ) , real_path, & package_json_string)
323- . map ( Arc :: new)
324- . map ( Some )
325- . map_err ( |error| {
326- ResolveError :: from_serde_json_error (
327- package_json_path,
328- & error,
329- Some ( package_json_string) ,
330- )
331- } )
324+ match PackageJson :: parse ( package_json_path. clone ( ) , real_path, package_json_string) {
325+ Ok ( v) => Ok ( Some ( Arc :: new ( v) ) ) ,
326+ Err ( parse_err) => {
327+ let package_json_path = self . path . join ( "package.json" ) ;
328+ let package_json_string = match fs. read_to_string ( & package_json_path) . await {
329+ Ok ( c) => c,
330+ Err ( io_err) => {
331+ return Err ( ResolveError :: from ( io_err) ) ;
332+ }
333+ } ;
334+
335+ let ( line, column) = off_to_location ( & package_json_string, parse_err. index ( ) ) ;
336+
337+ Err ( ResolveError :: JSON ( JSONError {
338+ path : package_json_path,
339+ message : format ! ( "Parsing error: {:?}" , parse_err. error( ) ) ,
340+ line,
341+ column,
342+ content : Some ( package_json_string) ,
343+ } ) )
344+ }
345+ }
332346 } )
333347 . await
334348 . cloned ( ) ;
349+
335350 // https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
336351 match & result {
337352 Ok ( Some ( package_json) ) => {
0 commit comments