From 42a7b255f33cd9222287219700e50b60fcc07a38 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 23 Oct 2023 16:12:00 -0400 Subject: [PATCH] fix(core): provide better error message if we cannot read a file when finding imports --- .../native/plugins/js/ts_import_locators.rs | 64 ++++++++++++++----- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/packages/nx/src/native/plugins/js/ts_import_locators.rs b/packages/nx/src/native/plugins/js/ts_import_locators.rs index 80262cd412db2..197a8d358bf4e 100644 --- a/packages/nx/src/native/plugins/js/ts_import_locators.rs +++ b/packages/nx/src/native/plugins/js/ts_import_locators.rs @@ -1,3 +1,4 @@ +use anyhow::anyhow; use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::path::Path; @@ -443,11 +444,13 @@ fn find_specifier_in_require(state: &mut State) -> Option<(String, ImportType)> None } -fn process_file((source_project, file_path): (&String, &String)) -> Option { +fn process_file( + (source_project, file_path): (&String, &String), +) -> anyhow::Result> { let now = Instant::now(); let cm = Arc::::default() .load_file(Path::new(file_path)) - .unwrap(); + .map_err(|e| anyhow!("Unable to load {}: {}", file_path, e))?; let comments = SingleThreadedComments::default(); @@ -573,16 +576,18 @@ fn process_file((source_project, file_path): (&String, &String)) -> Option>) -> Vec { +fn find_imports( + project_file_map: HashMap>, +) -> anyhow::Result> { enable_logger(); let files_to_process: Vec<(&String, &String)> = project_file_map @@ -590,9 +595,24 @@ fn find_imports(project_file_map: HashMap>) -> Vec, Vec<_>) = files_to_process .into_par_iter() - .filter_map(process_file) + .map(process_file) + .partition(|r| r.is_ok()); + + if !errors.is_empty() { + let errors = errors + .into_iter() + .map(|e| e.unwrap_err()) + .map(|e| e.to_string()) + .collect::>() + .join("\n"); + anyhow::bail!("{:?}", errors); + } + + successes + .into_iter() + .filter_map(|r| r.transpose()) .collect() } #[cfg(test)] @@ -643,7 +663,8 @@ import 'a4'; import 'a5'; let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); @@ -795,7 +816,8 @@ import 'a4'; import 'a5'; let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone(), broken_file_path], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -917,7 +939,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); @@ -963,7 +986,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -1020,7 +1044,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -1058,7 +1083,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -1105,7 +1131,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -1149,7 +1176,8 @@ import('./dynamic-import.vue') let test_file_path = temp_dir.display().to_string() + "/test.ts"; - let results = find_imports(HashMap::from([(String::from("a"), vec![test_file_path])])); + let results = + find_imports(HashMap::from([(String::from("a"), vec![test_file_path])])).unwrap(); let result = results.get(0).unwrap(); @@ -1188,7 +1216,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path); @@ -1239,7 +1268,8 @@ import('./dynamic-import.vue') let results = find_imports(HashMap::from([( String::from("a"), vec![test_file_path.clone()], - )])); + )])) + .unwrap(); let result = results.get(0).unwrap(); let ast_results: ImportResult = find_imports_with_ast(test_file_path);