From 52779f1d9c884b0ea421904ee7b886d7b7d1b01a Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Wed, 20 Nov 2024 15:57:04 -0500 Subject: [PATCH] feat(trace): more trace fixes (#9474) ### Description A couple small fixes for tracing: - Add more extensions for support - Add more files to look at for a main file (`module` is non-standard but common) - Try resolving `@types/` if `` fails to resolve ### Testing Instructions Added tests for new extensions, for module field, and for importing types from `foo` that really come from `@types/foo` --- Cargo.lock | 7 ++--- crates/turbo-trace/Cargo.toml | 2 +- crates/turbo-trace/src/tracer.rs | 29 +++++++++++++++---- crates/turborepo-lib/src/query/file.rs | 3 ++ crates/turborepo/tests/query.rs | 2 +- ...ex.ts`_with_dependencies_(npm@10.5.0).snap | 11 ++++++- ...ndex.js`_with_dependents_(npm@10.5.0).snap | 2 +- ...ndex.ts`_with_dependents_(npm@10.5.0).snap | 2 +- .../turbo_trace_monorepo/apps/my-app/index.ts | 4 ++- .../apps/my-app/package.json | 3 +- .../turbo_trace_monorepo/apps/my-app/types.ts | 2 +- .../packages/another/{index.js => index.jsx} | 0 .../packages/module-package/my-module.mjs | 1 + .../packages/module-package/package.json | 4 +++ 14 files changed, 55 insertions(+), 17 deletions(-) rename turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/another/{index.js => index.jsx} (100%) create mode 100644 turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/my-module.mjs create mode 100644 turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/package.json diff --git a/Cargo.lock b/Cargo.lock index c5a658ed6f9d1..46054e239253e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3617,13 +3617,12 @@ dependencies = [ [[package]] name = "oxc_resolver" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf8bcda84674ae69228a823dcdb81eac9a398d99f1bbc1dbf00623009fc11c1" +checksum = "ff67bf7c8cb5f4ea7537faa849892d8e13a408d50d0903aa40889fd395c66993" dependencies = [ "cfg-if", "dashmap 6.1.0", - "dunce", "indexmap 2.2.6", "json-strip-comments", "once_cell", @@ -6117,7 +6116,7 @@ dependencies = [ "futures", "globwalk", "miette", - "oxc_resolver 2.0.0", + "oxc_resolver 2.1.0", "swc_common", "swc_ecma_ast", "swc_ecma_parser", diff --git a/crates/turbo-trace/Cargo.toml b/crates/turbo-trace/Cargo.toml index 8fb3ed3bd84c7..9d710c271d0b7 100644 --- a/crates/turbo-trace/Cargo.toml +++ b/crates/turbo-trace/Cargo.toml @@ -10,7 +10,7 @@ clap = { version = "4.5.17", features = ["derive"] } futures = { workspace = true } globwalk = { version = "0.1.0", path = "../turborepo-globwalk" } miette = { workspace = true, features = ["fancy"] } -oxc_resolver = { version = "2.0.0" } +oxc_resolver = { version = "2.1.0" } swc_common = { workspace = true, features = ["concurrent", "tty-emitter"] } swc_ecma_ast = { workspace = true } swc_ecma_parser = { workspace = true } diff --git a/crates/turbo-trace/src/tracer.rs b/crates/turbo-trace/src/tracer.rs index 86efde5dbb211..3f34d54c86db2 100644 --- a/crates/turbo-trace/src/tracer.rs +++ b/crates/turbo-trace/src/tracer.rs @@ -59,13 +59,14 @@ pub enum TraceError { span: SourceSpan, #[source_code] text: String, + #[help] + reason: String, }, #[error("failed to walk files")] GlobError(Arc), } impl TraceResult { - #[allow(dead_code)] pub fn emit_errors(&self) { let handler = Handler::with_tty_emitter( ColorConfig::Auto, @@ -152,8 +153,7 @@ impl Tracer { file_content.clone(), ); - let syntax = if file_path.extension() == Some("ts") || file_path.extension() == Some("tsx") - { + let syntax = if matches!(file_path.extension(), Some("ts") | Some("tsx")) { Syntax::Typescript(TsSyntax { tsx: file_path.extension() == Some("tsx"), decorators: true, @@ -211,6 +211,18 @@ impl Tracer { debug!("built in: {:?}", err); } Err(err) => { + // Try to resolve the import as a type import via `@/types/` + let type_package = format!("@types/{}", import); + let resolved_type_import = resolver + .resolve(file_path, type_package.as_str()) + .ok() + .and_then(|resolved| resolved.into_path_buf().try_into().ok()); + + if let Some(resolved_type_import) = resolved_type_import { + files.push(resolved_type_import); + continue; + } + debug!("failed to resolve: {:?}", err); let (start, end) = source_map.span_to_char_offset(&source_file, *span); let start = start as usize; @@ -221,8 +233,8 @@ impl Tracer { file_path: file_path.to_string(), span: SourceSpan::new(start.into(), (end - start).into()), text: file_content.clone(), + reason: err.to_string(), }); - continue; } } } @@ -316,7 +328,14 @@ impl Tracer { .with_force_extension(EnforceExtension::Disabled) .with_extension(".ts") .with_extension(".tsx") - .with_module(self.cwd.join_component("node_modules").to_string()) + .with_extension(".jsx") + .with_extension(".d.ts") + .with_extension(".mjs") + .with_extension(".cjs") + // Some packages export a `module` field instead of `main`. This is non-standard, + // but was a proposal at some point. + .with_main_field("module") + .with_main_field("types") // 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"]); diff --git a/crates/turborepo-lib/src/query/file.rs b/crates/turborepo-lib/src/query/file.rs index d5f2f9f2c5884..77e78cf54fb21 100644 --- a/crates/turborepo-lib/src/query/file.rs +++ b/crates/turborepo-lib/src/query/file.rs @@ -76,6 +76,7 @@ impl File { #[derive(SimpleObject, Debug, Default)] pub struct TraceError { message: String, + reason: String, path: Option, import: Option, start: Option, @@ -113,6 +114,7 @@ impl From for TraceError { span, text, file_path, + reason, .. } => { let import = text @@ -123,6 +125,7 @@ impl From for TraceError { TraceError { message, import, + reason, path: Some(file_path), start: Some(span.offset()), end: Some(span.offset() + span.len()), diff --git a/crates/turborepo/tests/query.rs b/crates/turborepo/tests/query.rs index 46818a5775092..1b981978189fe 100644 --- a/crates/turborepo/tests/query.rs +++ b/crates/turborepo/tests/query.rs @@ -78,7 +78,7 @@ fn test_trace_on_monorepo() -> Result<(), anyhow::Error> { "query", "get `apps/my-app/index.ts` with dependencies" => "query { file(path: \"apps/my-app/index.ts\") { path dependencies { files { items { path } } errors { items { message } } } } }", "get `packages/utils/index.ts` with dependents" => "query { file(path: \"packages/utils/index.ts\") { path dependents { files { items { path } } errors { items { message } } } } }", - "get `packages/another/index.js` with dependents" => "query { file(path: \"packages/another/index.js\") { path dependents { files { items { path } } errors { items { message } } } } }", + "get `packages/another/index.js` with dependents" => "query { file(path: \"packages/another/index.jsx\") { path dependents { files { items { path } } errors { items { message } } } } }", ); Ok(()) diff --git a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`apps__my-app__index.ts`_with_dependencies_(npm@10.5.0).snap b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`apps__my-app__index.ts`_with_dependencies_(npm@10.5.0).snap index 62bfdc21cbe16..fe03645c20180 100644 --- a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`apps__my-app__index.ts`_with_dependencies_(npm@10.5.0).snap +++ b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`apps__my-app__index.ts`_with_dependencies_(npm@10.5.0).snap @@ -13,7 +13,16 @@ expression: query_output "path": "apps/my-app/types.ts" }, { - "path": "packages/another/index.js" + "path": "node_modules/@types/d3-scale/index.d.ts" + }, + { + "path": "node_modules/@types/d3-time/index.d.ts" + }, + { + "path": "packages/another/index.jsx" + }, + { + "path": "packages/module-package/my-module.mjs" }, { "path": "packages/utils/index.ts" diff --git a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__another__index.js`_with_dependents_(npm@10.5.0).snap b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__another__index.js`_with_dependents_(npm@10.5.0).snap index d5fb69de4071c..2f3b09cc59c5f 100644 --- a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__another__index.js`_with_dependents_(npm@10.5.0).snap +++ b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__another__index.js`_with_dependents_(npm@10.5.0).snap @@ -5,7 +5,7 @@ expression: query_output { "data": { "file": { - "path": "packages/another/index.js", + "path": "packages/another/index.jsx", "dependents": { "files": { "items": [ diff --git a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__utils__index.ts`_with_dependents_(npm@10.5.0).snap b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__utils__index.ts`_with_dependents_(npm@10.5.0).snap index 4e6e7ed0e3329..963a95763e00e 100644 --- a/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__utils__index.ts`_with_dependents_(npm@10.5.0).snap +++ b/crates/turborepo/tests/snapshots/query__turbo_trace_monorepo_get_`packages__utils__index.ts`_with_dependents_(npm@10.5.0).snap @@ -13,7 +13,7 @@ expression: query_output "path": "apps/my-app/index.ts" }, { - "path": "packages/another/index.js" + "path": "packages/another/index.jsx" } ] }, diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/index.ts b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/index.ts index 2b6c3821b3f30..96f84447bdb9d 100644 --- a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/index.ts +++ b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/index.ts @@ -1,4 +1,6 @@ import { useMyHook } from "utils/my-hook"; import ship from "utils"; -import { blackbeard } from "../../packages/another/index.js"; +import { blackbeard } from "../../packages/another/index.jsx"; import { Pirate } from "@/types.ts"; +import { walkThePlank } from "module-package"; +import type { ScalePoint } from "d3-scale"; diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/package.json b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/package.json index cf17ebf161c4a..b1ac06f3367f5 100644 --- a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/package.json +++ b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/package.json @@ -5,6 +5,7 @@ "maybefails": "exit 4" }, "dependencies": { - "utils": "*" + "utils": "*", + "@types/d3-scale": "^4.0.2" } } diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/types.ts b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/types.ts index b20bb0af42f65..8617784efc920 100644 --- a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/types.ts +++ b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/apps/my-app/types.ts @@ -1,4 +1,4 @@ -import { blackbeard } from "@/../../packages/another/index.js"; +import { blackbeard } from "@/../../packages/another/index.jsx"; export interface Pirate { ship: string; diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/another/index.js b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/another/index.jsx similarity index 100% rename from turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/another/index.js rename to turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/another/index.jsx diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/my-module.mjs b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/my-module.mjs new file mode 100644 index 0000000000000..5300c6eb7abe2 --- /dev/null +++ b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/my-module.mjs @@ -0,0 +1 @@ +export const walkThePlank = "walk the plank matey"; diff --git a/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/package.json b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/package.json new file mode 100644 index 0000000000000..79b674031cb6b --- /dev/null +++ b/turborepo-tests/integration/fixtures/turbo_trace_monorepo/packages/module-package/package.json @@ -0,0 +1,4 @@ +{ + "name": "module-package", + "module": "my-module.mjs" +} \ No newline at end of file