Skip to content

Commit

Permalink
feat: use fs-err in more places
Browse files Browse the repository at this point in the history
I've noticed that there are still many places where we don't use `fs-err`
  • Loading branch information
Hofer-Julian committed Dec 3, 2024
1 parent 6655397 commit fcad8e9
Show file tree
Hide file tree
Showing 27 changed files with 63 additions and 55 deletions.
5 changes: 5 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/pixi_build_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.0"

[dependencies]
dashmap = { workspace = true }
fs-err = { workspace = true }
futures = { workspace = true }
itertools = { workspace = true }
jsonrpsee = { workspace = true, features = ["client"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ mod test {
#[case::pinject("conda-render/pinject.txt")]
#[case::microarch("conda-render/microarch-level.txt")]
fn test_extract_rendered_recipe(#[case] path: &str) {
let rendered_recipe = std::fs::read_to_string(
let rendered_recipe = fs_err::read_to_string(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("test-data")
.join(path),
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_glob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.0"

[dependencies]
dashmap = { workspace = true }
fs-err = { workspace = true }
itertools = { workspace = true }
memchr = { workspace = true }
rattler_digest = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_manifest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.0"
[dependencies]
dunce = { workspace = true }
fancy_display = { workspace = true }
fs-err = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
pep440_rs = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/pixi_manifest/src/manifests/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Manifest {
/// Create a new manifest from a path
pub fn from_path(path: impl AsRef<Path>) -> miette::Result<Self> {
let manifest_path = dunce::canonicalize(path.as_ref()).into_diagnostic()?;
let contents = std::fs::read_to_string(path.as_ref()).into_diagnostic()?;
let contents = fs_err::read_to_string(path.as_ref()).into_diagnostic()?;
Self::from_str(manifest_path.as_ref(), contents)
}

Expand Down Expand Up @@ -162,7 +162,7 @@ impl Manifest {
/// Save the manifest to the file and update the contents
pub fn save(&mut self) -> miette::Result<()> {
let contents = self.document.to_string();
std::fs::write(&self.path, &contents).into_diagnostic()?;
fs_err::write(&self.path, &contents).into_diagnostic()?;
self.contents = Some(contents);
Ok(())
}
Expand Down Expand Up @@ -801,7 +801,7 @@ mod tests {
// Test the toml from a path
let dir = tempdir().unwrap();
let path = dir.path().join("pixi.toml");
std::fs::write(&path, PROJECT_BOILERPLATE).unwrap();
fs_err::write(&path, PROJECT_BOILERPLATE).unwrap();
// From &PathBuf
let _manifest = Manifest::from_path(&path).unwrap();
// From &Path
Expand Down Expand Up @@ -2321,7 +2321,7 @@ bar = "*"
for entry in glob(location).unwrap() {
match entry {
Ok(path) => {
let contents = std::fs::read_to_string(path).unwrap();
let contents = fs_err::read_to_string(path).unwrap();
let _manifest = Manifest::from_str(Path::new("pixi.toml"), contents).unwrap();
}
Err(e) => println!("{:?}", e),
Expand Down
2 changes: 1 addition & 1 deletion crates/pixi_trampoline/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use fs_err::File;
use miette::{Context, IntoDiagnostic};
use pixi_utils::executable_from_path;
use serde::Deserialize;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::ops::Not;
#[cfg(target_family = "unix")]
use std::os::unix::process::CommandExt;
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rustls-tls = [

[dependencies]
fd-lock = { workspace = true }
fs-err = { workspace = true }
indicatif = { workspace = true }
itertools = { workspace = true }
miette = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/pixi_utils/src/conda_environment_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl CondaEnvFile {
}

pub fn from_path(path: &Path) -> miette::Result<Self> {
let file = std::fs::File::open(path).into_diagnostic()?;
let file = fs_err::File::open(path).into_diagnostic()?;
let reader = std::io::BufReader::new(file);

let lines = reader
Expand Down Expand Up @@ -317,7 +317,7 @@ mod tests {

let f = tempfile::NamedTempFile::new().unwrap();
let path = f.path();
let mut file = std::fs::File::create(path).unwrap();
let mut file = fs_err::File::create(path).unwrap();
file.write_all(example_conda_env_file.as_bytes()).unwrap();

let conda_env_file_data = CondaEnvFile::from_path(path).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/pypi_mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version = "0.1.0"

[dependencies]
async-once-cell = { workspace = true }
fs-err = { workspace = true }
futures = { workspace = true }
http-cache-reqwest = { workspace = true }
itertools = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/pypi_mapping/src/custom_pypi_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn fetch_mapping_from_url(
}

pub fn fetch_mapping_from_path(path: &Path) -> miette::Result<CompressedMapping> {
let file = std::fs::File::open(path)
let file = fs_err::File::open(path)
.into_diagnostic()
.context(format!("failed to open file {}", path.display()))?;
let reader = std::io::BufReader::new(file);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
// Move the built packages to the output directory.
let output_dir = args.output_dir;
for package in result.packages {
std::fs::create_dir_all(&output_dir)
fs_err::create_dir_all(&output_dir)
.into_diagnostic()
.with_context(|| {
format!(
Expand Down
2 changes: 1 addition & 1 deletion src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ mod tests {
let template = "Test Template";

fn read_file_content(path: &Path) -> String {
let mut file = std::fs::File::open(path).unwrap();
let mut file = fs_err::File::open(path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content
Expand Down
14 changes: 7 additions & 7 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn verify_prefix_location_unchanged(environment_dir: &Path) -> miette:
prefix_file.display()
);

match std::fs::read_to_string(prefix_file.clone()) {
match fs_err::read_to_string(prefix_file.clone()) {
// Not found is fine as it can be new or backwards compatible.
Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
// Scream the error if we don't know it.
Expand Down Expand Up @@ -240,11 +240,11 @@ pub(crate) fn write_environment_file(
.parent()
.expect("There should already be a conda-meta folder");

match std::fs::create_dir_all(parent).into_diagnostic() {
match fs_err::create_dir_all(parent).into_diagnostic() {
Ok(_) => {
// Using json as it's easier to machine read it.
let contents = serde_json::to_string_pretty(&env_file).into_diagnostic()?;
match std::fs::write(&path, contents).into_diagnostic() {
match fs_err::write(&path, contents).into_diagnostic() {
Ok(_) => {
tracing::debug!("Wrote environment file to: {:?}", path);
}
Expand All @@ -270,7 +270,7 @@ pub(crate) fn read_environment_file(
) -> miette::Result<Option<EnvironmentFile>> {
let path = environment_file_path(environment_dir);

let contents = match std::fs::read_to_string(&path) {
let contents = match fs_err::read_to_string(&path) {
Ok(contents) => contents,
Err(e) if e.kind() == ErrorKind::NotFound => {
tracing::debug!("Environment file not yet found at: {:?}", path);
Expand All @@ -282,7 +282,7 @@ pub(crate) fn read_environment_file(
path,
e
);
let _ = std::fs::remove_file(&path);
let _ = fs_err::remove_file(&path);
return Err(e).into_diagnostic();
}
};
Expand All @@ -294,7 +294,7 @@ pub(crate) fn read_environment_file(
path,
e
);
let _ = std::fs::remove_file(&path);
let _ = fs_err::remove_file(&path);
return Ok(None);
}
};
Expand Down Expand Up @@ -526,7 +526,7 @@ pub async fn update_prefix_pypi(
async fn uninstall_outdated_site_packages(site_packages: &Path) -> miette::Result<()> {
// Check if the old interpreter is outdated
let mut installed = vec![];
for entry in std::fs::read_dir(site_packages).into_diagnostic()? {
for entry in fs_err::read_dir(site_packages).into_diagnostic()? {
let entry = entry.into_diagnostic()?;
if entry.file_type().into_diagnostic()?.is_dir() {
let path = entry.path();
Expand Down
4 changes: 2 additions & 2 deletions src/global/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl BinDir {
#[cfg(test)]
pub fn new(root: PathBuf) -> miette::Result<Self> {
let path = root.join("bin");
std::fs::create_dir_all(&path).into_diagnostic()?;
fs_err::create_dir_all(&path).into_diagnostic()?;
Ok(Self(path))
}

Expand Down Expand Up @@ -107,7 +107,7 @@ impl EnvRoot {
#[cfg(test)]
pub fn new(root: PathBuf) -> miette::Result<Self> {
let path = root.join("envs");
std::fs::create_dir_all(&path).into_diagnostic()?;
fs_err::create_dir_all(&path).into_diagnostic()?;
Ok(Self(path))
}

Expand Down
2 changes: 1 addition & 1 deletion src/global/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ mod tests {
#[tokio::test]
async fn test_extract_executable_from_script_windows() {
use crate::global::trampoline::GlobalExecutable;
use std::fs;
use fs_err;
use std::path::Path;
let script_without_quote = r#"
@SET "PATH=C:\Users\USER\.pixi/envs\hyperfine\bin:%PATH%"
Expand Down
2 changes: 1 addition & 1 deletion src/lock_file/resolve/uv_resolution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl UvResolutionContext {
pub(crate) fn from_project(project: &Project) -> miette::Result<Self> {
let uv_cache = get_cache_dir()?.join(consts::PYPI_CACHE_DIR);
if !uv_cache.exists() {
std::fs::create_dir_all(&uv_cache)
fs_err::create_dir_all(&uv_cache)
.into_diagnostic()
.context("failed to create uv cache directory")?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Prefix {
let concurrency_limit = concurrency_limit.unwrap_or(100);
let mut meta_futures = FuturesUnordered::<JoinHandle<miette::Result<PrefixRecord>>>::new();
let mut result = Vec::new();
for entry in std::fs::read_dir(self.root.join("conda-meta"))
for entry in fs_err::read_dir(self.root.join("conda-meta"))
.into_iter()
.flatten()
{
Expand Down
9 changes: 4 additions & 5 deletions src/project/environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::{HashMap, HashSet},
fmt::Debug,
fs,
hash::{Hash, Hasher},
sync::Once,
};
Expand Down Expand Up @@ -133,8 +132,8 @@ impl<'p> Environment<'p> {
"osx-arm64 (Apple Silicon) is not supported by the pixi.toml, falling back to osx-64 (emulated with Rosetta)"
);
// Create a file to prevent the warning from showing up multiple times. Also ignore the result.
fs::create_dir_all(warn_folder).and_then(|_| {
std::fs::File::create(emulation_warn)
fs_err::create_dir_all(warn_folder).and_then(|_| {
fs_err::File::create(emulation_warn)
}).ok();
}
});
Expand All @@ -152,8 +151,8 @@ impl<'p> Environment<'p> {
"win-arm64 is not supported by the pixi.toml, falling back to win-64 (emulation)"
);
// Create a file to prevent the warning from showing up multiple times. Also ignore the result.
fs::create_dir_all(warn_folder).and_then(|_| {
std::fs::File::create(emulation_warn)
fs_err::create_dir_all(warn_folder).and_then(|_| {
fs_err::File::create(emulation_warn)
}).ok();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/task/file_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn compute_file_hash(path: &Path) -> Result<String, FileHashesError> {
mod test {
use super::*;
use assert_matches::assert_matches;
use std::fs::{create_dir, write};
use fs_err::{create_dir, write};
use tempfile::tempdir;

#[tokio::test]
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const EXDEV: i32 = 17;
/// the file if possible and otherwise copying the file and removing the
/// original.
pub(crate) fn move_file(from: &Path, to: &Path) -> Result<(), MoveError> {
if let Err(e) = std::fs::rename(from, to) {
if let Err(e) = fs_err::rename(from, to) {
if e.raw_os_error() == Some(EXDEV) {
std::fs::copy(from, to).map_err(MoveError::CopyFailed)?;
std::fs::remove_file(from).map_err(MoveError::FailedToRemove)?
fs_err::copy(from, to).map_err(MoveError::CopyFailed)?;
fs_err::remove_file(from).map_err(MoveError::FailedToRemove)?
} else {
return Err(MoveError::MoveFailed(e));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/integration_rust/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl PixiControl {

// Add default project config
let pixi_path = tempdir.path().join(".pixi");
std::fs::create_dir_all(&pixi_path).unwrap();
std::fs::write(pixi_path.join("config.toml"), DEFAULT_PROJECT_CONFIG).unwrap();
fs_err::create_dir_all(&pixi_path).unwrap();
fs_err::write(pixi_path.join("config.toml"), DEFAULT_PROJECT_CONFIG).unwrap();

// Hide the progress bars for the tests
// Otherwise the override the test output
Expand All @@ -229,15 +229,15 @@ impl PixiControl {
/// Creates a new PixiControl instance from an existing manifest
pub fn from_manifest(manifest: &str) -> miette::Result<PixiControl> {
let pixi = Self::new()?;
std::fs::write(pixi.manifest_path(), manifest)
fs_err::write(pixi.manifest_path(), manifest)
.into_diagnostic()
.context("failed to write pixi.toml")?;
Ok(pixi)
}

/// Updates the complete manifest
pub fn update_manifest(&self, manifest: &str) -> miette::Result<()> {
std::fs::write(self.manifest_path(), manifest)
fs_err::write(self.manifest_path(), manifest)
.into_diagnostic()
.context("failed to write pixi.toml")?;
Ok(())
Expand Down Expand Up @@ -278,7 +278,7 @@ impl PixiControl {

/// Get the manifest contents
pub fn manifest_contents(&self) -> miette::Result<String> {
std::fs::read_to_string(self.manifest_path())
fs_err::read_to_string(self.manifest_path())
.into_diagnostic()
.context("failed to read manifest")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_rust/init_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn init_from_existing_pyproject_toml() {
let project_path = pixi.project_path();
let pyproject_toml = project_path.join("pyproject.toml");
let pyproject_toml_contents = include_str!("../data/pixi_tomls/pyproject_no_pixi.toml");
std::fs::write(&pyproject_toml, pyproject_toml_contents).unwrap();
fs_err::write(&pyproject_toml, pyproject_toml_contents).unwrap();

// Init a new project
pixi.init()
Expand Down
Loading

0 comments on commit fcad8e9

Please sign in to comment.