Skip to content

Commit

Permalink
add ./ to relative paths to make it easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 6, 2024
1 parent bb85aa2 commit 707756f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
67 changes: 30 additions & 37 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,44 +1124,37 @@ pub async fn project_trace_source(

let project_root_uri =
uri_from_file(project.container.project().project_root_path(), None).await? + "/";
let (file, original_file, is_internal) = if let Some(source_file) =
original_file.strip_prefix(&project_root_uri)
{
// Client code uses file://
(
get_relative_path_to(&current_directory_file_url, &original_file)
// TODO(sokra) remove this to include a ./ here to make it a relative path
.trim_start_matches("./")
.to_string(),
Some(source_file.to_string()),
false,
)
} else if let Some(source_file) =
original_file.strip_prefix(&*SOURCE_MAP_PREFIX_PROJECT)
{
// Server code uses turbopack://[project]
// TODO should this also be file://?
(
get_relative_path_to(
&current_directory_file_url,
&format!("{}{}", project_root_uri, source_file),
let (file, original_file, is_internal) =
if let Some(source_file) = original_file.strip_prefix(&project_root_uri) {
// Client code uses file://
(
get_relative_path_to(&current_directory_file_url, &original_file),
Some(source_file.to_string()),
false,
)
// TODO(sokra) remove this to include a ./ here to make it a relative path
.trim_start_matches("./")
.to_string(),
Some(source_file.to_string()),
false,
)
} else if let Some(source_file) = original_file.strip_prefix(SOURCE_MAP_PREFIX) {
// All other code like turbopack://[turbopack] is internal code
(source_file.to_string(), None, true)
} else {
bail!(
"Original file ({}) outside project ({})",
original_file,
project_root_uri
)
};
} else if let Some(source_file) =
original_file.strip_prefix(&*SOURCE_MAP_PREFIX_PROJECT)
{
// Server code uses turbopack://[project]
// TODO should this also be file://?
(
get_relative_path_to(
&current_directory_file_url,
&format!("{}{}", project_root_uri, source_file),
),
Some(source_file.to_string()),
false,
)
} else if let Some(source_file) = original_file.strip_prefix(SOURCE_MAP_PREFIX) {
// All other code like turbopack://[turbopack] is internal code
(source_file.to_string(), None, true)
} else {
bail!(
"Original file ({}) outside project ({})",
original_file,
project_root_uri
)
};

Ok(Some(StackFrame {
file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { Project, TurbopackStackFrame } from '../../../../build/swc/types'
import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file'
import { findSourceMap, type SourceMapPayload } from 'node:module'
import { pathToFileURL } from 'node:url'
import { isAbsolute } from 'node:path'

function shouldIgnorePath(modulePath: string): boolean {
return (
Expand Down Expand Up @@ -255,8 +256,13 @@ async function nativeTraceSource(

function relativeToCwd(file: string): string {
const relPath = path.relative(process.cwd(), url.fileURLToPath(file))
// TODO(sokra) include a ./ here to make it a relative path
return relPath
if (isAbsolute(relPath)) {
return relPath
}
if (relPath.startsWith('../')) {
return relPath
}
return './' + relPath
}

export async function createOriginalStackFrame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('non-root-project-monorepo', () => {
if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/source-maps-rsc/page.tsx (9:28) @ innerArrowFunction
"./app/source-maps-rsc/page.tsx (9:28) @ innerArrowFunction
7 | }
8 |
Expand All @@ -82,9 +82,9 @@ describe('non-root-project-monorepo', () => {
"<unknown>
[project]/apps/web/app/separate-file.ts [app-rsc] (ecmascript) (rsc://React/Server/file://<full-path>/apps/web/.next/server/chunks/ssr/apps_web_8d1c0a._.js (7:7)
innerFunction
app/source-maps-rsc/page.tsx (6:3)
./app/source-maps-rsc/page.tsx (6:3)
Page
app/source-maps-rsc/page.tsx (2:3)"
./app/source-maps-rsc/page.tsx (2:3)"
`)
} else {
// TODO the function name is incorrect
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('non-root-project-monorepo', () => {
if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
"./app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
> 1 | throw new Error('Expected error')
| ^
Expand All @@ -129,11 +129,11 @@ describe('non-root-project-monorepo', () => {
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"innerArrowFunction
app/source-maps-ssr/page.tsx (11:28)
./app/source-maps-ssr/page.tsx (11:28)
innerFunction
app/source-maps-ssr/page.tsx (8:3)
./app/source-maps-ssr/page.tsx (8:3)
Page
app/source-maps-ssr/page.tsx (4:3)"
./app/source-maps-ssr/page.tsx (4:3)"
`)
} else {
// TODO the function name should be hidden
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('non-root-project-monorepo', () => {
if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
"./app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
> 1 | throw new Error('Expected error')
| ^
Expand All @@ -182,11 +182,11 @@ describe('non-root-project-monorepo', () => {
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"innerArrowFunction
app/source-maps-client/page.tsx (16:28)
./app/source-maps-client/page.tsx (16:28)
innerFunction
app/source-maps-client/page.tsx (13:3)
./app/source-maps-client/page.tsx (13:3)
effectCallback
app/source-maps-client/page.tsx (7:5)"
./app/source-maps-client/page.tsx (7:5)"
`)
} else {
// TODO the function name should be hidden
Expand Down

0 comments on commit 707756f

Please sign in to comment.