Skip to content

Commit 6f16614

Browse files
committed
Give each fake eval its own URL
If we always add sourceURL even when we have source maps, the native stack gets source mapped. This is needed when looking up a source location from an error string where the engine doesn't have access to the native reference. Async stacks are printed with native references but errors printed into the console are printed as strings and then reverse engineered back into source mapped urls. This preserves each eval as its own id. We use a separate protocol to exclude them from other Sources. Otherwise the real file and the fake file are listed right next to eachother.
1 parent d478a85 commit 6f16614

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

fixtures/flight/server/region.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ if (process.env.NODE_ENV === 'development') {
224224
// was already applied we also use this path.
225225
const sourceContent = await readFile(requestedFilePath, 'utf8');
226226
const lines = sourceContent.split('\n').length;
227+
const sourceURL = requestedFilePath.startsWith('/') ? 'file://' + requestedFilePath : requestedFilePath;
227228
map = {
228229
version: 3,
229-
sources: [requestedFilePath],
230+
sources: [sourceURL],
230231
sourcesContent: [sourceContent],
231232
// Note: This approach to mapping each line only lets you jump to each line
232233
// not jump to a column within a line. To do that, you need a proper source map

packages/react-client/src/ReactFlightClient.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,6 @@ function resolveErrorDev(
16141614
} else {
16151615
error = callStack();
16161616
}
1617-
// Overriding the stack isn't really necessary at this point and maybe we should just
1618-
// leave the native one. However, the native one gets printed unsource mapped with
1619-
// reportError. If that's fixed it might be better to use the native one.
1620-
error.stack = stack;
16211617
}
16221618

16231619
(error: any).digest = digest;
@@ -1705,6 +1701,7 @@ const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
17051701
? new Map()
17061702
: (null: any);
17071703

1704+
let fakeFunctionIdx = 0;
17081705
function createFakeFunction<T>(
17091706
name: string,
17101707
filename: string,
@@ -1723,20 +1720,29 @@ function createFakeFunction<T>(
17231720
// point to the original source.
17241721
let code;
17251722
if (line <= 1) {
1726-
code = '_=>' + ' '.repeat(col < 4 ? 0 : col - 4) + '_()\n' + comment + '\n';
1723+
code = '_=>' + ' '.repeat(col < 4 ? 0 : col - 4) + '_()\n' + comment;
17271724
} else {
17281725
code =
17291726
comment +
17301727
'\n'.repeat(line - 2) +
17311728
'_=>\n' +
17321729
' '.repeat(col < 1 ? 0 : col - 1) +
1733-
'_()\n';
1730+
'_()';
1731+
}
1732+
1733+
if (filename.startsWith('/')) {
1734+
// If the filename starts with `/` we assume that it is a file system file
1735+
// rather than relative to the current host. Since on the server fully qualified
1736+
// stack traces use the file path.
1737+
// TODO: What does this look like on Windows?
1738+
filename = 'file://' + filename;
17341739
}
17351740

17361741
if (sourceMap) {
1737-
code += '//# sourceMappingURL=' + sourceMap;
1742+
code += '\n//# sourceURL=rsc://server/' + filename + '?' + fakeFunctionIdx++;
1743+
code += '\n//# sourceMappingURL=' + sourceMap;
17381744
} else if (filename) {
1739-
code += '//# sourceURL=' + filename;
1745+
code += '\n//# sourceURL=' + filename;
17401746
}
17411747

17421748
let fn: FakeFunction<T>;

0 commit comments

Comments
 (0)