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 18, 2024
1 parent a1dbc07 commit df05a43
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 37 deletions.
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
19 changes: 12 additions & 7 deletions turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub async fn directory_from_relative_path(
}

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 +38,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)
}};
}
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
17 changes: 17 additions & 0 deletions turbopack/crates/turbo-tasks-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ pub trait FileSystem: ValueToString {
target: Vc<LinkContent>,
) -> Vc<Completion>;
fn metadata(self: Vc<Self>, fs_path: Vc<FileSystemPath>) -> Vc<FileMeta>;
fn file_uri(self: Vc<Self>, _fs_path: Vc<FileSystemPath>) -> Vc<RcStr> {
unimplemented!();
}
}

#[turbo_tasks::value(cell = "new", eq = "manual")]
Expand Down Expand Up @@ -886,6 +889,14 @@ impl FileSystem for DiskFileSystem {

Ok(FileMeta::cell(meta.into()))
}

#[turbo_tasks::function(fs)]
async fn file_uri(&self, fs_path: Vc<FileSystemPath>) -> Result<Vc<RcStr>> {
Ok(Vc::cell(RcStr::from(format!(
"file://{}",
sys_to_unix(&self.to_sys_path(fs_path).await?.to_string_lossy())
))))
}
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -1301,6 +1312,12 @@ impl FileSystemPath {
Cow::Owned(path) => path.cell(),
})
}

#[turbo_tasks::function]
pub async fn uri(self: Vc<Self>) -> Result<Vc<RcStr>> {
let this = self.await?;
Ok(this.fs.file_uri(self))
}
}

