Repository: vercel/next.js
Pull request: Turbopack: add manual worker count override

<!-- 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

### 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 PACK-5642

Changed files:
filename: Cargo.toml
patch: @@ -254,6 +254,10 @@ inherits = "release"
 debug-assertions = true
 overflow-checks = true
 
+[profile.release-with-debug]
+inherits = "release"
+debug = true
+
 [workspace.dependencies]
 # Workspace crates
 next-api = { path = "crates/next-api" }

filename: turbopack/crates/turbopack-cli/benches/small_apps.rs
patch: @@ -74,6 +74,7 @@ fn bench_small_apps(c: &mut Criterion) {
                                 log_detail: false,
                                 full_stats: false,
                                 target: None,
+                                worker_threads: None,
                             },
                             no_sourcemap: false,
                             no_minify: false,

filename: turbopack/crates/turbopack-cli/src/arguments.rs
patch: @@ -25,6 +25,14 @@ impl Arguments {
             Arguments::Dev(args) => args.common.dir.as_deref(),
         }
     }
+
+    /// The number of worker threads to use. see [CommonArguments]::worker_threads
+    pub fn worker_threads(&self) -> Option<usize> {
+        match self {
+            Arguments::Build(args) => args.common.worker_threads,
+            Arguments::Dev(args) => args.common.worker_threads,
+        }
+    }
 }
 
 #[derive(
@@ -80,13 +88,17 @@ pub struct CommonArguments {
     #[clap(long)]
     pub full_stats: bool,
 
+    /// Whether to build for the `browser` or `node`
+    #[clap(long)]
+    pub target: Option<Target>,
+
+    /// Number of worker threads to use for parallel processing
+    #[clap(long)]
+    pub worker_threads: Option<usize>,
     // Enable experimental garbage collection with the provided memory limit in
     // MB.
     // #[clap(long)]
     // pub memory_limit: Option<usize>,
-    /// Whether to build for the `browser` or `node``
-    #[clap(long)]
-    pub target: Option<Target>,
 }
 
 #[derive(Debug, Args)]

filename: turbopack/crates/turbopack-cli/src/main.rs
patch: @@ -42,15 +42,25 @@ fn main() {
             });
         });
 
-    let worker_threads = available_parallelism().map(|n| n.get()).unwrap_or(1);
+    let args = Arguments::parse();
+
+    let worker_threads = args
+        .worker_threads()
+        .map(|v| {
+            if v == 0 {
+                panic!("--worker-threads=0 is invalid, you must use at least one thread.");
+            } else {
+                v
+            }
+        })
+        .unwrap_or_else(|| available_parallelism().map(|n| n.get()).unwrap_or(1));
 
     rt.worker_threads(worker_threads);
     rt.max_blocking_threads(usize::MAX - worker_threads);
 
     #[cfg(not(codspeed))]
     rt.disable_lifo_slot();
 
-    let args = Arguments::parse();
     rt.build().unwrap().block_on(main_inner(args)).unwrap();
 }