Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use fs-err in more places #2636

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

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

21 changes: 21 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,24 @@ ignore-interior-mutability = [
"pixi::project::environment::Environment",
"pixi::project::solve_group::SolveGroup",
]

disallowed-methods = [
"std::fs::canonicalize",
"std::fs::copy",
"std::fs::create_dir",
"std::fs::create_dir_all",
"std::fs::hard_link",
"std::fs::metadata",
"std::fs::read",
"std::fs::read_dir",
"std::fs::read_link",
"std::fs::read_to_string",
"std::fs::remove_dir",
"std::fs::remove_dir_all",
"std::fs::remove_file",
"std::fs::rename",
"std::fs::set_permissions",
"std::fs::soft_link",
"std::fs::symlink_metadata",
"std::fs::write",
]
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_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "0.1.0"
clap = { workspace = true, features = ["std", "derive", "env"] }
console = { workspace = true }
dirs = { workspace = true }
fs-err = { workspace = true }
itertools = { workspace = true }
miette = { workspace = true }
pixi_consts = { workspace = true }
Expand Down
7 changes: 3 additions & 4 deletions crates/pixi_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use reqwest_middleware::ClientWithMiddleware;
use serde::{de::IntoDeserializer, Deserialize, Serialize};
use std::{
collections::{BTreeSet as Set, HashMap},
fs,
path::{Path, PathBuf},
process::{Command, Stdio},
str::FromStr,
Expand Down Expand Up @@ -747,7 +746,7 @@ impl Config {
/// I/O errors or parsing errors
pub fn from_path(path: &Path) -> miette::Result<Config> {
tracing::debug!("Loading config from {}", path.display());
let s = fs::read_to_string(path)
let s = fs_err::read_to_string(path)
.into_diagnostic()
.wrap_err(format!("failed to read config from '{}'", path.display()))?;

Expand Down Expand Up @@ -1201,13 +1200,13 @@ impl Config {
tracing::debug!("Saving config to: {}", to.display());

let parent = to.parent().expect("config path should have a parent");
fs::create_dir_all(parent)
fs_err::create_dir_all(parent)
.into_diagnostic()
.wrap_err(format!(
"failed to create directories in '{}'",
parent.display()
))?;
fs::write(to, contents)
fs_err::write(to, contents)
.into_diagnostic()
.wrap_err(format!("failed to write config to '{}'", to.display()))
}
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
8 changes: 3 additions & 5 deletions crates/pixi_glob/src/glob_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ impl<'t> GlobSet<'t> {
#[cfg(test)]
mod tests {
use super::GlobSet;
use std::{
fs::{create_dir, File},
path::PathBuf,
};
use fs_err::File;
use std::path::PathBuf;
use tempfile::tempdir;

#[test]
Expand All @@ -111,7 +109,7 @@ mod tests {
File::create(root_path.join("include1.txt")).unwrap();
File::create(root_path.join("include2.log")).unwrap();
File::create(root_path.join("exclude.txt")).unwrap();
create_dir(root_path.join("subdir")).unwrap();
fs_err::create_dir(root_path.join("subdir")).unwrap();
File::create(root_path.join("subdir/include_subdir.txt")).unwrap();

// Test globs: include all .txt but exclude exclude.txt
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
4 changes: 2 additions & 2 deletions crates/pixi_manifest/src/pyproject.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fs, path::PathBuf, str::FromStr};
use std::{collections::HashMap, path::PathBuf, str::FromStr};

use indexmap::IndexMap;
use miette::{Diagnostic, IntoDiagnostic, Report, WrapErr};
Expand Down Expand Up @@ -58,7 +58,7 @@ impl PyProjectManifest {

/// Parses a `pyproject.toml` file into a PyProjectManifest
pub fn from_path(path: &PathBuf) -> Result<Self, Report> {
let source = fs::read_to_string(path)
let source = fs_err::read_to_string(path)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to read file: {:?}", path))?;
Self::from_toml_str(&source).into_diagnostic()
Expand Down
3 changes: 3 additions & 0 deletions crates/pixi_trampoline/Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/pixi_trampoline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ strip = true

[dependencies]
ctrlc = "3.4"
miette = "7.2.0"
fs-err = "3.0.0"
miette = "7.4.0"
pixi_utils = { path = "../pixi_utils" }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
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
8 changes: 4 additions & 4 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 @@ -187,7 +187,7 @@ fn parse_channels(channels: Vec<NamedChannelOrUrl>) -> Vec<NamedChannelOrUrl> {

#[cfg(test)]
mod tests {
use std::{fs, io::Write, path::Path, str::FromStr};
use std::{io::Write, path::Path, str::FromStr};

use rattler_conda_types::{MatchSpec, ParseStrictness::Strict};

Expand Down Expand Up @@ -271,7 +271,7 @@ mod tests {
.join("tests")
.join("environment_yamls");

let entries = match fs::read_dir(test_files_path) {
let entries = match fs_err::read_dir(test_files_path) {
Ok(entries) => entries,
Err(e) => panic!("Failed to read directory: {}", e),
};
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
2 changes: 1 addition & 1 deletion crates/pixi_utils/src/prefix_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl PrefixGuard {
let guard_path = prefix.join(GUARD_PATH);

// Ensure that the directory exists
std::fs::create_dir_all(guard_path.parent().unwrap())?;
fs_err::create_dir_all(guard_path.parent().unwrap())?;

// Open the file
Ok(Self {
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
12 changes: 7 additions & 5 deletions src/cli/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Display, fs, path::PathBuf};
use std::{fmt::Display, path::PathBuf};

use chrono::{DateTime, Local};
use clap::Parser;
Expand Down Expand Up @@ -316,24 +316,26 @@ impl Display for Info {

/// Returns the size of a directory
fn dir_size(path: impl Into<PathBuf>) -> miette::Result<String> {
fn dir_size(mut dir: fs::ReadDir) -> miette::Result<u64> {
fn dir_size(mut dir: fs_err::ReadDir) -> miette::Result<u64> {
dir.try_fold(0, |acc, file| {
let file = file.into_diagnostic()?;
let size = match file.metadata().into_diagnostic()? {
data if data.is_dir() => dir_size(fs::read_dir(file.path()).into_diagnostic()?)?,
data if data.is_dir() => {
dir_size(fs_err::read_dir(file.path()).into_diagnostic()?)?
}
data => data.len(),
};
Ok(acc + size)
})
}

let size = dir_size(fs::read_dir(path.into()).into_diagnostic()?)?;
let size = dir_size(fs_err::read_dir(path.into()).into_diagnostic()?)?;
Ok(format!("{} MiB", size / 1024 / 1024))
}

/// Returns last update time of file, formatted: DD-MM-YYYY H:M:S
fn last_updated(path: impl Into<PathBuf>) -> miette::Result<String> {
let time = fs::metadata(path.into())
let time = fs_err::metadata(path.into())
.into_diagnostic()?
.modified()
.into_diagnostic()?;
Expand Down
Loading
Loading