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 a1dbc07 commit c2e25ab
Show file tree
Hide file tree
Showing 49 changed files with 380 additions and 188 deletions.
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
8 changes: 4 additions & 4 deletions packages/next/src/build/swc/generated-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ export interface StackFrame {
column?: number
methodName?: string
}
export function projectGetSourceMap(
project: { __napiType: 'Project' },
filePath: string
): Promise<string | null>
export function projectTraceSource(
project: { __napiType: 'Project' },
frame: StackFrame
Expand All @@ -285,6 +281,10 @@ export function projectGetSourceForAsset(
project: { __napiType: 'Project' },
filePath: string
): Promise<string | null>
export function projectGetSourceMap(
project: { __napiType: 'Project' },
filePath: string
): Promise<string | null>
/** Runs exit handlers for the project registered using the [`ExitHandler`] API. */
export function projectOnExit(project: { __napiType: 'Project' }): Promise<void>
export function rootTaskDispose(rootTask: { __napiType: 'RootTask' }): void
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
24 changes: 15 additions & 9 deletions turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ pub use ::include_dir::{
use anyhow::Result;
use turbo_tasks::{RcStr, Vc};

use crate::{embed::EmbeddedFileSystem, DiskFileSystem, FileSystem};
use crate::{embed::EmbeddedFileSystem, DiskFileSystem, FileSystem, UriScheme};

#[turbo_tasks::function]
pub async fn directory_from_relative_path(
uri_scheme: Vc<UriScheme>,
name: RcStr,
path: RcStr,
) -> Result<Vc<Box<dyn FileSystem>>> {
let disk_fs = DiskFileSystem::new(name, path, vec![]);
let disk_fs = DiskFileSystem::new(uri_scheme, name, path, vec![]);
disk_fs.await?.start_watching(None).await?;

Ok(Vc::upcast(disk_fs))
}

pub fn directory_from_include_dir(
uri_scheme: RcStr,
name: RcStr,
dir: &'static include_dir::Dir<'static>,
) -> Vc<Box<dyn FileSystem>> {
Vc::upcast(EmbeddedFileSystem::new(name, dir))
Vc::upcast(EmbeddedFileSystem::new(uri_scheme, name, dir))
}

/// Returns an embedded [Vc<Box<dyn FileSystem>>] for the given path.
Expand All @@ -37,39 +39,43 @@ pub fn directory_from_include_dir(
/// only the directory path will be embedded into the binary.
#[macro_export]
macro_rules! embed_directory {
($name:tt, $path:tt) => {{ // make sure the path contains `$CARGO_MANIFEST_DIR`
($uri_scheme:tt, $name:tt, $path:tt) => {{ // make sure the path contains `$CARGO_MANIFEST_DIR`
assert!($path.contains("$CARGO_MANIFEST_DIR"));
// make sure `CARGO_MANIFEST_DIR` is the only env variable in the path
assert!(!$path.replace("$CARGO_MANIFEST_DIR", "").contains('$'));

turbo_tasks_fs::embed_directory_internal!($name, $path)
turbo_tasks_fs::embed_directory_internal!($uri_scheme, $name, $path)
}};
}

#[cfg(feature = "dynamic_embed_contents")]
#[macro_export]
#[doc(hidden)]
macro_rules! embed_directory_internal {
($name:tt, $path:tt) => {{
($uri_scheme:tt, $name:tt, $path:tt) => {{
// make sure the types the `include_dir!` proc macro refers to are in scope
use turbo_tasks_fs::embed::include_dir;

let path = $path.replace("$CARGO_MANIFEST_DIR", env!("CARGO_MANIFEST_DIR"));

turbo_tasks_fs::embed::directory_from_relative_path($name.to_string(), path)
turbo_tasks_fs::embed::directory_from_relative_path(
$uri_scheme.into(),
$name.to_string(),
path,
)
}};
}

#[cfg(not(feature = "dynamic_embed_contents"))]
#[macro_export]
#[doc(hidden)]
macro_rules! embed_directory_internal {
($name:tt, $path:tt) => {{
($uri_scheme:tt, $name:tt, $path:tt) => {{
// make sure the types the `include_dir!` proc macro refers to are in scope
use turbo_tasks_fs::embed::include_dir;

static dir: include_dir::Dir<'static> = turbo_tasks_fs::embed::include_dir!($path);

turbo_tasks_fs::embed::directory_from_include_dir($name.into(), &dir)
turbo_tasks_fs::embed::directory_from_include_dir($uri_scheme.into(), $name.into(), &dir)
}};
}
4 changes: 3 additions & 1 deletion turbopack/crates/turbo-tasks-fs/src/embed/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use anyhow::{Context, Result};
use dunce::canonicalize;
use turbo_tasks::{RcStr, Vc};

use crate::{DiskFileSystem, File, FileContent, FileSystem};
use crate::{DiskFileSystem, File, FileContent, FileSystem, UriScheme};

#[turbo_tasks::function]
pub async fn content_from_relative_path(
uri_scheme: Vc<UriScheme>,
package_path: RcStr,
path: RcStr,
) -> Result<Vc<FileContent>> {
Expand All @@ -19,6 +20,7 @@ pub async fn content_from_relative_path(
let path = resolved_path.file_name().unwrap().to_str().unwrap();

let disk_fs = DiskFileSystem::new(
uri_scheme,
root_path.to_string_lossy().into(),
root_path.to_string_lossy().into(),
vec![],
Expand Down
23 changes: 21 additions & 2 deletions turbopack/crates/turbo-tasks-fs/src/embed/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ pub struct EmbeddedFileSystem {
name: RcStr,
#[turbo_tasks(trace_ignore)]
dir: &'static Dir<'static>,
uri_scheme: RcStr,
}

impl EmbeddedFileSystem {
pub(super) fn new(name: RcStr, dir: &'static Dir<'static>) -> Vc<EmbeddedFileSystem> {
EmbeddedFileSystem { name, dir }.cell()
pub(super) fn new(
uri_scheme: RcStr,
name: RcStr,
dir: &'static Dir<'static>,
) -> Vc<EmbeddedFileSystem> {
EmbeddedFileSystem {
name,
dir,
uri_scheme,
}
.cell()
}
}

Expand Down Expand Up @@ -100,6 +110,15 @@ impl FileSystem for EmbeddedFileSystem {

Ok(FileMeta::default().cell())
}

#[turbo_tasks::function(fs)]
async fn file_uri(&self, fs_path: Vc<FileSystemPath>) -> Result<Vc<RcStr>> {
let path_str = &*fs_path.await?.path;
Ok(Vc::cell(RcStr::from(format!(
"{}://[{}]/{}",
self.uri_scheme, self.name, path_str
))))
}
}

#[turbo_tasks::value_impl]
Expand Down
Loading

0 comments on commit c2e25ab

Please sign in to comment.