Repository: vercel/next.js
Pull request: Turbopack: Use `format!` instead of `anyhow!` inside of `anyhow::Context::with_context` calls

`anyhow!` works here because we just need something that implements `Display`, but `format!` is better.

Changed files:
filename: turbopack/crates/turbo-tasks-backend/src/kv_backing_storage.rs
patch: @@ -5,7 +5,7 @@ use std::{
     sync::{Arc, LazyLock, Mutex, PoisonError, Weak},
 };
 
-use anyhow::{Context, Result, anyhow};
+use anyhow::{Context, Result};
 use turbo_bincode::{
     TurboBincodeBuffer, turbo_bincode_decode, turbo_bincode_encode, turbo_bincode_encode_into,
 };
@@ -328,7 +328,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
                                         WriteBuffer::Borrowed(&task_id.to_le_bytes()),
                                     )
                                     .with_context(|| {
-                                        anyhow!(
+                                        format!(
                                             "Unable to write task cache {task_type:?} => {task_id}"
                                         )
                                     })?;
@@ -339,7 +339,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
                                         WriteBuffer::Borrowed(&task_type_bytes),
                                     )
                                     .with_context(|| {
-                                        anyhow!(
+                                        format!(
                                             "Unable to write task cache {task_id} => {task_type:?}"
                                         )
                                     })?;
@@ -373,14 +373,14 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
                             batch
                                 .put(KeySpace::TaskMeta, WriteBuffer::Borrowed(key), meta)
                                 .with_context(|| {
-                                    anyhow!("Unable to write meta items for {task_id}")
+                                    format!("Unable to write meta items for {task_id}")
                                 })?;
                         }
                         if let Some(data) = data {
                             batch
                                 .put(KeySpace::TaskData, WriteBuffer::Borrowed(key), data)
                                 .with_context(|| {
-                                    anyhow!("Unable to write data items for {task_id}")
+                                    format!("Unable to write data items for {task_id}")
                                 })?;
                         }
                     }
@@ -415,7 +415,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
                                 WriteBuffer::Borrowed(&task_id.to_le_bytes()),
                             )
                             .with_context(|| {
-                                anyhow!("Unable to write task cache {task_type:?} => {task_id}")
+                                format!("Unable to write task cache {task_type:?} => {task_id}")
                             })?;
                         batch
                             .put(
@@ -424,7 +424,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
                                 WriteBuffer::Borrowed(&task_type_bytes),
                             )
                             .with_context(|| {
-                                anyhow!("Unable to write task cache {task_id} => {task_type:?}")
+                                format!("Unable to write task cache {task_id} => {task_type:?}")
                             })?;
                         next_task_id = next_task_id.max(task_id + 1);
                     }
@@ -440,9 +440,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
 
         {
             let _span = tracing::trace_span!("commit").entered();
-            batch
-                .commit()
-                .with_context(|| anyhow!("Unable to commit operations"))?;
+            batch.commit().context("Unable to commit operations")?;
         }
         Ok(())
     }

filename: turbopack/crates/turbo-tasks-fs/src/lib.rs
patch: @@ -1049,7 +1049,7 @@ impl FileSystem for DiskFileSystem {
                             Err(err)
                         }
                     })
-                    .with_context(|| anyhow!("removing {} failed", full_path.display()))?;
+                    .with_context(|| format!("removing {} failed", full_path.display()))?;
                 }
             }
 
@@ -1202,7 +1202,7 @@ impl FileSystem for DiskFileSystem {
                             .concurrency_limited(&inner.write_semaphore)
                             .await
                             .with_context(|| {
-                                anyhow!("removing existing symlink {} failed", full_path.display())
+                                format!("removing existing symlink {} failed", full_path.display())
                             })?;
                     }
 
@@ -1257,7 +1257,7 @@ impl FileSystem for DiskFileSystem {
                         ))
                         .concurrency_limited(&inner.write_semaphore)
                         .await
-                        .with_context(|| anyhow!("removing {} failed", full_path.display()))?;
+                        .with_context(|| format!("removing {} failed", full_path.display()))?;
                 }
             }
 

filename: turbopack/crates/turbopack-bench/src/util/env.rs
patch: @@ -1,6 +1,6 @@
 use std::{error::Error, str::FromStr};
 
-use anyhow::{Context, Result, anyhow};
+use anyhow::{Context, Result};
 
 /// Reads an environment variable.
 pub fn read_env<T>(name: &str, default: T) -> Result<T>
@@ -13,7 +13,7 @@ where
         None | Some("") => Ok(default),
         Some(config) => config
             .parse()
-            .with_context(|| anyhow!("Invalid value for {}", name)),
+            .with_context(|| format!("Invalid value for {}", name)),
     }
 }
 
@@ -40,7 +40,7 @@ where
             .split(',')
             .map(|s| {
                 s.parse()
-                    .with_context(|| anyhow!("Invalid value for {}", name))
+                    .with_context(|| format!("Invalid value for {}", name))
             })
             .collect(),
     }

filename: turbopack/crates/turbopack-tracing/tests/node-file-trace.rs
patch: @@ -16,7 +16,7 @@ use std::{
     time::{Duration, Instant},
 };
 
-use anyhow::{Context, Result, anyhow};
+use anyhow::{Context, Result};
 use difference::Changeset;
 use helpers::print_changeset;
 use regex::Regex;
@@ -645,8 +645,8 @@ async fn exec_node(directory: &str, path: &str) -> Result<CommandOutput> {
 
     let output = timeout(Duration::from_secs(100), cmd.output())
         .await
-        .with_context(|| anyhow!("node execution of {path} is hanging"))?
-        .with_context(|| anyhow!("failed to spawn node process of {path}"))?;
+        .with_context(|| format!("node execution of {path} is hanging"))?
+        .with_context(|| format!("failed to spawn node process of {path}"))?;
 
     let output = CommandOutput {
         stdout: String::from_utf8_lossy(&output.stdout).to_string(),

filename: turbopack/xtask/src/summarize_bench/mod.rs
patch: @@ -8,7 +8,7 @@ use std::{
     time::{Duration, UNIX_EPOCH},
 };
 
-use anyhow::{Context, anyhow};
+use anyhow::Context;
 use chrono::{DateTime, Utc};
 use indexmap::IndexSet;
 use rustc_hash::{FxHashMap, FxHasher};
@@ -84,7 +84,7 @@ pub fn process_all(path: PathBuf) {
                     let file = File::open(&data_file.path).unwrap();
                     let reader = std::io::BufReader::new(file);
                     let data: BaseBenchmarks = serde_json::from_reader(reader)
-                        .with_context(|| anyhow!("unable to read {}", data_file.path.display()))
+                        .with_context(|| format!("unable to read {}", data_file.path.display()))
                         .unwrap();
                     data
                 })