Skip to content

Commit

Permalink
chore(trace): fixes and improvements (#9398)
Browse files Browse the repository at this point in the history
### Description

- Added more conditions
- Made emitting errors configurable
- Add inference for multiple node_modules

<!--
  ✍️ Write a short summary of your work.
  If necessary, include relevant screenshots.
-->

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
NicholasLYang authored Nov 7, 2024
1 parent 52f6f46 commit 865cf60
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
49 changes: 28 additions & 21 deletions crates/turbo-trace/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,36 +274,40 @@ impl Tracer {
root: &AbsoluteSystemPath,
existing_resolver: &Resolver,
) -> Option<Resolver> {
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 {
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion crates/turborepo-lib/src/query/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl File {
depth: Option<usize>,
ts_config: Option<String>,
import_type: Option<ImportType>,
emit_errors: Option<bool>,
) -> Result<TraceResult, Error> {
let mut tracer = Tracer::new(
self.run.repo_root().to_owned(),
Expand All @@ -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())
Expand Down

0 comments on commit 865cf60

Please sign in to comment.