Skip to content

Commit

Permalink
Reapply "Implement Turbopack trace server bindings" (#65419) (#65527)
Browse files Browse the repository at this point in the history
Applies #65419 with the latest version of Turbopack that uses a
different websocket library.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3363
  • Loading branch information
timneutkens committed May 13, 2024
1 parent 1518895 commit ac6e412
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 88 deletions.
167 changes: 123 additions & 44 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ swc_core = { version = "0.90.33", features = [
testing = { version = "0.35.22" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240508.4" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240513.1" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240508.4" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240513.1" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240508.4" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240513.1" }

# General Deps

Expand Down
2 changes: 2 additions & 0 deletions packages/next-swc/crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub mod next_api;
pub mod parse;
pub mod transform;
#[cfg(not(target_arch = "wasm32"))]
pub mod turbo_trace_server;
#[cfg(not(target_arch = "wasm32"))]
pub mod turbopack;
#[cfg(not(target_arch = "wasm32"))]
pub mod turbotrace;
Expand Down
14 changes: 12 additions & 2 deletions packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc, time::Duration};
use std::{path::PathBuf, sync::Arc, thread, time::Duration};

use anyhow::{anyhow, bail, Context, Result};
use napi::{
Expand Down Expand Up @@ -290,12 +290,22 @@ pub async fn project_new(
.context("Unable to create .next directory")
.unwrap();
let trace_file = internal_dir.join("trace.log");
let trace_writer = std::fs::File::create(trace_file).unwrap();
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap();
let (trace_writer, guard) = TraceWriter::new(trace_writer);
let subscriber = subscriber.with(RawTraceLayer::new(trace_writer));

let guard = ExitGuard::new(guard).unwrap();

let trace_server = std::env::var("NEXT_TURBOPACK_TRACE_SERVER").ok();
if trace_server.is_some() {
thread::spawn(move || {
turbopack_binding::turbopack::trace_server::start_turbopack_trace_server(
trace_file,
);
});
println!("Turbopack trace server started. View trace at https://turbo-trace-viewer.vercel.app/");
}

subscriber.init();

Some(guard)
Expand Down
7 changes: 7 additions & 0 deletions packages/next-swc/crates/napi/src/turbo_trace_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::path::PathBuf;

#[napi]
pub fn start_turbopack_trace_server(path: String) {
let path_buf = PathBuf::from(path);
turbopack_binding::turbopack::trace_server::start_turbopack_trace_server(path_buf);
}
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ turbopack-binding = { workspace = true, features = [
"__turbopack_image",
"__turbopack_node",
"__turbopack_trace_utils",
"__turbopack_trace_server",
] }

turbo-tasks = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.4",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240508.4",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240513.1",
"acorn": "8.5.0",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
Expand Down
15 changes: 15 additions & 0 deletions packages/next/src/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,19 @@ program
})
.usage('[directory] [options]')

const internal = program
.command('internal')
.description(
'Internal debugging commands. Use with caution. Not covered by semver.'
)

internal
.command('turbo-trace-server')
.argument('[file]', 'Trace file to serve.')
.action((file) => {
return import('../cli/internal/turbo-trace-server.js').then((mod) =>
mod.startTurboTraceServerCli(file)
)
})

program.parse(process.argv)
34 changes: 20 additions & 14 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let lastNativeBindingsLoadErrorCode:
| 'unsupported_target'
| string
| undefined = undefined
let nativeBindings: any
let nativeBindings: Binding
let wasmBindings: any
let downloadWasmPromise: any
let pendingBindings: any
Expand All @@ -158,21 +158,21 @@ export interface Binding {
stream: any
get: any
}
mdx: {
compile: any
compileSync: any
}
createProject: (
options: ProjectOptions,
turboEngineOptions?: TurboEngineOptions
) => Promise<Project>
startTurbopackTraceServer: (path: string) => void
}
mdx: {
compile: any
compileSync: any
}
minify: any
minifySync: any
transform: any
transformSync: any
parse: any
parseSync: any

getTargetTriple(): string | undefined

Expand Down Expand Up @@ -775,7 +775,10 @@ function rustifyEnv(env: Record<string, string>): RustifiedEnv {
}

// TODO(sokra) Support wasm option.
function bindingToApi(binding: any, _wasm: boolean) {
function bindingToApi(
binding: any,
_wasm: boolean
): Binding['turbo']['createProject'] {
type NativeFunction<T> = (
callback: (err: Error, value: T) => void
) => Promise<{ __napiType: 'RootTask' }>
Expand Down Expand Up @@ -1217,10 +1220,10 @@ function bindingToApi(binding: any, _wasm: boolean) {
}
}

async function createProject(
options: ProjectOptions,
turboEngineOptions: TurboEngineOptions
) {
const createProject: Binding['turbo']['createProject'] = async (
options,
turboEngineOptions
) => {
return new ProjectImpl(
await binding.projectNew(
await rustifyProjectOptions(options),
Expand Down Expand Up @@ -1278,9 +1281,6 @@ async function loadWasm(importPath = '') {
? bindings.parse(src.toString(), options)
: Promise.resolve(bindings.parseSync(src.toString(), options))
},
parseSync(src: string, options: any) {
return bindings.parseSync(src.toString(), options)
},
getTargetTriple() {
return undefined
},
Expand Down Expand Up @@ -1510,6 +1510,12 @@ function loadNative(importPath?: string) {
},
},
createProject: bindingToApi(customBindings ?? bindings, false),
startTurbopackTraceServer: (traceFilePath) => {
Log.warn(
'Turbopack trace server started. View trace at https://turbo-trace-viewer.vercel.app/'
)
;(customBindings ?? bindings).startTurbopackTraceServer(traceFilePath)
},
},
mdx: {
compile: (src: string, options: any) =>
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/cli/internal/turbo-trace-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { loadBindings } from '../../build/swc'

export async function startTurboTraceServerCli(file: string) {
let bindings = await loadBindings()
bindings.turbo.startTurbopackTraceServer(file)
}
Loading

0 comments on commit ac6e412

Please sign in to comment.