-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: uv format from project root #15440
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
Changes from all commits
56f4f45
50d0f5a
d0d3275
58c2095
ae4bb9c
737c055
a0fc2f2
dd6b1fa
6fc9156
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) | ||
| .await?; | ||
|
|
||
| // Parse version if provided | ||
| let version = version.as_deref().map(Version::from_str).transpose()?; | ||
|
|
||
|
|
@@ -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()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem! |
||
| command.arg("format"); | ||
|
|
||
| if check { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,60 @@ fn format_project() -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn format_from_project_root() -> Result<()> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
jorgehermo9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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(&[]); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inspired from https://github.com/astral-sh/uv/blob/main/crates/uv/src/commands/project/export.rs#L108