Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve source mapping of console output and errors #47388

Merged
merged 34 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
47a50c5
fix bug in magic identifier decoding
sokra Mar 21, 2023
04bac9f
use once_cell from workspace dep
sokra Mar 21, 2023
d33cb99
disable source map references for node.js rendering
sokra Mar 21, 2023
0421117
format and add IPC output end markers
sokra Mar 21, 2023
cc684a0
update import
sokra Mar 21, 2023
364837a
pass project path for source mapping
sokra Mar 21, 2023
5c4778b
test with turbo branch
sokra Mar 22, 2023
b533045
update snapshots
sokra Mar 22, 2023
ade2658
add test case
sokra Mar 22, 2023
20cde19
allow to start an integration test
sokra Mar 22, 2023
3ad04d6
review
sokra Mar 22, 2023
d438908
improve test case
sokra Mar 22, 2023
9a28eb3
rename en/decode -> (un)mangle
sokra Mar 22, 2023
19712b0
constants
sokra Mar 22, 2023
38b6028
improve test case
sokra Mar 22, 2023
1d3dbe8
fix react-dev-overlay replacement in monorepos
sokra Mar 22, 2023
8238fb2
add app page test case
sokra Mar 22, 2023
2c749c7
update lockfile
sokra Mar 22, 2023
fa5934d
update issue snapshotting
sokra Mar 22, 2023
5387ff5
fix test case
sokra Mar 22, 2023
598a57f
Merge branch 'canary' into sokra/web-547-errors-from-nodejs-do-not-have
sokra Mar 22, 2023
cc25fe0
Merge remote-tracking branch 'origin/canary' into sokra/web-547-error…
sokra Mar 23, 2023
4a732ce
improve test case
sokra Mar 23, 2023
929916e
update lockfile
sokra Mar 23, 2023
1c918a4
fix test case
sokra Mar 23, 2023
9093d1a
Merge remote-tracking branch 'origin/sokra/web-547-errors-from-nodejs…
sokra Mar 23, 2023
ef5c7cc
clippy
sokra Mar 23, 2023
1b10f5f
update lockfile
sokra Mar 23, 2023
6bcc8b1
sync dependencies
sokra Mar 23, 2023
e99e19a
update turbopack
sokra Mar 23, 2023
16241f4
Merge remote-tracking branch 'origin/canary' into sokra/web-547-error…
sokra Mar 23, 2023
d23a797
comments
sokra Mar 23, 2023
f2cd5cc
use released turbopack version
sokra Mar 23, 2023
1ba8232
Merge branch 'canary' into sokra/web-547-errors-from-nodejs-do-not-have
kodiakhq[bot] Mar 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 121 additions & 90 deletions packages/next-swc/Cargo.lock

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions packages/next-swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,38 @@ swc_emotion = { version = "0.29.10" }
testing = { version = "0.31.31" }

# Turbo crates
auto-hash-map = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
node-file-trace = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
swc-ast-explorer = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-malloc = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1", default-features = false }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-build = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-bytes = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-fetch = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1", default-features = false }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-hash = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-macros = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-macros-shared = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-memory = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-tasks-testing = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbo-updater = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-cli-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-core = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-create-test-app = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-css = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-dev = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-dev-server = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-ecmascript = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-json = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-mdx = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-node = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-static = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-swc-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-test-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
turbopack-tests = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.1" }
auto-hash-map = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
node-file-trace = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
swc-ast-explorer = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-malloc = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3", default-features = false }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-build = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-bytes = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-fetch = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3", default-features = false }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-hash = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-macros = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-macros-shared = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-memory = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-tasks-testing = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbo-updater = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-cli-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-core = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-create-test-app = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-css = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-dev = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-dev-server = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-ecmascript = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-env = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-json = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-mdx = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-node = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-static = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-swc-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-test-utils = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }
turbopack-tests = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230323.3" }

