Repository: vercel/next.js
File: crates/napi/src/lockfile.rs (Rust)

Code before:
    let mut open_options = OpenOptions::new();
    open_options.write(true).create(true).truncate(true);

    // On Windows, we don't use `File::lock` because that grabs a mandatory lock. That can break
    // tools or code that read the contents of the `.next` directory because the mandatory lock
    // file will fail with EBUSY when read. Instead, we open a file with write mode, but without
    // `FILE_SHARE_WRITE`. That gives us behavior closer to what we get on POSIX platforms.
    //
    // On POSIX platforms, Rust uses `flock` which creates an advisory lock, which can be
    // read/written/deleted.

    #[cfg(windows)]
    return {
        use std::os::windows::fs::OpenOptionsExt;

        use windows_sys::Win32::{Foundation, Storage::FileSystem};

        open_options
            .share_mode(FileSystem::FILE_SHARE_READ | FileSystem::FILE_SHARE_DELETE)
            .custom_flags(FileSystem::FILE_FLAG_DELETE_ON_CLOSE);
        match open_options.open(&path) {
            Ok(mut file) => {
                // Write content to the lockfile if provided
                if let Some(ref data) = content {
                    let _ = file.write_all(data.as_bytes());
                    let _ = file.flush();
                }
                Ok(Some(External::new(Mutex::new(ManuallyDrop::new(Some(
                    LockfileInner { file },
                ))))))
            }
            Err(err)
                if err.raw_os_error()
                    == Some(Foundation::ERROR_SHARING_VIOLATION.try_into().unwrap()) =>
            {
                Ok(None)
            }
            Err(err) => Err(err.into()),
        }
    };

    #[cfg(not(windows))]
    return {
        use std::fs::TryLockError;

        let file = open_options.open(&path)?;
        match file.try_lock() {
            Ok(_) => {
                // Write content to the lockfile if provided
                if let Some(ref data) = content {
                    // Need to get a mutable reference

Review comment (bug):
Just to be clear: This is doing things "best-effort". The write could fail, and we're swallowing the error.

This is okay, but I think it would be slightly better to return the error if this fails, because it's an indication that something weird is going on:

```suggestion
                // Write content to the lockfile if provided
                if let Some(ref data) = content {
                    file.write_all(data.as_bytes())?;
                    file.flush()?;
```

Code after:
    // On Windows, we don't use `File::lock` because that grabs a mandatory lock. That can break
    // tools or code that read the contents of the `.next` directory because the mandatory lock
    // file will fail with EBUSY when read. Instead, we open a file with write mode, but without
    // `FILE_SHARE_WRITE`. That gives us behavior closer to what we get on POSIX platforms.
    //
    // On POSIX platforms, Rust uses `flock` which creates an advisory lock, which can be
    // read/written/deleted.

    #[cfg(windows)]
    return {
        use std::os::windows::fs::OpenOptionsExt;

        use windows_sys::Win32::{Foundation, Storage::FileSystem};

        // On Windows, opening with write mode without FILE_SHARE_WRITE acts as the lock.
        // We use truncate(true) here because if we can open the file, we have the lock.
        let mut open_options = OpenOptions::new();
        open_options.write(true).create(true).truncate(true);
        open_options
            .share_mode(FileSystem::FILE_SHARE_READ | FileSystem::FILE_SHARE_DELETE)
            .custom_flags(FileSystem::FILE_FLAG_DELETE_ON_CLOSE);
        match open_options.open(&path) {
            Ok(mut file) => {
                // Write content to the lockfile if provided
                if let Some(ref data) = content {
                    file.write_all(data.as_bytes())?;
                    file.flush()?;
                }
                Ok(Some(External::new(Mutex::new(ManuallyDrop::new(Some(
                    LockfileInner { file },
                ))))))
            }
            Err(err)
                if err.raw_os_error()
                    == Some(Foundation::ERROR_SHARING_VIOLATION.try_into().unwrap()) =>
            {
                Ok(None)
            }
            Err(err) => Err(err.into()),
        }
    };

    #[cfg(not(windows))]
    return {
        use std::{fs::TryLockError, io::Seek};

        // On Unix, we must NOT truncate on open because flock is advisory -
        // opening with truncate would clear another process's lockfile content.
        // Instead, open without truncate, acquire the lock, then truncate.
        let mut open_options = OpenOptions::new();
        open_options.write(true).create(true).read(true);