Repository: DioxusLabs/dioxus
File: packages/cli/src/cargo_toml.rs (Rust)

Code before:

use cargo_toml::{Manifest, Profile, Profiles};

/// Load the manifest from a path inheriting from the global config where needed
pub fn load_manifest_from_path(path: &Path) -> Result<Manifest, cargo_toml::Error> {
    let mut original = Manifest::from_path(path)?;
    // Merge the .cargo/config.toml if it exists
    extend_manifest_config_toml(&mut original, &path.join(".cargo").join("config.toml"));

    // Merge the global cargo config if it exists
    if let Some(global_config) = global_cargo_config_path() {
        extend_manifest_config_toml(&mut original, &global_config);
    }
    Ok(original)
}

/// Get the global cargo config path if it exists
fn global_cargo_config_path() -> Option<PathBuf> {
    if cfg!(windows) {
        std::env::var_os("USERPROFILE").map(|user_profile| {
            PathBuf::from(user_profile)
                .join(".cargo")
                .join("config.toml")
        })
    } else if cfg!(unix) {
        dirs::home_dir().map(|home_dir| home_dir.join(".cargo").join("config.toml"))
    } else {
        None
    }
}

// Extend a manifest with a config.toml if it exists
fn extend_manifest_config_toml(manifest: &mut Manifest, path: &Path) {
    // Read the config.toml if it exists
    let Ok(config) = std::fs::read_to_string(path) else {
        return;
    };
    let Ok(config) = config.parse::<toml::Value>() else {
        return;
    };
    // Try to parse profiles
    if let Some(profiles) = config.get("profile").and_then(|p| p.as_table()) {
        let profiles: cargo_toml::Profiles =
            toml::from_str(&profiles.to_string()).unwrap_or_default();
        merge_profiles(&mut manifest.profile, profiles);
    }
}

/// Merge the new profiles into the target profiles. Keep the existing values if they exist.
fn merge_profiles(target: &mut Profiles, new: Profiles) {
    if let Some(new_release) = new.release {

Review comment (suggestion):
Not necessarily in home directory. I think `$CARGO_HOME` needs to be checked first and if unset default to home directory.

Code after:

use cargo_toml::{Manifest, Profile, Profiles};

/// Load the manifest from a path inheriting from the global config where needed
pub fn load_manifest_from_path(path: &Path) -> Result<Manifest, cargo_toml::Error> {
    let mut original = Manifest::from_path(path)?;

    // Merge the .cargo/config.toml if it exists
    extend_manifest_config_toml(&mut original, &path.join(".cargo").join("config.toml"));

    // Merge the global cargo config if it exists
    if let Some(global_config) = global_cargo_config_path() {
        extend_manifest_config_toml(&mut original, &global_config);
    }

    Ok(original)
}

/// Get the path to cargo home
fn cargo_home() -> Option<PathBuf> {
    // If the cargo home env var is set, use that
    if let Some(cargo_home) = std::env::var_os("CARGO_HOME") {
        return Some(PathBuf::from(cargo_home));
    }

    // Otherwise, use the default location
    if cfg!(windows) {
        std::env::var_os("USERPROFILE")
            .map(|user_profile| PathBuf::from(user_profile).join(".cargo"))
    } else if cfg!(unix) {
        dirs::home_dir().map(|home_dir| home_dir.join(".cargo"))
    } else {
        None
    }
}

/// Get the global cargo config path if it exists
fn global_cargo_config_path() -> Option<PathBuf> {
    cargo_home().map(|cargo_home| cargo_home.join("config.toml"))
}

// Extend a manifest with a config.toml if it exists
fn extend_manifest_config_toml(manifest: &mut Manifest, path: &Path) {
    // Read the config.toml if it exists
    let Ok(config) = std::fs::read_to_string(path) else {
        return;
    };

    let Ok(config) = config.parse::<toml::Value>() else {
        return;
    };