From bfa83c4a7ee2d96ef85ca301919a2a3e08f59eb3 Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Tue, 26 Nov 2024 17:31:38 +0800 Subject: [PATCH] bugfix --- .vscode/settings.json | 1 + Cargo.lock | 7 ++ __test__/check_dependencies/index.spec.ts | 31 ++++---- crates/module_graph/Cargo.toml | 1 + crates/module_graph/src/graph.rs | 96 +++++++++-------------- package.json | 2 +- 6 files changed, 65 insertions(+), 73 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b0e7669..d90d53e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,6 +21,7 @@ "musleabihf", "napi", "newable", + "pathdiff", "petgraph", "ropey", "RUSTFLAGS", diff --git a/Cargo.lock b/Cargo.lock index e8d38de..d78ac26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,7 @@ dependencies = [ "oxc_ast", "oxc_resolver", "oxc_span", + "pathdiff", "petgraph", "rayon", "serde", @@ -895,6 +896,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + [[package]] name = "petgraph" version = "0.6.5" diff --git a/__test__/check_dependencies/index.spec.ts b/__test__/check_dependencies/index.spec.ts index f59f3be..1bd4572 100644 --- a/__test__/check_dependencies/index.spec.ts +++ b/__test__/check_dependencies/index.spec.ts @@ -110,21 +110,22 @@ test("Get which files depend on the specified file with all-from", () => { expect(normalizedPaths).toMatchSnapshot(); }); -test("Test error syntax code", () => { - const cwd = path.resolve(dirname(__filename), "features", "error_syntax"); - const current = "a.js"; - const response = checkDependencies(current, { - cwd, - }); - - const normalizedPaths = normalizePaths(response); - - expect(normalizedPaths).toMatchSnapshot(); - - for (const file of response.invalidSyntaxFiles) { - expect(winPath(file.replace(cwd, ""))).toMatchSnapshot(); - } -}); +// test("Test error syntax code", () => { +// const cwd = path.resolve(dirname(__filename), "features", "error_syntax"); +// const current = "a.js"; +// const response = checkDependencies(current, { +// cwd, +// }); + +// const normalizedPaths = normalizePaths(response); + +// expect(normalizedPaths).toMatchSnapshot(); +// console.log("response-->", response); + +// for (const file of response.invalidSyntaxFiles) { +// expect(winPath(file.replace(cwd, ""))).toMatchSnapshot(); +// } +// }); test("Test error syntax code", () => { const cwd = path.resolve(dirname(__filename), "features", "error_syntax"); diff --git a/crates/module_graph/Cargo.toml b/crates/module_graph/Cargo.toml index c6112ec..1660c52 100644 --- a/crates/module_graph/Cargo.toml +++ b/crates/module_graph/Cargo.toml @@ -23,3 +23,4 @@ log = { workspace = true } serde_json = { workspace = true } serde = { workspace = true } oxc_span = { workspace = true } +pathdiff = "0.2" diff --git a/crates/module_graph/src/graph.rs b/crates/module_graph/src/graph.rs index c00c67c..5e7035e 100644 --- a/crates/module_graph/src/graph.rs +++ b/crates/module_graph/src/graph.rs @@ -1,7 +1,7 @@ use std::{ collections::{HashMap, HashSet}, fs::read_to_string, - path::PathBuf, + path::{Path, PathBuf}, sync::{ atomic::{AtomicU32, Ordering}, Arc, Mutex, @@ -25,10 +25,7 @@ use rayon::prelude::*; use utils::SemanticBuilder; use wax::{Glob, WalkEntry, WalkError}; -use crate::{ - debug_expensive, - model::{Args, Edge, Graphics, GroupGraphics}, -}; +use crate::model::{Args, Edge, Graphics, GroupGraphics}; pub struct Graph<'a> { id_counter: Arc, @@ -141,7 +138,7 @@ impl<'a> Graph<'a> { let handler = match builder.build_handler() { Ok(handler) => handler, Err(e) => { - eprintln!("parse error: {} {}", e, path.to_string_lossy()); + // eprintln!("parse error: {} {}", e, path.to_string_lossy()); if let Ok(mut invalid_syntax_files) = self.invalid_syntax_files.lock() { invalid_syntax_files.push( @@ -161,52 +158,46 @@ impl<'a> Graph<'a> { let mut thread_edges: Vec = Vec::new(); for node in nodes.iter() { - let source = self.to_relative_path(&self.args.cwd, path.to_path_buf()); + let source = self.to_relative_path(&self.args.cwd, path); let source_id = self.build_id(&source); let source_value = match node.kind() { AstKind::ImportDeclaration(id) => id.source.value.to_string(), AstKind::ExportAllDeclaration(id) => id.source.value.to_string(), _ => { - let span = Span { start: 0, end: 0 }; - let loc = Location { - start: beans::Position { line: 0, col: 0 }, - end: beans::Position { line: 0, col: 0 }, - }; - let edge = Edge { - source: source_id, - target: empty_id.clone(), - ast_node: AstNode { span, loc }, - }; - thread_edges.push(edge); + // debug_expensive!("______: {} {:?}", source, node.kind()); + // let span = Span { start: 0, end: 0 }; + // let loc = Location { + // start: beans::Position { line: 0, col: 0 }, + // end: beans::Position { line: 0, col: 0 }, + // }; + // let edge = Edge { + // source: source_id, + // target: empty_id.clone(), + // ast_node: AstNode { span, loc }, + // }; + // thread_edges.push(edge); continue; } }; - debug_expensive!("source_value: {}", source_value); - let parent = match path.parent() { Some(p) => p, None => continue, }; - debug_expensive!( - "path {} parent: {}", - path.to_string_lossy(), - parent.to_string_lossy() - ); - let resolved_path = match self.resolver.resolve(&parent, &source_value) { Ok(rp) => rp, - Err(_) => continue, + Err(_) => { + eprintln!( + "resolve error: {} {}", + source_value, + path.to_string_lossy() + ); + continue; + } }; - debug_expensive!( - "resolved {} {}", - source_value, - resolved_path.full_path().to_string_lossy(), - ); - // if resolved_path // .full_path() // .components() @@ -217,20 +208,20 @@ impl<'a> Graph<'a> { let (span, loc) = handler.get_node_box(node); - let target = self.to_relative_path( - &self.args.cwd, - resolved_path.full_path().to_path_buf(), - ); - let target_id = self.build_id(&target); + let target = + self.to_relative_path(&self.args.cwd, resolved_path.full_path()); - debug_expensive!("target {} {}", target, target_id); + let target_id = self.build_id(&target); let edge = self.build_edge(source_id, target_id, span, loc); + thread_edges.push(edge); } if let Ok(mut edges) = self.edges.lock() { edges.extend(thread_edges); + } else { + eprintln!("edges lock failed"); } }); } @@ -357,18 +348,12 @@ impl<'a> Graph<'a> { let bin_map = self.bi_map.lock().unwrap(); let edges = self.edges.lock().unwrap(); - debug_expensive!("bin_map: {:?}", bin_map); - - debug_expensive!("edges: {:?}", edges.len()); - let phantom_deps: Vec = edges .iter() .filter_map(|edge| { let target = bin_map.get_by_right(&edge.target)?; - debug_expensive!("target: {}", target); - - if !target.starts_with("node_modules") || END_ID == target { + if !target.contains("node_modules") || END_ID == target { return None; } @@ -390,8 +375,6 @@ impl<'a> Graph<'a> { phantom_deps }; - debug_expensive!("phantom_deps: {:?}", phantom_deps); - if let Ok(invalid_syntax_files) = self.invalid_syntax_files.lock() { Ok(Graphics { dictionaries: self.get_dictionaries(), @@ -576,12 +559,13 @@ impl<'a> Graph<'a> { } fn build_id(&self, node: &str) -> String { + let node = node.replace("\\", "/"); if let Ok(mut bin_map) = self.bi_map.lock() { - if let Some(id) = bin_map.get_by_left(node) { + if let Some(id) = bin_map.get_by_left(&node) { id.to_string() } else { let id = self.id_counter.fetch_add(1, Ordering::SeqCst); - bin_map.insert(node.to_string().replace("\\", "/"), id.to_string()); + bin_map.insert(node, id.to_string()); id.to_string() } } else { @@ -590,16 +574,14 @@ impl<'a> Graph<'a> { } } - fn to_relative_path( + fn to_relative_path>( &self, cwd: &String, - absolute_path_buf: PathBuf, + absolute_path_buf: P, ) -> String { - if let Ok(absolute_path) = Utf8PathBuf::from_path_buf(absolute_path_buf) { - absolute_path.to_string().replace(cwd, "") - } else { - "".to_string() - } + pathdiff::diff_paths(absolute_path_buf, cwd) + .map(|p| p.to_string_lossy().replace('\\', "/")) + .unwrap_or_default() } fn build_edge( diff --git a/package.json b/package.json index 14e28b8..b801dc4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shined/source-code-diagnosis", - "version": "0.0.61", + "version": "0.0.62", "main": "index.js", "types": "index.d.ts", "napi": {