# General Deps

Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ easy-error = "1.0.0"
either = "1"
fxhash = "0.2.1"
hex = "0.4.3"
once_cell = "1.13.0"
once_cell = { workspace = true }
next-transform-font = {"workspace" = true}
pathdiff = "0.2.0"
regex = "1.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ napi = { version = "2", default-features = false, features = [
] }
napi-derive = "2"
next-swc = { version = "0.0.0", path = "../core" }
once_cell = "1.13.0"
once_cell = { workspace = true }
serde = "1"
serde_json = "1"
tracing = { version = "0.1.37", features = ["release_max_level_info"] }
Expand Down
186 changes: 94 additions & 92 deletions packages/next-swc/crates/next-core/js/src/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,179 +1,181 @@
import { createConnection } from "node:net";
import { createConnection } from 'node:net'

import {
StackFrame,
parse as parseStackTrace,
} from "@vercel/turbopack-next/compiled/stacktrace-parser";
} from '@vercel/turbopack-next/compiled/stacktrace-parser'

export type StructuredError = {
name: string;
message: string;
stack: StackFrame[];
};
name: string
message: string
stack: StackFrame[]
}

export function structuredError(e: Error): StructuredError {
return {
name: e.name,
message: e.message,
stack: parseStackTrace(e.stack!),
};
}
}

type State =
| {
type: "waiting";
type: 'waiting'
}
| {
type: "packet";
length: number;
};
type: 'packet'
length: number
}

export type Ipc<TIncoming, TOutgoing> = {
recv(): Promise<TIncoming>;
send(message: TOutgoing): Promise<void>;
sendError(error: Error): Promise<never>;
};
recv(): Promise<TIncoming>
send(message: TOutgoing): Promise<void>
sendError(error: Error): Promise<never>
}

function createIpc<TIncoming, TOutgoing>(
port: number
): Ipc<TIncoming, TOutgoing> {
const socket = createConnection(port, "127.0.0.1");
const packetQueue: Buffer[] = [];
const recvPromiseResolveQueue: Array<(message: TIncoming) => void> = [];
const socket = createConnection(port, '127.0.0.1')
const packetQueue: Buffer[] = []
const recvPromiseResolveQueue: Array<(message: TIncoming) => void> = []

function pushPacket(packet: Buffer) {
const recvPromiseResolve = recvPromiseResolveQueue.shift();
const recvPromiseResolve = recvPromiseResolveQueue.shift()
if (recvPromiseResolve != null) {
recvPromiseResolve(JSON.parse(packet.toString("utf8")) as TIncoming);
recvPromiseResolve(JSON.parse(packet.toString('utf8')) as TIncoming)
} else {
packetQueue.push(packet);
packetQueue.push(packet)
}
}

let state: State = { type: "waiting" };
let buffer: Buffer = Buffer.alloc(0);
socket.once("connect", () => {
socket.on("data", (chunk) => {
buffer = Buffer.concat([buffer, chunk]);
let state: State = { type: 'waiting' }
let buffer: Buffer = Buffer.alloc(0)
socket.once('connect', () => {
socket.on('data', (chunk) => {
buffer = Buffer.concat([buffer, chunk])

loop: while (true) {
switch (state.type) {
case "waiting": {
case 'waiting': {
if (buffer.length >= 4) {
const length = buffer.readUInt32BE(0);
buffer = buffer.subarray(4);
state = { type: "packet", length };
const length = buffer.readUInt32BE(0)
buffer = buffer.subarray(4)
state = { type: 'packet', length }
} else {
break loop;
break loop
}
break;
break
}
case "packet": {
case 'packet': {
if (buffer.length >= state.length) {
const packet = buffer.subarray(0, state.length);
buffer = buffer.subarray(state.length);
state = { type: "waiting" };
pushPacket(packet);
const packet = buffer.subarray(0, state.length)
buffer = buffer.subarray(state.length)
state = { type: 'waiting' }
pushPacket(packet)
} else {
break loop;
break loop
}
break;
break
}
}
}
});
});
})
})

function send(message: any): Promise<void> {
const packet = Buffer.from(JSON.stringify(message), "utf8");
const length = Buffer.alloc(4);
length.writeUInt32BE(packet.length);
socket.write(length);
const packet = Buffer.from(JSON.stringify(message), 'utf8')
const length = Buffer.alloc(4)
length.writeUInt32BE(packet.length)
socket.write(length)

return new Promise((resolve, reject) => {
socket.write(packet, (err) => {
process.stderr.write(`TURBOPACK_OUTPUT_D\n`)
process.stdout.write(`TURBOPACK_OUTPUT_D\n`)
if (err != null) {
reject(err);
reject(err)
} else {
resolve();
resolve()
}
});
});
})
})
}

