From f00c3f26a2d9cb5976463daf5199d946ea0aa8fd Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Tue, 23 Jul 2024 03:13:05 +0800 Subject: [PATCH] `uv init` ignores workspace when `--isolated` flag is used (#5290) ## Summary Per https://github.com/astral-sh/uv/pull/5250#issuecomment-2242137762 > It would also be great to have an argument (perhaps leveraging the global isolated option) that allows us to disable workspace discovery when we don't want to add a project as a member. ## Test Plan ```sh $cargo run -- init work $ cargo run -- init work/pkg-a --isolated warning: `uv init` is experimental and may change without warning Initialized project sub-c in /tmp/work ``` --- crates/uv/src/commands/project/init.rs | 17 ++++++---- crates/uv/src/lib.rs | 1 + crates/uv/tests/init.rs | 45 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/crates/uv/src/commands/project/init.rs b/crates/uv/src/commands/project/init.rs index 4825c7e42ccc..2887a5dd9cf9 100644 --- a/crates/uv/src/commands/project/init.rs +++ b/crates/uv/src/commands/project/init.rs @@ -19,6 +19,7 @@ pub(crate) async fn init( explicit_path: Option, name: Option, no_readme: bool, + isolated: bool, preview: PreviewMode, printer: Printer, ) -> Result { @@ -26,11 +27,9 @@ pub(crate) async fn init( warn_user_once!("`uv init` is experimental and may change without warning"); } - let current_dir = std::env::current_dir()?.canonicalize()?; - // Default to the current directory if a path was not provided. let path = match explicit_path { - None => current_dir.clone(), + None => std::env::current_dir()?.canonicalize()?, Some(ref path) => PathBuf::from(path), }; @@ -67,10 +66,14 @@ pub(crate) async fn init( let path = path.canonicalize()?; // Discover the current workspace, if it exists. - let workspace = match ProjectWorkspace::discover(&path, None).await { - Ok(project) => Some(project), - Err(WorkspaceError::MissingPyprojectToml) => None, - Err(err) => return Err(err.into()), + let workspace = if isolated { + None + } else { + match ProjectWorkspace::discover(&path, None).await { + Ok(project) => Some(project), + Err(WorkspaceError::MissingPyprojectToml) => None, + Err(err) => return Err(err.into()), + } }; // Create the `pyproject.toml`. diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index 2e2a16ccead7..f8a662dad5b7 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -834,6 +834,7 @@ async fn run_project( args.path, args.name, args.no_readme, + globals.isolated, globals.preview, printer, ) diff --git a/crates/uv/tests/init.rs b/crates/uv/tests/init.rs index 6954f7544cdb..f8edceb09104 100644 --- a/crates/uv/tests/init.rs +++ b/crates/uv/tests/init.rs @@ -511,3 +511,48 @@ fn init_invalid_names() -> Result<()> { Ok(()) } + +#[test] +fn init_workspace_isolated() -> Result<()> { + let context = TestContext::new("3.12"); + + 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" + "#, + })?; + + let child = context.temp_dir.join("foo"); + fs_err::create_dir(&child)?; + + uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--isolated"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + warning: `uv init` is experimental and may change without warning + Initialized project foo + "###); + + let workspace = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?; + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + workspace, @r###" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + "### + ); + }); + + Ok(()) +}