Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trace): resolve incorrect extension #9486

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions crates/turbo-trace/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc};

use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use globwalk::WalkType;
use miette::{Diagnostic, Report, SourceSpan};
use oxc_resolver::{
Expand Down Expand Up @@ -67,7 +67,7 @@
}

impl TraceResult {
pub fn emit_errors(&self) {

Check warning on line 70 in crates/turbo-trace/src/tracer.rs

View workflow job for this annotation

GitHub Actions / Rust lints

method `emit_errors` is never used

Check warning on line 70 in crates/turbo-trace/src/tracer.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

method `emit_errors` is never used

Check warning on line 70 in crates/turbo-trace/src/tracer.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

method `emit_errors` is never used

Check warning on line 70 in crates/turbo-trace/src/tracer.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

method `emit_errors` is never used

Check warning on line 70 in crates/turbo-trace/src/tracer.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on windows

method `emit_errors` is never used
let handler = Handler::with_tty_emitter(
ColorConfig::Auto,
true,
Expand Down Expand Up @@ -211,15 +211,36 @@
debug!("built in: {:?}", err);
}
Err(err) => {
// Try to resolve the import as a type import via `@/types/<import>`
let type_package = format!("@types/{}", import);
let resolved_type_import = resolver
.resolve(file_path, type_package.as_str())
if !import.starts_with(".") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a check against ./?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I don't think packages can start with .?

// Try to resolve the import as a type import via `@/types/<import>`
let type_package = format!("@types/{}", import);
debug!("trying to resolve type import: {}", type_package);
let resolved_type_import = resolver
.resolve(file_dir, type_package.as_str())
.ok()
.and_then(|resolved| resolved.into_path_buf().try_into().ok());

if let Some(resolved_type_import) = resolved_type_import {
debug!("resolved type import succeeded");
files.push(resolved_type_import);
continue;
}
}

// Also try without the extension just in case the wrong extension is used
let without_extension = Utf8Path::new(import).with_extension("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just calling out that this will convert something like types.d.ts to types.d source, is that the desired behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I originally had an explicit check for types.d.ts that stripped it all the way back to types but when I ran this on the other repo, that didn't fix any errors. I suspect oxc-resolver also treats types.d as the name, so this should work just fine

debug!(
"trying to resolve extensionless import: {}",
without_extension
);
let resolved_extensionless_import = resolver
.resolve(file_dir, without_extension.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);
if let Some(resolved_extensionless_import) = resolved_extensionless_import {
debug!("resolved extensionless import succeeded");
files.push(resolved_extensionless_import);
continue;
}

Expand Down
1 change: 1 addition & 0 deletions crates/turborepo/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn test_trace() -> Result<(), anyhow::Error> {
"get `import_value_and_type.ts` with type dependencies" => "query { file(path: \"import_value_and_type.ts\") { path dependencies(importType: TYPES) { files { items { path } } } } }",
"get `import_value_and_type.ts` with value dependencies" => "query { file(path: \"import_value_and_type.ts\") { path dependencies(importType: VALUES) { files { items { path } } } } }",
"get `export_conditions` with dependencies" => "query { file(path: \"export_conditions.js\") { path dependencies(depth: 1) { files { items { path } } } } }",
"get `incorrect_extension.mjs` with dependencies" => "query { file(path: \"incorrect_extension.mjs\") { path dependencies(depth: 1) { files { items { path } } } } }",
);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/turborepo/tests/query.rs
expression: query_output
---
{
"data": {
"file": {
"path": "incorrect_extension.mjs",
"dependencies": {
"files": {
"items": [
{
"path": "bar.js"
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { bar } from "./bar.cjs";
Loading