Skip to content

Commit

Permalink
chore: use fs-err in turborepo fs related libs (#5517)
Browse files Browse the repository at this point in the history
### Description
Swaps our usage of various `fs` methods to use `fs-err` instead. To
quote the `fs-err` docs:

> Using [std::fs](https://doc.rust-lang.org/stable/std/fs/), if this
code fails:
>
> `let file = File::open("does not exist.txt")?;`
>
> The error message that Rust gives you isn't very useful:
>
> `The system cannot find the file specified. (os error 2)`
>
> ...but if we use `fs-err` instead, our error contains more actionable
information:
>
> ```failed to open file `does not exist.txt`
> caused by: The system cannot find the file specified. (os error 2)```

### Testing Instructions
Existing unit tests pass

Co-authored-by: Chris Olszewski <Chris Olszewski>
  • Loading branch information
chris-olszewski committed Jul 14, 2023
1 parent f3a36e7 commit b6bb8fe
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/turborepo-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"

[dependencies]
anyhow = { workspace = true }
fs-err = "2.9.0"
turbopath = { workspace = true }
walkdir = "2.3.3"

Expand Down
5 changes: 3 additions & 2 deletions crates/turborepo-fs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![deny(clippy::all)]

use std::fs::{self, DirBuilder, Metadata};
use std::fs::{DirBuilder, FileType, Metadata};

use anyhow::Result;
use fs_err as fs;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
use walkdir::WalkDir;

Expand Down Expand Up @@ -91,7 +92,7 @@ pub fn copy_file(

fn copy_file_with_type(
from: impl AsRef<AbsoluteSystemPath>,
from_type: fs::FileType,
from_type: FileType,
to: impl AsRef<AbsoluteSystemPath>,
) -> Result<()> {
let from = from.as_ref();
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-paths/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
[dependencies]
camino = { workspace = true }
dunce = { workspace = true }
fs-err = "2.9.0"
path-clean = "1.0.1"
# TODO: Make this a crate feature
serde = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/turborepo-paths/src/absolute_system_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::os::unix::fs::symlink as symlink_dir;
#[cfg(windows)]
use std::os::windows::fs::{symlink_dir, symlink_file};
use std::{
fmt, fs,
fmt,
fs::{File, Metadata, OpenOptions},
io,
path::Path,
};

use camino::{Utf8Component, Utf8Components, Utf8Path, Utf8PathBuf};
use fs_err as fs;
use path_clean::PathClean;

use crate::{
Expand Down
6 changes: 4 additions & 2 deletions crates/turborepo-paths/src/absolute_system_path_buf.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{
borrow::Borrow,
fmt, fs,
fmt,
io::{self, Write},
ops::Deref,
path::{Path, PathBuf},
};

use camino::{Utf8Components, Utf8Path, Utf8PathBuf};
use fs_err as fs;
use path_clean::PathClean;
use serde::Serialize;

Expand Down Expand Up @@ -233,7 +234,8 @@ impl AbsoluteSystemPathBuf {
}

pub fn try_exists(&self) -> Result<bool, PathError> {
Ok(fs::try_exists(&self.0)?)
// try_exists is an experimental API and not yet in fs_err
Ok(std::fs::try_exists(&self.0)?)
}

pub fn extension(&self) -> Option<&str> {
Expand Down

0 comments on commit b6bb8fe

Please sign in to comment.