From 9a8a138384d27d476b362bd9a4c7a88a31e86f36 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 30 Jun 2024 23:27:33 +0800 Subject: [PATCH] feat(napi): add tracing --- Cargo.lock | 98 +++++++++++++++++++++++++++++++++++++++++++- README.md | 8 +++- examples/resolver.rs | 2 + napi/Cargo.toml | 9 ++-- napi/src/lib.rs | 7 +++- napi/src/tracing.rs | 23 +++++++++++ 6 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 napi/src/tracing.rs diff --git a/Cargo.lock b/Cargo.lock index 414e8bfc..6f84d1a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,7 +328,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.5", ] [[package]] @@ -347,6 +347,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + [[package]] name = "memchr" version = "2.7.2" @@ -428,6 +434,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f5438dd2b2ff4c6df6e1ce22d825ed2fa93ee2922235cc45186991717f0a892d" +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -468,6 +484,12 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "oxc_napi_resolver" version = "0.0.0" @@ -477,6 +499,7 @@ dependencies = [ "napi-derive", "oxc_resolver", "tokio", + "tracing-subscriber", ] [[package]] @@ -675,6 +698,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -712,6 +744,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tokio" version = "1.38.0" @@ -752,6 +794,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -766,6 +834,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vfs" version = "0.12.0" @@ -785,6 +859,22 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.8" @@ -794,6 +884,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/README.md b/README.md index 12b37a3e..8c5ba003 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Rust port of [enhanced-resolve]. * support project references defined `tsconfig.references` * support [template variable ${configDir} for substitution of config files directory path](https://github.com/microsoft/TypeScript/pull/58042) * supports in-memory file system via the `FileSystem` trait -* contains `tracing` instrumentation +* contains `tracing` instrumentation via `OXC_LOG:DEBUG` ## Usage @@ -167,6 +167,12 @@ e.g. The input values are `options`, `path` and `specifier`, the returned value is `ret`. +### NAPI + +``` +OXC_LOG=DEBUG your_program +``` + ### Rolldown ```bash diff --git a/examples/resolver.rs b/examples/resolver.rs index e77aa053..9209e7e8 100644 --- a/examples/resolver.rs +++ b/examples/resolver.rs @@ -17,6 +17,8 @@ fn main() { let options = ResolveOptions { alias_fields: vec![vec!["browser".into()]], alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])], + extensions: vec![".js".into(), ".ts".into()], + extension_alias: vec![(".js".into(), vec![".ts".into()])], ..ResolveOptions::default() }; diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 0f94a510..46eddc8a 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -11,10 +11,11 @@ test = false doctest = false [dependencies] -oxc_resolver = { path = ".." } -napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] } -napi-derive = { version = "2.16.6" } -tokio = "1.38.0" +oxc_resolver = { path = ".." } +napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] } +napi-derive = { version = "2.16.6" } +tokio = "1.38.0" +tracing-subscriber = { version = "0.3.18", features = [] } # Omit the `regex` feature [build-dependencies] napi-build = "2.1.3" diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 96287041..d4c1da04 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -10,9 +10,13 @@ use std::{ use napi_derive::napi; use oxc_resolver::{ResolveOptions, Resolver}; -use self::options::{NapiResolveOptions, StrOrStrList}; +use self::{ + options::{NapiResolveOptions, StrOrStrList}, + tracing::init_tracing, +}; mod options; +mod tracing; #[napi(object)] pub struct ResolveResult { @@ -47,6 +51,7 @@ pub struct ResolverFactory { impl ResolverFactory { #[napi(constructor)] pub fn new(options: NapiResolveOptions) -> Self { + init_tracing(); Self { resolver: Arc::new(Resolver::new(Self::normalize_options(options))) } } diff --git a/napi/src/tracing.rs b/napi/src/tracing.rs new file mode 100644 index 00000000..1f7d309f --- /dev/null +++ b/napi/src/tracing.rs @@ -0,0 +1,23 @@ +use std::sync::OnceLock; + +use tracing_subscriber::{filter::Targets, prelude::*}; + +/// To debug `oxc_resolver`: +/// `OXC_LOG=DEBUG your program` +pub fn init_tracing() { + static TRACING: OnceLock<()> = OnceLock::new(); + TRACING.get_or_init(|| { + // Usage without the `regex` feature. + // + tracing_subscriber::registry() + .with(std::env::var("OXC_LOG").map_or_else( + |_| Targets::new(), + |env_var| { + use std::str::FromStr; + Targets::from_str(&env_var).unwrap() + }, + )) + .with(tracing_subscriber::fmt::layer()) + .init(); + }); +}