Skip to content

Commit

Permalink
Replace turbopack://[project]/... sourcemap uris with file://...
Browse files Browse the repository at this point in the history
This makes working with devtools more straightforward, reduces our own overhead when tracing stack frames in the error overlay, etc.

Generated code from Turbopack or Next.js still use `turbopack://[turbopack]` or `turbopack://[next]` respectively.

Test Plan: CI. Update snapshots.
  • Loading branch information
wbinnssmith committed Oct 21, 2024
1 parent 2b860fd commit b7b45e1
Show file tree
Hide file tree
Showing 51 changed files with 399 additions and 200 deletions.
20 changes: 15 additions & 5 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,15 +1099,25 @@ pub async fn project_trace_source(
}
};

let Some(source_file) = original_file.strip_prefix(SOURCE_MAP_PREFIX) else {
bail!("Original file ({}) outside project", original_file)
};
let project_path_uri = format!(
"{}/",
&*project
.container
.project()
.project_path()
.fs()
.root()
.uri()
.await?
);

let (source_file, is_internal) =
if let Some(source_file) = source_file.strip_prefix("[project]/") {
if let Some(source_file) = original_file.strip_prefix(&project_path_uri) {
(source_file, false)
} else {
} else if let Some(source_file) = original_file.strip_prefix(SOURCE_MAP_PREFIX) {
(source_file, true)
} else {
bail!("Original file ({}) outside project", original_file)
};

Ok(Some(StackFrame {
Expand Down
31 changes: 24 additions & 7 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use turbo_tasks::{
TransientInstance, TryFlatJoinIterExt, Value, Vc,
};
use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, VirtualFileSystem};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, UriScheme, VirtualFileSystem};
use turbopack::{
evaluate_context::node_build_environment, transition::TransitionOptions, ModuleAssetContext,
};
Expand Down Expand Up @@ -524,6 +524,15 @@ impl Issue for ConflictIssue {

#[turbo_tasks::value_impl]
impl Project {
#[turbo_tasks::function]
pub async fn uri_scheme(&self) -> Result<Vc<UriScheme>> {
Ok(match &*self.mode.await? {
NextMode::Build => UriScheme::Custom("turbopack".into()),
NextMode::Development => UriScheme::File,
}
.cell())
}

#[turbo_tasks::function]
pub async fn app_project(self: Vc<Self>) -> Result<Vc<OptionAppProject>> {
let app_dir = find_app_dir(self.project_path()).await?;
Expand All @@ -539,12 +548,14 @@ impl Project {
}

#[turbo_tasks::function]
fn project_fs(&self) -> Vc<DiskFileSystem> {
DiskFileSystem::new(
async fn project_fs(self: Vc<Self>) -> Result<Vc<DiskFileSystem>> {
let this = &*self.await?;
Ok(DiskFileSystem::new(
self.uri_scheme(),
PROJECT_FILESYSTEM_NAME.into(),
self.root_path.clone(),
this.root_path.clone(),
vec![],
)
))
}

#[turbo_tasks::function]
Expand All @@ -554,8 +565,14 @@ impl Project {
}

#[turbo_tasks::function]
pub fn output_fs(&self) -> Vc<DiskFileSystem> {
DiskFileSystem::new("output".into(), self.project_path.clone(), vec![])
pub async fn output_fs(self: Vc<Self>) -> Result<Vc<DiskFileSystem>> {
let this = &*self.await?;
Ok(DiskFileSystem::new(
self.uri_scheme(),
"output".into(),
this.project_path.clone(),
vec![],
))
}

#[turbo_tasks::function]
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/embed_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const VIRTUAL_PACKAGE_NAME: &str = "@vercel/turbopack-next";
#[turbo_tasks::function]
pub(crate) fn next_js_fs() -> Vc<Box<dyn FileSystem>> {
// [TODO]: macro need to be refactored to be used via turbopack-binding
turbo_tasks_fs::embed_directory!("next", "$CARGO_MANIFEST_DIR/js/src")
turbo_tasks_fs::embed_directory!("turbopack", "next", "$CARGO_MANIFEST_DIR/js/src")
}

#[turbo_tasks::function]
Expand Down
3 changes: 2 additions & 1 deletion crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use turbo_tasks_env::{CommandLineProcessEnv, ProcessEnv};
use turbo_tasks_fetch::{fetch, HttpResponseBody};
use turbo_tasks_fs::{
json::parse_json_with_source_context, DiskFileSystem, File, FileContent, FileSystem,
FileSystemPath,
FileSystemPath, UriScheme,
};
use turbopack::evaluate_context::node_evaluate_asset_context;
use turbopack_core::{
Expand Down Expand Up @@ -637,6 +637,7 @@ async fn get_mock_stylesheet(
) -> Result<Option<Vc<RcStr>>> {
let response_path = Path::new(&mocked_responses_path);
let mock_fs = Vc::upcast::<Box<dyn FileSystem>>(DiskFileSystem::new(
UriScheme::Custom("test".into()).cell(),
"mock".into(),
response_path
.parent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describeVariants.each(['turbo'])('experimental-lightningcss', () => {
expect($('p').text()).toBe('hello world')
// swc_css does not include `-module` in the class name, while lightningcss does.
expect($('p').attr('class')).toBe(
'search-keyword style-module__hlQ3RG__blue'
'search-keyword style-module__Qo74fG__blue'
)
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/development/sass-error/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('app dir - css', () => {
47 | }
48 |
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")' at [project]/app/global.scss.css:0:884"
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")' at app/global.scss.css:0:884"
`)
})
}
Expand Down
20 changes: 10 additions & 10 deletions test/integration/css-modules/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Basic CSS Module Support', () => {
if (process.env.TURBOPACK) {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`".index-module__VJHdSq__redText{color:red}"`)
).toMatchInlineSnapshot(`".index-module__NoKL0W__redText{color:red}"`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
Expand All @@ -83,7 +83,7 @@ describe('Basic CSS Module Support', () => {

if (process.env.TURBOPACK) {
expect($('#verify-red').attr('class')).toMatchInlineSnapshot(
`"index-module__VJHdSq__redText"`
`"index-module__NoKL0W__redText"`
)
} else {
expect($('#verify-red').attr('class')).toMatchInlineSnapshot(
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('3rd Party CSS Module Support', () => {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(
`".index-module__jAE1EW__foo{position:relative}.index-module__jAE1EW__foo .bar,.index-module__jAE1EW__foo .baz{height:100%;overflow:hidden}.index-module__jAE1EW__foo .lol{width:80%}.index-module__jAE1EW__foo>.lel{width:80%}"`
`".index-module__Cwv4sq__foo{position:relative}.index-module__Cwv4sq__foo .bar,.index-module__Cwv4sq__foo .baz{height:100%;overflow:hidden}.index-module__Cwv4sq__foo .lol{width:80%}.index-module__Cwv4sq__foo>.lel{width:80%}"`
)
} else {
expect(
Expand All @@ -162,7 +162,7 @@ describe('3rd Party CSS Module Support', () => {

if (process.env.TURBOPACK) {
expect($('#verify-div').attr('class')).toMatchInlineSnapshot(
`"index-module__jAE1EW__foo"`
`"index-module__Cwv4sq__foo"`
)
} else {
expect($('#verify-div').attr('class')).toMatchInlineSnapshot(
Expand Down Expand Up @@ -377,7 +377,7 @@ describe('Valid CSS Module Usage from within node_modules', () => {
const cssPreload = $('#nm-div')
if (process.env.TURBOPACK) {
expect(cssPreload.text()).toMatchInlineSnapshot(
`"{"message":"Why hello there","default":{"message":"Why hello there"}} {"redText":"index-module__kwuKnq__redText","default":{"redText":"index-module__kwuKnq__redText"}}"`
`"{"message":"Why hello there","default":{"message":"Why hello there"}} {"redText":"index-module__xkL-Iq__redText","default":{"redText":"index-module__xkL-Iq__redText"}}"`
)
} else {
expect(cssPreload.text()).toMatchInlineSnapshot(
Expand All @@ -401,7 +401,7 @@ describe('Valid CSS Module Usage from within node_modules', () => {
if (process.env.TURBOPACK) {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`".index-module__kwuKnq__redText{color:red}"`)
).toMatchInlineSnapshot(`".index-module__xkL-Iq__redText{color:red}"`)
} else {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
Expand Down Expand Up @@ -522,7 +522,7 @@ describe('CSS Module Composes Usage (Basic)', () => {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(
`".index-module__QppuLW__className{background:red;color:#ff0}.index-module__QppuLW__subClass{background:#00f;}"`
`".index-module__LaIBiq__className{background:red;color:#ff0}.index-module__LaIBiq__subClass{background:#00f;}"`
)
} else {
expect(
Expand Down Expand Up @@ -643,7 +643,7 @@ describe('Dynamic Route CSS Module Usage', () => {
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(
`".index-module__Iury9a__home{background:red}"`
`".index-module__oDjVyW__home{background:red}"`
)
} else {
expect(
Expand Down Expand Up @@ -708,10 +708,10 @@ describe('Catch-all Route CSS Module Usage', () => {
if (process.env.TURBOPACK) {
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim())
.toMatchInlineSnapshot(`
".index-module___rV4CG__home{background:red}
".index-module__oqhyRW__home{background:red}
.\\35 5css-module__qe774W__home{color:green}"
.\\35 5css-module__zUCz1G__home{color:green}"
`)
} else {
expect(
Expand Down
8 changes: 7 additions & 1 deletion turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use turbo_tasks::{
};
use turbo_tasks_fs::{
glob::Glob, DirectoryEntry, DiskFileSystem, FileSystem, FileSystemPath, ReadGlobResult,
UriScheme,
};
use turbopack::{
emit_asset, emit_with_completion, module_options::ModuleOptionsContext, rebase::RebasedAsset,
Expand Down Expand Up @@ -188,7 +189,12 @@ impl Args {
}

async fn create_fs(name: &str, root: &str, watch: bool) -> Result<Vc<Box<dyn FileSystem>>> {
let fs = DiskFileSystem::new(name.into(), root.into(), vec![]);
let fs = DiskFileSystem::new(
UriScheme::Custom("turbopack".into()).cell(),
name.into(),
root.into(),
vec![],
);
if watch {
fs.await?.start_watching(None).await?;
} else {
Expand Down
16 changes: 11 additions & 5 deletions turbopack/crates/turbo-tasks-fetch/tests/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use turbo_tasks::Vc;
use turbo_tasks_fetch::{fetch, FetchErrorKind};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, UriScheme};
use turbo_tasks_testing::{register, run, Registration};
use turbopack_core::issue::{Issue, IssueSeverity, StyledString};

Expand Down Expand Up @@ -120,7 +120,7 @@ async fn errors_on_failed_connection() {
assert_eq!(*err.kind.await?, FetchErrorKind::Connect);
assert_eq!(*err.url.await?, url);

let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context());
let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_test_issue_context());
assert_eq!(*issue.severity().await?, IssueSeverity::Error);
assert_eq!(*issue.description().await?.unwrap().await?, StyledString::Text("There was an issue establishing a connection while requesting https://doesnotexist/foo.woff.".into()));
anyhow::Ok(())
Expand All @@ -145,7 +145,7 @@ async fn errors_on_404() {
assert!(matches!(*err.kind.await?, FetchErrorKind::Status(404)));
assert_eq!(*err.url.await?, resource_url);

let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context());
let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_test_issue_context());
assert_eq!(*issue.severity().await?, IssueSeverity::Error);
assert_eq!(
*issue.description().await?.unwrap().await?,
Expand All @@ -163,6 +163,12 @@ async fn errors_on_404() {
.unwrap()
}

fn get_issue_context() -> Vc<FileSystemPath> {
DiskFileSystem::new("root".into(), "/".into(), vec![]).root()
fn get_test_issue_context() -> Vc<FileSystemPath> {
DiskFileSystem::new(
UriScheme::Custom("test".into()).cell(),
"root".into(),
"/".into(),
vec![],
)
.root()
}
9 changes: 7 additions & 2 deletions turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sha2::{Digest, Sha256};
use turbo_tasks::{util::FormatDuration, RcStr, ReadConsistency, TurboTasks, UpdateInfo, Vc};
use turbo_tasks_fs::{
register, DirectoryContent, DirectoryEntry, DiskFileSystem, FileContent, FileSystem,
FileSystemPath,
FileSystemPath, UriScheme,
};
use turbo_tasks_memory::MemoryBackend;

Expand All @@ -31,7 +31,12 @@ async fn main() -> Result<()> {
let task = tt.spawn_root_task(|| {
Box::pin(async {
let root = current_dir().unwrap().to_str().unwrap().into();
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
let disk_fs = DiskFileSystem::new(
UriScheme::Custom("turbopack".into()).cell(),
"project".into(),
root,
vec![],
);
disk_fs.await?.start_watching(None).await?;

// Smart Pointer cast
Expand Down
9 changes: 7 additions & 2 deletions turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sha2::{Digest, Sha256};
use turbo_tasks::{util::FormatDuration, RcStr, ReadConsistency, TurboTasks, UpdateInfo, Vc};
use turbo_tasks_fs::{
glob::Glob, register, DirectoryEntry, DiskFileSystem, FileContent, FileSystem, FileSystemPath,
ReadGlobResult,
ReadGlobResult, UriScheme,
};
use turbo_tasks_memory::MemoryBackend;

Expand All @@ -28,7 +28,12 @@ async fn main() -> Result<()> {
let task = tt.spawn_root_task(|| {
Box::pin(async {
let root = current_dir().unwrap().to_str().unwrap().into();
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
let disk_fs = DiskFileSystem::new(
UriScheme::Custom("turbopack".into()).cell(),
"project".into(),
root,
vec![],
);
disk_fs.await?.start_watching(None).await?;

// Smart Pointer cast
Expand Down
Loading

0 comments on commit b7b45e1

Please sign in to comment.