Skip to content

Commit

Permalink
Implement Turbopack trace server bindings (vercel#65410)
Browse files Browse the repository at this point in the history
## What?

Implements support for running the Turbopack trace server, which is the
websocket server that powers https://turbo-trace-viewer.vercel.app/ when
using `NEXT_TURBOPACK_TRACING=1 NEXT_TURBOPACK_TRACE_SERVER=1`.

Currently you have to manually run the server through the Turbo
repository which in practice means that only people working on Turbopack
are able to run it.

With the bindings implemented anyone should be able to run the trace
server.

Note that the traces that come out of Turbopack are very low level,
they're meant for optimizing Turbopack like finding slowdowns / large
memory usage / optimizing performance.

However, it's useful for people that want to peek into why their
application is slow to compile. I.e. we've used
https://turbo-trace-viewer.vercel.app to investigate reports in vercel#48748.

This PR adds support for `trace.log` by default, so if you add
`NEXT_TURBOPACK_TRACING=1 NEXT_TURBOPACK_TRACE_SERVER=1` it will
automatically select the `trace.log` for the current instance of
Next.js. You can only have one trace server running at the same time.

### `next internal` 

In order to support running the trace server standalone, which is useful
for investigating trace files other people have shared, I've added a new
subcommand `internal` that is not covered by semver / use at your own
risk. It's meant for internal tools that are useful to be bound to the
version of Next.js, the turbo-trace-server is a great example of that as
it has an internal binary format for storing data that needs to match
the trace.log file.

If you want to take a look at `.next/trace` instead the new `next
internal` subcommand can be used for that:

```sh
# Replace [path] with a path to a file.
next internal turbo-trace-server [path]
```

For example:

```sh
next internal turbo-trace-server ~/Downloads/trace
```

Currently the trace server does not support loading multiple files, just
hasn't been implemented yet. Once we can load two or more files we can
load both `.next/trace` and `trace.log` when
`NEXT_TURBOPACK_TRACE_SERVER=1` and support multiple paths passed to
`next internal turbo-trace-server`.


### Turbopack upgrade


PR includes a Turbopack upgrade:

* vercel/turborepo#8073 <!-- OJ Kwon -
feat(webpack-loaders): support dummy span interface -->
* vercel/turborepo#8083 <!-- OJ Kwon - fix(webpack):
print resource, project path when relative calc fails -->
* vercel/turborepo#8094 <!-- Tim Neutkens -
Implement bindings for Turbopack trace server -->


<!-- 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-3328
  • Loading branch information
timneutkens authored May 6, 2024
1 parent c56d69d commit b85210d
Show file tree
Hide file tree
Showing 11 changed files with 1,071 additions and 313 deletions.
1,289 changes: 1,000 additions & 289 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-240503.1" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240506.2" }
# [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-240503.1" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240506.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240503.1" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240506.2" }

# 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 @@ -249,12 +249,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-240503.1",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240506.2",
"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 @@ -137,7 +137,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 @@ -157,21 +157,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 @@ -752,7 +752,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 @@ -1194,10 +1197,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 @@ -1255,9 +1258,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 @@ -1483,6 +1483,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)
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b85210d

Please sign in to comment.