-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add uv workspace dir command
#16678
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
Merged
Merged
Add uv workspace dir command
#16678
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aaca6e5
Initial implementation of workspace dir
mikaylathompson 8401729
Add tests, clean up, doc string
mikaylathompson e2437c7
Formatting fixes
mikaylathompson a044a19
Update docstring
mikaylathompson 8569c7f
Update for review comments
mikaylathompson b9be7f5
Use println from anstream
mikaylathompson c59050e
Add separate workspace-dir preview flag
mikaylathompson e249d84
println -> use printer
mikaylathompson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||||||||||||||||||||||||||||||||||||||
| use anstream::println; | ||||||||||||||||||||||||||||||||||||||||||
| use std::path::Path; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use anyhow::{Result, bail}; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use owo_colors::OwoColorize; | ||||||||||||||||||||||||||||||||||||||||||
| use uv_fs::Simplified; | ||||||||||||||||||||||||||||||||||||||||||
| use uv_normalize::PackageName; | ||||||||||||||||||||||||||||||||||||||||||
| use uv_preview::{Preview, PreviewFeatures}; | ||||||||||||||||||||||||||||||||||||||||||
| use uv_warnings::warn_user; | ||||||||||||||||||||||||||||||||||||||||||
| use uv_workspace::{DiscoveryOptions, Workspace, WorkspaceCache}; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use crate::commands::ExitStatus; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Print the path to the workspace dir | ||||||||||||||||||||||||||||||||||||||||||
| pub(crate) async fn dir( | ||||||||||||||||||||||||||||||||||||||||||
| package_name: Option<PackageName>, | ||||||||||||||||||||||||||||||||||||||||||
| project_dir: &Path, | ||||||||||||||||||||||||||||||||||||||||||
| preview: Preview, | ||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<ExitStatus> { | ||||||||||||||||||||||||||||||||||||||||||
| if preview.is_enabled(PreviewFeatures::WORKSPACE_DIR) { | ||||||||||||||||||||||||||||||||||||||||||
| warn_user!( | ||||||||||||||||||||||||||||||||||||||||||
| "The `uv workspace dir` command is experimental and may change without warning. Pass `--preview-features {}` to disable this warning.", | ||||||||||||||||||||||||||||||||||||||||||
| PreviewFeatures::WORKSPACE_DIR | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let workspace_cache = WorkspaceCache::default(); | ||||||||||||||||||||||||||||||||||||||||||
| let workspace = | ||||||||||||||||||||||||||||||||||||||||||
| Workspace::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache).await?; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let dir: &Path = match package_name { | ||||||||||||||||||||||||||||||||||||||||||
| None => workspace.install_path().as_path(), | ||||||||||||||||||||||||||||||||||||||||||
| Some(package) => { | ||||||||||||||||||||||||||||||||||||||||||
| if let Some(p) = workspace.packages().get(&package) { | ||||||||||||||||||||||||||||||||||||||||||
| p.root().as_path() | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| bail!("Package `{package}` not found in workspace.") | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+34
to
+43
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. This code compiles without the type cast:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| println!("{}", dir.simplified_display().cyan()); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Ok(ExitStatus::Success) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub(crate) mod dir; | ||
| pub(crate) mod metadata; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,4 +141,5 @@ mod workflow; | |
|
|
||
| mod extract; | ||
| mod workspace; | ||
| mod workspace_dir; | ||
| mod workspace_metadata; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,123 @@ | ||||||
| use std::env; | ||||||
|
|
||||||
| use anyhow::Result; | ||||||
| use assert_cmd::assert::OutputAssertExt; | ||||||
| use assert_fs::fixture::PathChild; | ||||||
|
|
||||||
| use crate::common::{TestContext, copy_dir_ignore, uv_snapshot}; | ||||||
|
|
||||||
| /// Test basic output for a simple workspace with one member. | ||||||
| #[test] | ||||||
| fn workspace_dir_simple() { | ||||||
| let context = TestContext::new("3.12"); | ||||||
|
|
||||||
| // Initialize a workspace with one member | ||||||
| context.init().arg("foo").assert().success(); | ||||||
|
|
||||||
| let workspace = context.temp_dir.child("foo"); | ||||||
|
|
||||||
| uv_snapshot!(context.filters(), context.workspace_dir().current_dir(&workspace), @r###" | ||||||
| success: true | ||||||
| exit_code: 0 | ||||||
| ----- stdout ----- | ||||||
| [TEMP_DIR]/foo | ||||||
| ----- stderr ----- | ||||||
| "### | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| // Workspace dir output when run with `--package` | ||||||
|
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. nit:
Suggested change
|
||||||
| #[test] | ||||||
| fn workspace_dir_specific_package() { | ||||||
| let context = TestContext::new("3.12"); | ||||||
| context.init().arg("foo").assert().success(); | ||||||
| context.init().arg("foo/bar").assert().success(); | ||||||
| let workspace = context.temp_dir.child("foo"); | ||||||
|
|
||||||
| // root workspace | ||||||
| uv_snapshot!(context.filters(), context.workspace_dir().current_dir(&workspace), @r###" | ||||||
| success: true | ||||||
| exit_code: 0 | ||||||
| ----- stdout ----- | ||||||
| [TEMP_DIR]/foo | ||||||
| ----- stderr ----- | ||||||
| "### | ||||||
| ); | ||||||
|
|
||||||
| // with --package bar | ||||||
| uv_snapshot!(context.filters(), context.workspace_dir().arg("--package").arg("bar").current_dir(&workspace), @r###" | ||||||
| success: true | ||||||
| exit_code: 0 | ||||||
| ----- stdout ----- | ||||||
| [TEMP_DIR]/foo/bar | ||||||
| ----- stderr ----- | ||||||
| "### | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /// Test output when run from a workspace member directory. | ||||||
| #[test] | ||||||
| fn workspace_metadata_from_member() -> Result<()> { | ||||||
| let context = TestContext::new("3.12"); | ||||||
| let workspace = context.temp_dir.child("workspace"); | ||||||
|
|
||||||
| let albatross_workspace = context | ||||||
| .workspace_root | ||||||
| .join("scripts/workspaces/albatross-root-workspace"); | ||||||
|
|
||||||
| copy_dir_ignore(albatross_workspace, &workspace)?; | ||||||
|
|
||||||
| let member_dir = workspace.join("packages").join("bird-feeder"); | ||||||
|
|
||||||
| uv_snapshot!(context.filters(), context.workspace_dir().current_dir(&member_dir), @r###" | ||||||
| success: true | ||||||
| exit_code: 0 | ||||||
| ----- stdout ----- | ||||||
| [TEMP_DIR]/workspace | ||||||
| ----- stderr ----- | ||||||
| "### | ||||||
| ); | ||||||
|
|
||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| /// Test workspace dir error output for a non-existent package. | ||||||
| #[test] | ||||||
| fn workspace_dir_package_doesnt_exist() { | ||||||
| let context = TestContext::new("3.12"); | ||||||
|
|
||||||
| // Initialize a workspace with one member | ||||||
| context.init().arg("foo").assert().success(); | ||||||
|
|
||||||
| let workspace = context.temp_dir.child("foo"); | ||||||
|
|
||||||
| uv_snapshot!(context.filters(), context.workspace_dir().arg("--package").arg("bar").current_dir(&workspace), @r###" | ||||||
| success: false | ||||||
| exit_code: 2 | ||||||
| ----- stdout ----- | ||||||
| ----- stderr ----- | ||||||
| error: Package `bar` not found in workspace. | ||||||
| "### | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /// Test workspace dir error output when not in a project. | ||||||
| #[test] | ||||||
| fn workspace_metadata_no_project() { | ||||||
| let context = TestContext::new("3.12"); | ||||||
|
|
||||||
| uv_snapshot!(context.filters(), context.workspace_dir(), @r###" | ||||||
| success: false | ||||||
| exit_code: 2 | ||||||
| ----- stdout ----- | ||||||
| ----- stderr ----- | ||||||
| error: No `pyproject.toml` found in current directory or any parent directory | ||||||
| "### | ||||||
| ); | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(My own doc here is bad, I should fix that)