return {
async recv() {
const packet = packetQueue.shift();
const packet = packetQueue.shift()
if (packet != null) {
return JSON.parse(packet.toString("utf8")) as TIncoming;
return JSON.parse(packet.toString('utf8')) as TIncoming
}

const result = await new Promise<TIncoming>((resolve) => {
recvPromiseResolveQueue.push((result) => {
resolve(result);
});
});
resolve(result)
})
})

return result;
return result
},

send(message: TOutgoing) {
return send(message);
return send(message)
},

async sendError(error: Error): Promise<never> {
try {
await send({
type: "error",
type: 'error',
...structuredError(error),
});
})
} catch (err) {
// ignore and exit anyway
}
process.exit(1);
process.exit(1)
},
};
}
}

const PORT = process.argv[2];
const PORT = process.argv[2]

export const IPC = createIpc<unknown, unknown>(parseInt(PORT, 10));
export const IPC = createIpc<unknown, unknown>(parseInt(PORT, 10))

process.on("uncaughtException", (err) => {
IPC.sendError(err);
});
process.on('uncaughtException', (err) => {
IPC.sendError(err)
})

const improveConsole = (name: string, stream: string, addStack: boolean) => {
// @ts-ignore
const original = console[name];
const original = console[name]
// @ts-ignore
const stdio = process[stream];
const stdio = process[stream]
// @ts-ignore
console[name] = (...args: any[]) => {
stdio.write(`TURBOPACK_OUTPUT_B\n`);
original(...args);
stdio.write(`TURBOPACK_OUTPUT_B\n`)
original(...args)
if (addStack) {
const stack = new Error().stack?.replace(/^.+\n.+\n/, "") + "\n";
stdio.write("TURBOPACK_OUTPUT_S\n");
stdio.write(stack);
const stack = new Error().stack?.replace(/^.+\n.+\n/, '') + '\n'
stdio.write('TURBOPACK_OUTPUT_S\n')
stdio.write(stack)
}
stdio.write("TURBOPACK_OUTPUT_E\n");
};
};

improveConsole("error", "stderr", true);
improveConsole("warn", "stderr", true);
improveConsole("count", "stdout", true);
improveConsole("trace", "stderr", false);
improveConsole("log", "stdout", true);
improveConsole("group", "stdout", true);
improveConsole("groupCollapsed", "stdout", true);
improveConsole("table", "stdout", true);
improveConsole("debug", "stdout", true);
improveConsole("info", "stdout", true);
improveConsole("dir", "stdout", true);
improveConsole("dirxml", "stdout", true);
improveConsole("timeEnd", "stdout", true);
improveConsole("timeLog", "stdout", true);
improveConsole("timeStamp", "stdout", true);
improveConsole("assert", "stderr", true);
stdio.write('TURBOPACK_OUTPUT_E\n')
}
}

improveConsole('error', 'stderr', true)
improveConsole('warn', 'stderr', true)
improveConsole('count', 'stdout', true)
improveConsole('trace', 'stderr', false)
improveConsole('log', 'stdout', true)
improveConsole('group', 'stdout', true)
improveConsole('groupCollapsed', 'stdout', true)
improveConsole('table', 'stdout', true)
improveConsole('debug', 'stdout', true)
improveConsole('info', 'stdout', true)
improveConsole('dir', 'stdout', true)
improveConsole('dirxml', 'stdout', true)
improveConsole('timeEnd', 'stdout', true)
improveConsole('timeLog', 'stdout', true)
improveConsole('timeStamp', 'stdout', true)
improveConsole('assert', 'stderr', true)
Loading