Skip to content

Commit

Permalink
fix(core): provide better error message if we cannot read a file when…
Browse files Browse the repository at this point in the history
… finding imports
  • Loading branch information
Cammisuli committed Oct 23, 2023
1 parent cc8d8e8 commit fb09f07
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions packages/nx/src/native/plugins/js/ts_import_locators.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::path::Path;
Expand Down Expand Up @@ -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<ImportResult> {
fn process_file(
(source_project, file_path): (&String, &String),
) -> anyhow::Result<Option<ImportResult>> {
let now = Instant::now();
let cm = Arc::<SourceMap>::default()
.load_file(Path::new(file_path))
.unwrap();
.map_err(|e| anyhow!("Unable to load {}: {}", file_path, e))?;

let comments = SingleThreadedComments::default();

Expand Down Expand Up @@ -573,16 +576,18 @@ fn process_file((source_project, file_path): (&String, &String)) -> Option<Impor
.filter_map(code_is_not_ignored)
.collect();

Some(ImportResult {
Ok(Some(ImportResult {
file: file_path.clone(),
source_project: source_project.clone(),
static_import_expressions,
dynamic_import_expressions,
})
}))
}

#[napi]
fn find_imports(project_file_map: HashMap<String, Vec<String>>) -> Vec<ImportResult> {
fn find_imports(
project_file_map: HashMap<String, Vec<String>>,
) -> anyhow::Result<Vec<ImportResult>> {
enable_logger();

let files_to_process: Vec<(&String, &String)> = project_file_map
Expand All @@ -592,7 +597,7 @@ fn find_imports(project_file_map: HashMap<String, Vec<String>>) -> Vec<ImportRes

files_to_process
.into_par_iter()
.filter_map(process_file)
.filter_map(|file| process_file(file).transpose())
.collect()
}
#[cfg(test)]
Expand Down Expand Up @@ -643,7 +648,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();

Expand Down Expand Up @@ -795,7 +801,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);
Expand Down Expand Up @@ -917,7 +924,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();

Expand Down Expand Up @@ -963,7 +971,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);
Expand Down Expand Up @@ -1020,7 +1029,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);
Expand Down Expand Up @@ -1058,7 +1068,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);
Expand Down Expand Up @@ -1105,7 +1116,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);
Expand Down Expand Up @@ -1149,7 +1161,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();

Expand Down Expand Up @@ -1188,7 +1201,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);
Expand Down Expand Up @@ -1239,7 +1253,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);
Expand Down

0 comments on commit fb09f07

Please sign in to comment.