Skip to content

Commit

Permalink
feat(trace): resolve incorrect extension (#9486)
Browse files Browse the repository at this point in the history
### Description

Invalid extensions are unfortunately too common in imports. Therefore if
we can't resolve with the extension, we strip it and try again.

### Testing Instructions

Added a test where we try to import with the wrong extension
  • Loading branch information
NicholasLYang authored Nov 21, 2024
1 parent b6ddde3 commit 0a2ef17
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
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 @@ -211,15 +211,36 @@ impl Tracer {
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(".") {
// 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("");
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";

0 comments on commit 0a2ef17

Please sign in to comment.