impl Display for FileSystemPath {
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-cli/src/embed_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use turbo_tasks_fs::{embed_directory, FileContent, FileSystem, FileSystemPath};

#[turbo_tasks::function]
fn embed_fs() -> Vc<Box<dyn FileSystem>> {
embed_directory!("turbopack-cli", "$CARGO_MANIFEST_DIR/js/src")
embed_directory!("turbopack", "turbopack-cli", "$CARGO_MANIFEST_DIR/js/src")
}

#[turbo_tasks::function]
Expand Down
7 changes: 3 additions & 4 deletions turbopack/crates/turbopack-core/src/source_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,16 @@ impl SourceMap {
) -> Result<(Arc<str>, Arc<str>)> {
Ok(
if let Some(path) = *origin.parent().try_join((&*source_request).into()).await? {
let path_str = path.to_string().await?;
let source = format!("{SOURCE_MAP_PREFIX}{}", path_str);
let source = &*path.uri().await?;
let source_content = if let Some(source_content) = source_content {
source_content
} else if let FileContent::Content(file) = &*path.read().await? {
let text = file.content().to_str()?;
text.to_string().into()
} else {
format!("unable to read source {path_str}").into()
format!("unable to read source {source}").into()
};
(source.into(), source_content)
(source.to_string().into(), source_content)
} else {
let origin_str = origin.to_string().await?;
static INVALID_REGEX: Lazy<Regex> =
Expand Down
5 changes: 4 additions & 1 deletion turbopack/crates/turbopack-css/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub struct InlineSourcesContentConfig {}
impl SourceMapGenConfig for InlineSourcesContentConfig {
fn file_name_to_source(&self, f: &FileName) -> String {
match f {
FileName::Custom(s) => format!("{SOURCE_MAP_PREFIX}{s}"),
FileName::Custom(s) => {
dbg!(s);
format!("{SOURCE_MAP_PREFIX}{s}")
}
_ => f.to_string(),
}
}
Expand Down
23 changes: 15 additions & 8 deletions turbopack/crates/turbopack-css/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use turbopack_core::{
source::Source,
source_map::{GenerateSourceMap, OptionSourceMap},
source_pos::SourcePos,
SOURCE_MAP_PREFIX,
};
use turbopack_swc_utils::emitter::IssueEmitter;

Expand All @@ -66,6 +65,7 @@ use crate::{

// Capture up until the first "."
static BASENAME_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[^.]*").unwrap());
static URI_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(.*)\:\/\/.*").unwrap());

#[derive(Debug)]
pub enum StyleSheetLike<'i, 'o> {
Expand Down Expand Up @@ -493,7 +493,7 @@ pub async fn parse_css(
async move {
let content = source.content();
let fs_path = source.ident().path();
let ident_str = &*source.ident().to_string().await?;
let file_uri = &*source.ident().path().uri().await?;
Ok(match &*content.await? {
AssetContent::Redirect { .. } => ParseCssResult::Unparseable.cell(),
AssetContent::File(file_content) => match &*file_content.await? {
Expand All @@ -505,7 +505,7 @@ pub async fn parse_css(
*file_content,
string.into_owned(),
fs_path,
ident_str,
file_uri,
source,
origin,
import_context,
Expand All @@ -526,7 +526,7 @@ async fn process_content(
content_vc: Vc<FileContent>,
code: String,
fs_path_vc: Vc<FileSystemPath>,
filename: &str,
file_uri: &str,
source: Vc<Box<dyn Source>>,
origin: Vc<Box<dyn ResolveOrigin>>,
import_context: Vc<ImportContext>,
Expand Down Expand Up @@ -564,7 +564,7 @@ async fn process_content(

_ => None,
},
filename: filename.to_string(),
filename: file_uri.to_string(),
error_recovery: true,
..Default::default()
};
Expand Down Expand Up @@ -656,7 +656,7 @@ async fn process_content(
)),
);

let fm = cm.new_source_file(FileName::Custom(filename.to_string()).into(), code.clone());
let fm = cm.new_source_file(FileName::Custom(file_uri.to_string()).into(), code.clone());
let mut errors = vec![];

let ss = swc_core::css::parser::parse_file(
Expand Down Expand Up @@ -706,7 +706,7 @@ async fn process_content(
.context("Must include basename preceding .")?
.as_str();
// Truncate this as u32 so it's formatted as 8-character hex in the suffix below
let path_hash = turbo_tasks_hash::hash_xxh3_hash64(filename) as u32;
let path_hash = turbo_tasks_hash::hash_xxh3_hash64(file_uri) as u32;

Some(SwcCssModuleMode {
basename: basename.to_string(),
Expand Down Expand Up @@ -989,7 +989,14 @@ impl GenerateSourceMap for ParseCssResultSourceMap {
let mut builder = SourceMapBuilder::new(None);

for src in source_map.get_sources() {
builder.add_source(&format!("{SOURCE_MAP_PREFIX}{src}"));
if URI_RE.is_match(src) {
builder.add_source(src);
} else {
// This is a filepath that has been trimmed by Parcel
// to be made relative. Restore the file:/// prefix.
// TODO: Allow Parcel's sourcemap to opt-out of this behavior.
builder.add_source(&format!("file:///{src}"));
}
}

for (idx, content) in source_map.get_sources_content().iter().enumerate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use turbopack_ecmascript::StaticEcmascriptCode;

#[turbo_tasks::function]
pub fn embed_fs() -> Vc<Box<dyn FileSystem>> {
embed_directory!("turbopack", "$CARGO_MANIFEST_DIR/js/src")
embed_directory!("turbopack", "$CARGO_MANIFEST_DIR/js/src", "turbopack")
}

#[turbo_tasks::function]
Expand Down
18 changes: 7 additions & 11 deletions turbopack/crates/turbopack-ecmascript/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use turbopack_core::{
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
source::Source,
source_map::{GenerateSourceMap, OptionSourceMap, SourceMap},
SOURCE_MAP_PREFIX,
};
use turbopack_swc_utils::emitter::IssueEmitter;

Expand Down Expand Up @@ -139,12 +138,7 @@ struct InlineSourcesContentConfig {}

impl SourceMapGenConfig for InlineSourcesContentConfig {
fn file_name_to_source(&self, f: &FileName) -> String {
match f {
FileName::Custom(s) => {
format!("{SOURCE_MAP_PREFIX}{s}")
}
_ => f.to_string(),
}
f.to_string()
}

fn inline_sources_content(&self, _f: &FileName) -> bool {
Expand Down Expand Up @@ -180,7 +174,9 @@ async fn parse_internal(
let content = source.content();
let fs_path_vc = source.ident().path();
let fs_path = &*fs_path_vc.await?;
let ident = &*source.ident().to_string().await?;

let file_uri = fs_path_vc.uri().await?;

let file_path_hash = hash_xxh3_hash64(&*source.ident().to_string().await?) as u128;
let ty = ty.into_value();
let content = match content.await {
Expand Down Expand Up @@ -210,7 +206,7 @@ async fn parse_internal(
string.into_owned(),
fs_path_vc,
fs_path,
ident,
&file_uri,
file_path_hash,
source,
ty,
Expand Down Expand Up @@ -250,7 +246,7 @@ async fn parse_file_content(
string: String,
fs_path_vc: Vc<FileSystemPath>,
fs_path: &FileSystemPath,
ident: &str,
file_uri: &str,
file_path_hash: u128,
source: Vc<Box<dyn Source>>,
ty: EcmascriptModuleAssetType,
Expand Down Expand Up @@ -278,7 +274,7 @@ async fn parse_file_content(

let mut result = WrapFuture::new(
async {
let file_name = FileName::Custom(ident.to_string());
let file_name = FileName::Custom(file_uri.to_string());
let fm = source_map.new_source_file(file_name.clone().into(), string);

let comments = SwcComments::default();
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-node/src/embed_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use turbo_tasks_fs::{embed_directory, FileContent, FileSystem, FileSystemPath};

#[turbo_tasks::function]
pub fn embed_fs() -> Vc<Box<dyn FileSystem>> {
embed_directory!("turbopack-node", "$CARGO_MANIFEST_DIR/js/src")
embed_directory!("turbopack", "turbopack-node", "$CARGO_MANIFEST_DIR/js/src")
}

#[turbo_tasks::function]
Expand Down

0 comments on commit df05a43

Please sign in to comment.