Repository: astral-sh/uv
File: crates/uv/src/commands/project/format.rs (Rust)

Code before:
    version: Option<String>,
    network_settings: NetworkSettings,
    cache: Cache,
    printer: Printer,
    preview: Preview,
) -> Result<ExitStatus> {
    // Check if the format feature is in preview
    if !preview.is_enabled(PreviewFeatures::FORMAT) {
        warn_user!(
            "`uv format` is experimental and may change without warning. Pass `--preview-features {}` to disable this warning.",
            PreviewFeatures::FORMAT
        );
    }

    let workspace_cache = WorkspaceCache::default();
    let project =
        VirtualProject::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache).await;
    let working_dir = match project {
        // If we found a project, we use the project root
        Ok(proj) => proj.root().to_owned(),
        // If there is a problem finding a project, we just use the provided directory
        // e.g. unmanaged projects
        Err(WorkspaceError::MissingPyprojectToml)
        | Err(WorkspaceError::MissingProject(_))
        | Err(WorkspaceError::NonWorkspace(_)) => project_dir.to_owned(),
        Err(err) => return Err(err.into()),
    };

    // Parse version if provided
    let version = version.as_deref().map(Version::from_str).transpose()?;

    let client = BaseClientBuilder::new()
        .retries_from_env()?
        .connectivity(network_settings.connectivity)
        .native_tls(network_settings.native_tls)
        .allow_insecure_host(network_settings.allow_insecure_host.clone())
        .build();

    // Get the path to Ruff, downloading it if necessary
    let reporter = BinaryDownloadReporter::single(printer);
    let default_version = Binary::Ruff.default_version();
    let version = version.as_ref().unwrap_or(&default_version);
    let ruff_path = bin_install(Binary::Ruff, version, &client, &cache, &reporter)
        .await
        .with_context(|| format!("Failed to install ruff {version}"))?;

    let mut command = Command::new(&ruff_path);
    command.current_dir(working_dir);
    command.arg("format");

    if check {

Review comment (suggestion):
I'm not sure on the behaviour in this case. Should we propagate the error to the caller (previous behaviour), or in this case, should we `warn_user!`  about the error and fallback to `project_dir` (as the previous branch does)?

I prefer to not do "magic fallbacks" when there is an error such as this (for example, malformed pyproject.toml) and to just propagate them to the user instead of continue with the command. Let me know your thoughts please.

In https://github.com/astral-sh/uv/blob/main/crates/uv/src/commands/venv.rs#L97 such fallback is done, logging a warning in these cases and continue working using defaults


Code after:
    version: Option<String>,
    network_settings: NetworkSettings,
    cache: Cache,
    printer: Printer,
    preview: Preview,
) -> Result<ExitStatus> {
    // Check if the format feature is in preview
    if !preview.is_enabled(PreviewFeatures::FORMAT) {
        warn_user!(
            "`uv format` is experimental and may change without warning. Pass `--preview-features {}` to disable this warning.",
            PreviewFeatures::FORMAT
        );
    }

    let workspace_cache = WorkspaceCache::default();
    let target_dir =
        match VirtualProject::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache)
            .await
        {
            // If we found a project, we use the project root
            Ok(proj) => proj.root().to_owned(),
            // If there is a problem finding a project, we just use the provided directory,
            // e.g., for unmanaged projects
            Err(
                WorkspaceError::MissingPyprojectToml
                | WorkspaceError::MissingProject(_)
                | WorkspaceError::NonWorkspace(_),
            ) => project_dir.to_owned(),
            Err(err) => return Err(err.into()),
        };

    // Parse version if provided
    let version = version.as_deref().map(Version::from_str).transpose()?;

    let client = BaseClientBuilder::new()
        .retries_from_env()?
        .connectivity(network_settings.connectivity)
        .native_tls(network_settings.native_tls)
        .allow_insecure_host(network_settings.allow_insecure_host.clone())
        .build();

    // Get the path to Ruff, downloading it if necessary
    let reporter = BinaryDownloadReporter::single(printer);
    let default_version = Binary::Ruff.default_version();
    let version = version.as_ref().unwrap_or(&default_version);
    let ruff_path = bin_install(Binary::Ruff, version, &client, &cache, &reporter)
        .await
        .with_context(|| format!("Failed to install ruff {version}"))?;

    let mut command = Command::new(&ruff_path);
    command.current_dir(target_dir);