From 865cf6073863b1ce03a5bef2be5d393cac399b07 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Thu, 7 Nov 2024 14:39:00 -0500 Subject: [PATCH] chore(trace): fixes and improvements (#9398) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description - Added more conditions - Made emitting errors configurable - Add inference for multiple node_modules ### Testing Instructions --- crates/turbo-trace/src/tracer.rs | 49 +++++++++++++++----------- crates/turborepo-lib/src/query/file.rs | 5 ++- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/crates/turbo-trace/src/tracer.rs b/crates/turbo-trace/src/tracer.rs index 5057c22befd1e..86efde5dbb211 100644 --- a/crates/turbo-trace/src/tracer.rs +++ b/crates/turbo-trace/src/tracer.rs @@ -274,36 +274,40 @@ impl Tracer { root: &AbsoluteSystemPath, existing_resolver: &Resolver, ) -> Option { - let resolver = root + let tsconfig_dir = root .ancestors() .skip(1) - .find(|p| p.join_component("tsconfig.json").exists()) - .map(|ts_config_dir| { - let mut options = existing_resolver.options().clone(); + .find(|p| p.join_component("tsconfig.json").exists()); + + // Resolves the closest `node_modules` directory. This is to work with monorepos + // where both the package and the monorepo have a `node_modules` + // directory. + let node_modules_dir = root + .ancestors() + .skip(1) + .find(|p| p.join_component("node_modules").exists()); + + if tsconfig_dir.is_some() || node_modules_dir.is_some() { + let mut options = existing_resolver.options().clone(); + if let Some(tsconfig_dir) = tsconfig_dir { options.tsconfig = Some(TsconfigOptions { - config_file: ts_config_dir + config_file: tsconfig_dir .join_component("tsconfig.json") .as_std_path() .into(), references: TsconfigReferences::Auto, }); + } - existing_resolver.clone_with_options(options) - }); + if let Some(node_modules_dir) = node_modules_dir { + options = options + .with_module(node_modules_dir.join_component("node_modules").to_string()); + } - root.ancestors() - .skip(1) - .find(|p| p.join_component("node_modules").exists()) - .map(|node_modules_dir| { - let node_modules = node_modules_dir.join_component("node_modules"); - let resolver = resolver.as_ref().unwrap_or(existing_resolver); - let options = resolver - .options() - .clone() - .with_module(node_modules.to_string()); - - resolver.clone_with_options(options) - }) + Some(existing_resolver.clone_with_options(options)) + } else { + None + } } pub fn create_resolver(&mut self) -> Resolver { @@ -312,7 +316,10 @@ impl Tracer { .with_force_extension(EnforceExtension::Disabled) .with_extension(".ts") .with_extension(".tsx") - .with_condition_names(&["import", "require"]); + .with_module(self.cwd.join_component("node_modules").to_string()) + // Condition names are used to determine which export to use when importing a module. + // We add a bunch so oxc_resolver can resolve all kinds of imports. + .with_condition_names(&["import", "require", "node", "default"]); if let Some(ts_config) = self.ts_config.take() { options.tsconfig = Some(TsconfigOptions { diff --git a/crates/turborepo-lib/src/query/file.rs b/crates/turborepo-lib/src/query/file.rs index 200a2c9ffc2d7..d5f2f9f2c5884 100644 --- a/crates/turborepo-lib/src/query/file.rs +++ b/crates/turborepo-lib/src/query/file.rs @@ -197,6 +197,7 @@ impl File { depth: Option, ts_config: Option, import_type: Option, + emit_errors: Option, ) -> Result { let mut tracer = Tracer::new( self.run.repo_root().to_owned(), @@ -209,7 +210,9 @@ impl File { } let mut result = tracer.trace(depth).await; - result.emit_errors(); + if emit_errors.unwrap_or(true) { + result.emit_errors(); + } // Remove the file itself from the result result.files.remove(&self.path); TraceResult::new(result, self.run.clone())