Skip to content

Commit

Permalink
fix: prefix_file rename (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored Mar 14, 2024
1 parent 835df28 commit 713d94b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
9 changes: 1 addition & 8 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{
consts,
environment::{get_up_to_date_prefix, verify_prefix_location_unchanged, LockFileUsage},
project::{manifest::PyPiRequirement, DependencyType, Project, SpecType},
FeatureName,
Expand Down Expand Up @@ -114,13 +113,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
let spec_platforms = &args.platform;

// Sanity check of prefix location
verify_prefix_location_unchanged(
project
.default_environment()
.dir()
.join(consts::PREFIX_FILE_NAME)
.as_path(),
)?;
verify_prefix_location_unchanged(project.default_environment().dir().as_path())?;

// Add the platform if it is not already present
let platforms_to_add = spec_platforms
Expand Down
9 changes: 1 addition & 8 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::convert::identity;
use std::str::FromStr;
use std::{collections::HashMap, path::PathBuf, string::String};

use crate::consts;
use clap::Parser;
use dialoguer::theme::ColorfulTheme;
use itertools::Itertools;
Expand Down Expand Up @@ -54,13 +53,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
let project = Project::load_or_else_discover(args.manifest_path.as_deref())?;

// Sanity check of prefix location
verify_prefix_location_unchanged(
project
.default_environment()
.dir()
.join(consts::PREFIX_FILE_NAME)
.as_path(),
)?;
verify_prefix_location_unchanged(project.default_environment().dir().as_path())?;

// Extract the passed in environment name.
let explicit_environment = args
Expand Down
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lazy_static::lazy_static;
pub const PROJECT_MANIFEST: &str = "pixi.toml";
pub const PROJECT_LOCK_FILE: &str = "pixi.lock";
pub const PIXI_DIR: &str = ".pixi";
pub const PREFIX_FILE_NAME: &str = "prefix";
pub const PREFIX_FILE_NAME: &str = "pixi_env_prefix";
pub const ENVIRONMENTS_DIR: &str = "envs";
pub const SOLVE_GROUP_ENVIRONMENTS_DIR: &str = "solve-group-envs";
pub const PYPI_DEPENDENCIES: &str = "pypi-dependencies";
Expand Down
42 changes: 27 additions & 15 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ use std::{collections::HashMap, io::ErrorKind, path::Path, sync::Arc};
/// Verify the location of the prefix folder is not changed so the applied prefix path is still valid.
/// Errors when there is a file system error or the path does not align with the defined prefix.
/// Returns false when the file is not present.
pub fn verify_prefix_location_unchanged(prefix_file: &Path) -> miette::Result<()> {
match std::fs::read_to_string(prefix_file) {
pub fn verify_prefix_location_unchanged(environment_dir: &Path) -> miette::Result<()> {
let prefix_file = environment_dir
.join("conda-meta")
.join(consts::PREFIX_FILE_NAME);

tracing::info!(
"verifying prefix location is unchanged, with prefix file: {}",
prefix_file.display()
);

match std::fs::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.
Err(e) => Err(e).into_diagnostic(),
Err(e) => {
tracing::error!("failed to read prefix file: {}", prefix_file.display());
Err(e).into_diagnostic()
}
// Check if the path in the file aligns with the current path.
Ok(p) if prefix_file.starts_with(&p) => Ok(()),
Ok(p) => Err(miette::miette!(
Expand All @@ -50,11 +62,17 @@ pub fn verify_prefix_location_unchanged(prefix_file: &Path) -> miette::Result<()
}

/// Create the prefix location file.
/// Give it the file path of the required location to place it.
fn create_prefix_location_file(prefix_file: &Path) -> miette::Result<()> {
let parent = prefix_file
/// Give it the environment path to place it.
fn create_prefix_location_file(environment_dir: &Path) -> miette::Result<()> {
let prefix_file = environment_dir
.join("conda-meta")
.join(consts::PREFIX_FILE_NAME);

tracing::info!("create prefix file: {}", prefix_file.display());
let binding = prefix_file.clone();
let parent = binding
.parent()
.ok_or_else(|| miette::miette!("cannot find parent of '{}'", prefix_file.display()))?;
.ok_or_else(|| miette::miette!("cannot find parent of '{}'", binding.display()))?;

if parent.exists() {
let contents = parent.to_str().ok_or_else(|| {
Expand All @@ -71,13 +89,7 @@ fn create_prefix_location_file(prefix_file: &Path) -> miette::Result<()> {
/// 3. It verifies the absence of the `env` folder.
pub fn sanity_check_project(project: &Project) -> miette::Result<()> {
// Sanity check of prefix location
verify_prefix_location_unchanged(
project
.default_environment()
.dir()
.join(consts::PREFIX_FILE_NAME)
.as_path(),
)?;
verify_prefix_location_unchanged(project.default_environment().dir().as_path())?;

// Make sure the system requirements are met
verify_current_platform_has_required_virtual_packages(&project.default_environment())
Expand Down Expand Up @@ -303,7 +315,7 @@ pub async fn update_prefix_conda(
}

// Mark the location of the prefix
create_prefix_location_file(&prefix.root().join(consts::PREFIX_FILE_NAME))?;
create_prefix_location_file(prefix.root())?;

// Determine if the python version changed.
Ok(PythonStatus::from_transaction(&transaction))
Expand Down

0 comments on commit 713d94b

Please sign in to comment.