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

fix: use oxc-resolver hot fix for symlinks #9302

Merged
merged 11 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/turbo-trace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
camino.workspace = true
clap = { version = "4.5.17", features = ["derive"] }
miette = { workspace = true, features = ["fancy"] }
oxc_resolver = "1.11.0"
oxc_resolver = { git = "https://github.com/nicholaslyang/oxc-resolver.git", branch = "symlink-hotfix" }
swc_common = { workspace = true }
swc_ecma_ast = { workspace = true }
swc_ecma_parser = { workspace = true }
Expand Down
49 changes: 33 additions & 16 deletions crates/turborepo-lib/src/query/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use async_graphql::{Object, SimpleObject};
use camino::Utf8PathBuf;
use itertools::Itertools;
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax};
Expand Down Expand Up @@ -68,10 +69,11 @@ impl File {
}
}

#[derive(SimpleObject, Debug)]
#[derive(SimpleObject, Debug, Default)]
pub struct TraceError {
message: String,
path: Option<String>,
import: Option<String>,
start: Option<usize>,
end: Option<usize>,
}
Expand All @@ -83,27 +85,32 @@ impl From<turbo_trace::TraceError> for TraceError {
turbo_trace::TraceError::FileNotFound(file) => TraceError {
message,
path: Some(file.to_string()),
start: None,
end: None,
..Default::default()
},
turbo_trace::TraceError::PathEncoding(_) => TraceError {
message,
path: None,
start: None,
end: None,
..Default::default()
},
turbo_trace::TraceError::RootFile(path) => TraceError {
message,
path: Some(path.to_string()),
start: None,
end: None,
},
turbo_trace::TraceError::Resolve { span, text } => TraceError {
message,
path: Some(text.name().to_string()),
start: Some(span.offset()),
end: Some(span.offset() + span.len()),
..Default::default()
},
turbo_trace::TraceError::Resolve { span, text } => {
let import = text
.inner()
.read_span(&span, 1, 1)
.ok()
.map(|s| String::from_utf8_lossy(s.data()).to_string());

TraceError {
message,
import,
path: Some(text.name().to_string()),
start: Some(span.offset()),
end: Some(span.offset() + span.len()),
}
}
}
}
}
Expand Down Expand Up @@ -147,11 +154,21 @@ impl File {
Ok(self.path.to_string())
}

async fn dependencies(&self, depth: Option<usize>) -> TraceResult {
async fn dependencies(&self, depth: Option<usize>, ts_config: Option<String>) -> TraceResult {
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a behavior change where we now respect tsconfig? Maybe update PR description to include functionality this gains us? (I'm curious and want to know, but can't grok it from this PR)

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 tsconfig is necessary for doing prefix resolution, i.e. @/components/ui. tsconfig loading is what's broken in oxc-resolver

let ts_config = match ts_config {
Some(ts_config) => Some(Utf8PathBuf::from(ts_config)),
None => self
.path
.ancestors()
.skip(1)
.find(|p| p.join_component("tsconfig.json").exists())
.map(|p| p.as_path().to_owned()),
};

let tracer = Tracer::new(
self.run.repo_root().to_owned(),
vec![self.path.clone()],
None,
ts_config,
);

let mut result = tracer.trace(depth);
Expand Down
3 changes: 3 additions & 0 deletions turborepo-tests/integration/fixtures/oxc_repro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
/node_modules
.turbo
30 changes: 30 additions & 0 deletions turborepo-tests/integration/fixtures/oxc_repro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# oxc symlink bug reproduction

The bug occurs when a symlink is nested inside a directory that is also symlinked.
This occurs with pnpm since it recreates a conventional node_modules structure using
a content addressed store and symlinks.

Here's the setup:

- `apps/web/nm/@repo/typescript-config` is a symlink pointing to `tooling/typescript-config` (imagine `typescript-config` is a workspace package and symlinked into `apps/web`'s node modules)
- `tooling/typescript-config/index.js` is a _relative_ symlink pointing to `../../nm/index.js`
- Therefore, `apps/web/nm/@repo/typescript-config/index.js` is resolved as:

```
apps/web/nm/@repo/typescript-config/index.js
-> tooling/typescript-config/index.js
-> tooling/typescript-config/../../nm/index.js
-> nm/index.js
```

However, when oxc resolves this, it does not do the first resolution, so we get:

```
apps/web/nm/@repo/typescript-config/index.js
-> apps/web/nm/@repo/typescript-config/../../nm/index.js
-> apps/web/nm/nm/index.js
```

You can validate this by running `node main.mjs`, which attempts to resolve
both `apps/web/nm/@repo/typescript-config/index.js` and `apps/web/nm/@repo/index.js`.
The first fails while the second succeeds.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = "10";
1 change: 1 addition & 0 deletions turborepo-tests/integration/fixtures/oxc_repro/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from "./apps/web/nm/@repo/typescript-config/index.js";
Empty file.
14 changes: 14 additions & 0 deletions turborepo-tests/integration/fixtures/oxc_repro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "oxc-repro",
"version": "1.0.0",
"description": "",
"workspaces": ["c"],
"packageManager": "npm@10.5.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
24 changes: 24 additions & 0 deletions turborepo-tests/integration/tests/trace-double-symlink.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Setup
$ . ${TESTDIR}/../../helpers/setup_integration_test.sh oxc_repro

$ ${TURBO} query "query { file(path: \"./index.js\") { path dependencies { files { items { path } } errors { items { message import } } } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"file": {
"path": "index.js",
"dependencies": {
"files": {
"items": [
{
"path": "nm/index.js"
}
]
},
"errors": {
"items": []
}
}
}
}
}
Loading