-
Notifications
You must be signed in to change notification settings - Fork 3k
Add hint to use uv self version when uv version cannot find a project
#14738
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 7 commits
afa915b
bfcd975
0c66f2d
ee9198c
9dfb59f
2b7765d
cae0ed1
f7d7b69
92666e5
826b07b
399cf76
0b2560d
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 |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ use uv_python::{PythonDownloads, PythonPreference, PythonRequest}; | |
| use uv_settings::PythonInstallMirrors; | ||
| use uv_workspace::pyproject_mut::Error; | ||
| use uv_workspace::{ | ||
| DiscoveryOptions, WorkspaceCache, | ||
| DiscoveryOptions, WorkspaceCache, WorkspaceError, | ||
| pyproject_mut::{DependencyTarget, PyProjectTomlMut}, | ||
| }; | ||
| use uv_workspace::{VirtualProject, Workspace}; | ||
|
|
@@ -59,6 +59,7 @@ pub(crate) async fn project_version( | |
| output_format: VersionFormat, | ||
| project_dir: &Path, | ||
| package: Option<PackageName>, | ||
| explicit_project: bool, | ||
| dry_run: bool, | ||
| locked: bool, | ||
| frozen: bool, | ||
|
|
@@ -78,7 +79,7 @@ pub(crate) async fn project_version( | |
| preview: PreviewMode, | ||
| ) -> Result<ExitStatus> { | ||
| // Read the metadata | ||
| let project = find_target(project_dir, package.as_ref()).await?; | ||
| let project = find_target(project_dir, package.as_ref(), explicit_project).await?; | ||
|
|
||
| let pyproject_path = project.root().join("pyproject.toml"); | ||
| let Some(name) = project.project_name().cloned() else { | ||
|
|
@@ -325,10 +326,25 @@ pub(crate) async fn project_version( | |
| Ok(status) | ||
| } | ||
|
|
||
| /// Helper function to enhance workspace errors with helpful hints | ||
| fn enhance_workspace_error(err: WorkspaceError, explicit_project: bool) -> anyhow::Error { | ||
|
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. Let's call this
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. Renamed the function from |
||
| if matches!(err, WorkspaceError::MissingPyprojectToml) && !explicit_project { | ||
| anyhow!( | ||
| "{}\n\n{}{} If you meant to view uv's version, use `{}` instead", | ||
| err, | ||
| "hint".bold().cyan(), | ||
| ":".bold(), | ||
| "uv self version".green() | ||
| ) | ||
| } else { | ||
| err.into() | ||
| } | ||
| } | ||
|
|
||
| /// Find the pyproject.toml we're modifying | ||
| /// | ||
| /// Note that `uv version` never needs to support PEP 723 scripts, as those are unversioned. | ||
| async fn find_target(project_dir: &Path, package: Option<&PackageName>) -> Result<VirtualProject> { | ||
| async fn find_target(project_dir: &Path, package: Option<&PackageName>, explicit_project: bool) -> Result<VirtualProject> { | ||
| // Find the project in the workspace. | ||
| // No workspace caching since `uv version` changes the workspace definition. | ||
| let project = if let Some(package) = package { | ||
|
|
@@ -338,7 +354,8 @@ async fn find_target(project_dir: &Path, package: Option<&PackageName>) -> Resul | |
| &DiscoveryOptions::default(), | ||
| &WorkspaceCache::default(), | ||
| ) | ||
| .await? | ||
| .await | ||
| .map_err(|err| enhance_workspace_error(err, explicit_project))? | ||
| .with_current_project(package.clone()) | ||
| .with_context(|| format!("Package `{package}` not found in workspace"))?, | ||
| ) | ||
|
|
@@ -348,7 +365,8 @@ async fn find_target(project_dir: &Path, package: Option<&PackageName>) -> Resul | |
| &DiscoveryOptions::default(), | ||
| &WorkspaceCache::default(), | ||
| ) | ||
| .await? | ||
| .await | ||
| .map_err(|err| enhance_workspace_error(err, explicit_project))? | ||
| }; | ||
| Ok(project) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1687,20 +1687,22 @@ fn version_get_fallback_missing_strict() -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| // Should error if this pyproject.toml is missing | ||
| // and --preview was passed explicitly. | ||
|
|
||
|
|
||
| // Should error with hint if pyproject.toml is missing in normal mode | ||
|
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 use
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. Changed the comment to use |
||
| #[test] | ||
| fn version_get_fallback_missing_strict_preview() -> Result<()> { | ||
| fn version_get_missing_with_hint() -> Result<()> { | ||
| let context = TestContext::new("3.12"); | ||
|
|
||
| uv_snapshot!(context.filters(), context.version() | ||
| .arg("--preview"), @r" | ||
| uv_snapshot!(context.filters(), context.version(), @r" | ||
| success: false | ||
| exit_code: 2 | ||
| ----- stdout ----- | ||
|
|
||
| ----- stderr ----- | ||
| error: No `pyproject.toml` found in current directory or any parent directory | ||
|
|
||
| [1m[36mhint[0m[0m[1m:[0m If you meant to view uv's version, use `[32muv self version[0m` instead | ||
|
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 filter color codes during tests
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. Fixed the test to expect plain text output without ANSI color codes that are filtered by the test framework. Changes in commit f7d7b69. |
||
| "); | ||
|
|
||
| Ok(()) | ||
|
|
||
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.
We should probably be more specific here?
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.
Made the function documentation more specific to clearly explain what it does, when it applies the hint, and under what conditions. Updated in commit c74b67c.