Skip to content
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
10 changes: 9 additions & 1 deletion crates/uv/src/commands/project/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use uv_client::BaseClientBuilder;
use uv_configuration::{Preview, PreviewFeatures};
use uv_pep440::Version;
use uv_warnings::warn_user;
use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceCache};

use crate::child::run_to_completion;
use crate::commands::ExitStatus;
Expand All @@ -36,6 +37,12 @@ pub(crate) async fn format(
PreviewFeatures::FORMAT
);
}

let workspace_cache = WorkspaceCache::default();
let project =
VirtualProject::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.await?;

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

Expand All @@ -55,7 +62,8 @@ pub(crate) async fn format(
.context("Failed to install ruff {version}")?;

let mut command = Command::new(&ruff_path);
command.current_dir(project_dir);
// Run ruff in the project root
command.current_dir(project.root());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this implementation behaves for a workspace with multiple package. running just uv format would always run the format from the workspace root. What is the intended way of working with specific packages of a workspace?
using uv run format --project package_a and uv run format --project package_b?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you wanted to run it in a workspace member it'd be uv format --package <name>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, perfect then!

Thanks @zanieb, I'm kind of new with uv and I'm sorry for those basic questions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem!

command.arg("format");

if check {
Expand Down
54 changes: 54 additions & 0 deletions crates/uv/tests/it/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,60 @@ fn format_project() -> Result<()> {
Ok(())
}

#[test]
fn format_from_project_root() -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add a --no-project test case. I'd expect that to not use the project root.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support that yet actually, so that's a follow-up

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may implement that as well! if no one takes it first 😃

let context = TestContext::new_with_versions(&[]);

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"#})?;

// Create an unformatted Python file
let main_py = context.temp_dir.child("main.py");
main_py.write_str(indoc! {r#"
import sys
def hello():
print( "Hello, World!" )
if __name__=="__main__":
hello( )
"#})?;

let subdir = context.temp_dir.child("subdir");
fs_err::create_dir_all(&subdir)?;

// Using format from a subdirectory should still run in the project root
uv_snapshot!(context.filters(), context.format().current_dir(&subdir), @r"
success: true
exit_code: 0
----- stdout -----
1 file reformatted

----- stderr -----
warning: `uv format` is experimental and may change without warning. Pass `--preview-features format` to disable this warning.
");

// Check that the file was formatted
let formatted_content = fs_err::read_to_string(&main_py)?;
assert_snapshot!(formatted_content, @r#"
import sys


def hello():
print("Hello, World!")


if __name__ == "__main__":
hello()
"#);

Ok(())
}

#[test]
fn format_relative_project() -> Result<()> {
let context = TestContext::new_with_versions(&[]);
Expand Down
Loading