Unverified Commit 8fe8ff79 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Tweak retry loop for link creation to try to fix os error 80 on Windows (#88669)

Though I've been unable to reproduce the issue (prior PRs in this stack are my attempts at that), I believe this *should* fix the OS Error 80 ([`ERROR_FILE_EXISTS`](https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-)) that we've seen users report on Windows: https://github.com/vercel/next.js/discussions/88382

Basically instead of deleting the existing link in a retry loop, and then trying to create new new link using a retry loop, we should instead:
- Merge these two retry loops into one, in case another process creates the link after we delete it. This shouldn't typically happen, but could theoretically happen.
- Extend the retry logic to also retry on [`ErrorKind::AlreadyExists`](https://github.com/rust-lang/rust/blob/503745e9170b40841611aaaa634641edffd00b29/library/std/src/sys/io/error/windows.rs#L19). This might be possible if the filesystem is eventually consistent (which can happen on Windows), but I wasn't able to reproduce this.

There's a similar -- but different -- issue that users have been reporting where they get OS Error 1 (`ERROR_INVALID_FUNCTION`) on Windows. I believe this happens if we try to create a junction point on a non-NTFS filesystem. In that case, we need to report a clearer issue to the user instead of bubbling this up as an internal Turbopack error.

I tested this on Windows with the symlink fuzzer from #88667, as well as `cargo test -p turbo-tasks-fs`:

![Screenshot 2026-01-16 at 2.16.23 PM.png](https://app.graphite.com/user-attachments/assets/5c86e09b-c5e5-4ff2-9d89-da009917a018.png)
parent f263c562
...@@ -9544,6 +9544,7 @@ dependencies = [ ...@@ -9544,6 +9544,7 @@ dependencies = [
"serde_path_to_error", "serde_path_to_error",
"sha2", "sha2",
"tempfile", "tempfile",
"thiserror 1.0.69",
"tokio", "tokio",
"tracing", "tracing",
"triomphe 0.1.12", "triomphe 0.1.12",
......
...@@ -43,6 +43,7 @@ rustc-hash = { workspace = true } ...@@ -43,6 +43,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc"] } serde = { workspace = true, features = ["rc"] }
serde_json = { workspace = true } serde_json = { workspace = true }
serde_path_to_error = { workspace = true } serde_path_to_error = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
triomphe = { workspace = true } triomphe = { workspace = true }
......
...@@ -80,7 +80,7 @@ use crate::{ ...@@ -80,7 +80,7 @@ use crate::{
json::UnparsableJson, json::UnparsableJson,
mutex_map::MutexMap, mutex_map::MutexMap,
read_glob::{read_glob, track_glob}, read_glob::{read_glob, track_glob},
retry::retry_blocking, retry::{can_retry, retry_blocking, retry_blocking_custom},
rope::{Rope, RopeReader}, rope::{Rope, RopeReader},
util::extract_disk_access, util::extract_disk_access,
watcher::DiskWatcher, watcher::DiskWatcher,
...@@ -1139,65 +1139,87 @@ impl FileSystem for DiskFileSystem { ...@@ -1139,65 +1139,87 @@ impl FileSystem for DiskFileSystem {
})?; })?;
} }
if old_content.is_some() { #[derive(thiserror::Error, Debug)]
// Remove existing symlink before creating a new one. On Unix, symlink(2) #[error("{msg}: {source}")]
// fails with EEXIST if the link already exists instead of overwriting it. struct SymlinkCreationError {
// Windows has similar behavior with junction points. msg: &'static str,
remove_symbolic_link_dir_helper(&full_path) #[source]
.instrument(tracing::info_span!( source: io::Error,
"remove existing symlink before write",
name = ?full_path,
))
.concurrency_limited(&inner.write_semaphore)
.await
.with_context(|| {
format!("removing existing symlink {full_path:?} failed")
})?;
} }
retry_blocking(|| { let mut has_old_content = old_content.is_some();
#[cfg(not(windows))] let try_create_link = || {
{ if has_old_content {
std::os::unix::fs::symlink(&target, &full_path) // Remove existing symlink before creating a new one. On Unix,
// symlink(2) fails with EEXIST if the link already exists instead of
// overwriting it. Windows has similar behavior with junction points.
remove_symbolic_link_dir_helper(&full_path).map_err(|err| {
SymlinkCreationError {
msg: "removal of existing symbolic link or junction point \
failed",
source: err,
}
})?;
has_old_content = false;
} }
#[cfg(not(windows))]
let io_result = std::os::unix::fs::symlink(&target, &full_path);
#[cfg(windows)] #[cfg(windows)]
{ let io_result = if is_directory {
if is_directory { std::os::windows::fs::junction_point(&target, &full_path)
std::os::windows::fs::junction_point(&target, &full_path) } else {
} else { std::os::windows::fs::symlink_file(&target, &full_path)
std::os::windows::fs::symlink_file(&target, &full_path) };
io_result.map_err(|err| {
if err.kind() == ErrorKind::AlreadyExists {
// try to remove the symlink on the next iteration of the loop
has_old_content = true;
} }
} SymlinkCreationError {
}) msg: "creation of a new symbolic link or junction point failed",
.instrument(tracing::info_span!( source: err,
"write symlink", }
name = ?full_path, })
target = ?target, };
)) fn can_retry_link(err: &SymlinkCreationError) -> bool {
.concurrency_limited(&inner.write_semaphore) err.source.kind() == ErrorKind::AlreadyExists || can_retry(&err.source)
.await }
.with_context(|| { let err_context = || {
#[cfg(not(windows))] #[cfg(not(windows))]
let message = format!("failed to create symlink to {target:?}"); let message = format!(
"failed to create symlink at {full_path:?} pointing to {target:?}"
);
#[cfg(windows)] #[cfg(windows)]
let message = if is_directory { let message = if is_directory {
format!("failed to create junction point to {target:?}") format!(
"failed to create junction point at {full_path:?} pointing to \
{target:?}"
)
} else { } else {
format!( format!(
"failed to create symlink to {target:?}\n\ "failed to create symlink at {full_path:?} pointing to {target:?}\n\
(Note: creating file symlinks on Windows require developer mode or \ (Note: creating file symlinks on Windows require developer mode or \
admin permissions: \ admin permissions: \
https://learn.microsoft.com/en-us/windows/advanced-settings/developer-mode)", https://learn.microsoft.com/en-us/windows/advanced-settings/developer-mode)",
) )
}; };
message message
})?; };
retry_blocking_custom(try_create_link, can_retry_link)
.instrument(tracing::info_span!(
"write symlink",
name = ?full_path,
target = ?target,
))
.concurrency_limited(&inner.write_semaphore)
.await
.with_context(err_context)?;
} }
OsSpecificLinkContent::Invalid => { OsSpecificLinkContent::Invalid => {
bail!("invalid symlink target: {full_path:?}") bail!("invalid symlink target: {full_path:?}")
} }
OsSpecificLinkContent::NotFound => { OsSpecificLinkContent::NotFound => {
remove_symbolic_link_dir_helper(&full_path) retry_blocking(|| remove_symbolic_link_dir_helper(&full_path))
.instrument(tracing::info_span!("remove symlink", name = ?full_path)) .instrument(tracing::info_span!("remove symlink", name = ?full_path))
.concurrency_limited(&inner.write_semaphore) .concurrency_limited(&inner.write_semaphore)
.await .await
...@@ -1236,41 +1258,35 @@ impl FileSystem for DiskFileSystem { ...@@ -1236,41 +1258,35 @@ impl FileSystem for DiskFileSystem {
} }
} }
async fn remove_symbolic_link_dir_helper(path: &Path) -> Result<()> { fn remove_symbolic_link_dir_helper(path: &Path) -> io::Result<()> {
retry_blocking(|| { let result = if cfg!(windows) {
if cfg!(windows) { // Junction points on Windows are treated as directories, and therefore need
// Junction points on Windows are treated as directories, and therefore need // `remove_dir`:
// `remove_dir`: //
// // > `RemoveDirectory` can be used to remove a directory junction. Since the target
// > `RemoveDirectory` can be used to remove a directory junction. Since the target // > directory and its contents will remain accessible through its canonical path, the
// > directory and its contents will remain accessible through its canonical path, the // > target directory itself is not affected by removing a junction which targets it.
// > target directory itself is not affected by removing a junction which targets it. //
// // -- https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectoryw
// -- https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectoryw //
// // However, Next 16.1.0 shipped with symlinks, before we switched to junction links on
// However, Next 16.1.0 shipped with symlinks, before we switched to junction links on // Windows, and `remove_dir` won't work on symlinks. So try to remove it as a directory
// Windows, and `remove_dir` won't work on symlinks. So try to remove it as a directory // (junction) first, and then fall back to removing it as a file (symlink).
// (junction) first, and then fall back to removing it as a file (symlink). std::fs::remove_dir(path).or_else(|err| {
std::fs::remove_dir(path).or_else(|err| { if err.kind() == ErrorKind::NotADirectory {
if err.kind() == ErrorKind::NotADirectory { std::fs::remove_file(path)
std::fs::remove_file(path) } else {
} else { Err(err)
Err(err) }
} })
}) } else {
} else { std::fs::remove_file(path)
std::fs::remove_file(path) };
} match result {
}) Ok(()) => Ok(()),
.await Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
.or_else(|err| { Err(err) => Err(err),
if err.kind() == ErrorKind::NotFound { }
Ok(())
} else {
Err(err)
}
})
.with_context(|| format!("removing existing symlink {path:?} failed"))
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment