Skip to content

Commit

Permalink
uv init should not create nested workspace (#5293)
Browse files Browse the repository at this point in the history
## Summary

Resolves #5251
  • Loading branch information
j178 authored Jul 22, 2024
1 parent 26e042a commit d232bfe
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 21 deletions.
87 changes: 83 additions & 4 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct Workspace {
///
/// This table is overridden by the project sources.
sources: BTreeMap<PackageName, Source>,
/// The `pyproject.toml` of the workspace root.
pyproject_toml: PyProjectToml,
}

impl Workspace {
Expand Down Expand Up @@ -323,6 +325,11 @@ impl Workspace {
&self.sources
}

/// The `pyproject.toml` of the workspace.
pub fn pyproject_toml(&self) -> &PyProjectToml {
&self.pyproject_toml
}

/// Collect the workspace member projects from the `members` and `excludes` entries.
async fn collect_members(
workspace_root: PathBuf,
Expand Down Expand Up @@ -440,6 +447,7 @@ impl Workspace {
}
let workspace_sources = workspace_pyproject_toml
.tool
.clone()
.and_then(|tool| tool.uv)
.and_then(|uv| uv.sources)
.unwrap_or_default();
Expand All @@ -451,6 +459,7 @@ impl Workspace {
lock_path,
packages: workspace_members,
sources: workspace_sources,
pyproject_toml: workspace_pyproject_toml,
})
}
}
Expand Down Expand Up @@ -753,6 +762,7 @@ impl ProjectWorkspace {
// There may be package sources, but we don't need to duplicate them into the
// workspace sources.
sources: BTreeMap::default(),
pyproject_toml: project_pyproject_toml.clone(),
},
});
};
Expand Down Expand Up @@ -1150,7 +1160,15 @@ mod tests {
"pyproject_toml": "[PYPROJECT_TOML]"
}
},
"sources": {}
"sources": {},
"pyproject_toml": {
"project": {
"name": "bird-feeder",
"requires-python": ">=3.12",
"optional-dependencies": null
},
"tool": null
}
}
}
"###);
Expand Down Expand Up @@ -1186,7 +1204,15 @@ mod tests {
"pyproject_toml": "[PYPROJECT_TOML]"
}
},
"sources": {}
"sources": {},
"pyproject_toml": {
"project": {
"name": "bird-feeder",
"requires-python": ">=3.12",
"optional-dependencies": null
},
"tool": null
}
}
}
"###);
Expand Down Expand Up @@ -1244,6 +1270,33 @@ mod tests {
"workspace": true,
"editable": null
}
},
"pyproject_toml": {
"project": {
"name": "albatross",
"requires-python": ">=3.12",
"optional-dependencies": null
},
"tool": {
"uv": {
"sources": {
"bird-feeder": {
"workspace": true,
"editable": null
}
},
"workspace": {
"members": [
"packages/*"
],
"exclude": null
},
"managed": null,
"dev-dependencies": null,
"override-dependencies": null,
"constraint-dependencies": null
}
}
}
}
}
Expand Down Expand Up @@ -1298,7 +1351,25 @@ mod tests {
"pyproject_toml": "[PYPROJECT_TOML]"
}
},
"sources": {}
"sources": {},
"pyproject_toml": {
"project": null,
"tool": {
"uv": {
"sources": null,
"workspace": {
"members": [
"packages/*"
],
"exclude": null
},
"managed": null,
"dev-dependencies": null,
"override-dependencies": null,
"constraint-dependencies": null
}
}
}
}
}
"###);
Expand Down Expand Up @@ -1333,7 +1404,15 @@ mod tests {
"pyproject_toml": "[PYPROJECT_TOML]"
}
},
"sources": {}
"sources": {},
"pyproject_toml": {
"project": {
"name": "albatross",
"requires-python": ">=3.12",
"optional-dependencies": null
},
"tool": null
}
}
}
"###);
Expand Down
25 changes: 11 additions & 14 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::path::PathBuf;

use anyhow::Result;
use owo_colors::OwoColorize;

use pep508_rs::PackageName;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_warnings::warn_user_once;
use uv_workspace::pyproject_mut::PyProjectTomlMut;
use uv_workspace::{ProjectWorkspace, WorkspaceError};
use uv_workspace::{Workspace, WorkspaceError};

use crate::commands::ExitStatus;
use crate::printer::Printer;
Expand Down Expand Up @@ -53,7 +54,7 @@ pub(crate) async fn init(
.unwrap_or_else(|_| path.simplified().to_path_buf());

anyhow::bail!(
"Project is already initialized in {}",
"Project is already initialized in `{}`",
path.display().cyan()
);
}
Expand All @@ -69,8 +70,9 @@ pub(crate) async fn init(
let workspace = if isolated {
None
} else {
match ProjectWorkspace::discover(&path, None).await {
Ok(project) => Some(project),
// Attempt to find a workspace root.
match Workspace::discover(&path, None).await {
Ok(workspace) => Some(workspace),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(err) => return Err(err.into()),
}
Expand Down Expand Up @@ -114,25 +116,20 @@ pub(crate) async fn init(

if let Some(workspace) = workspace {
// Add the package to the workspace.
let mut pyproject =
PyProjectTomlMut::from_toml(workspace.current_project().pyproject_toml())?;
pyproject.add_workspace(path.strip_prefix(workspace.project_root())?)?;
let mut pyproject = PyProjectTomlMut::from_toml(workspace.pyproject_toml())?;
pyproject.add_workspace(path.strip_prefix(workspace.install_path())?)?;

// Save the modified `pyproject.toml`.
fs_err::write(
workspace.current_project().root().join("pyproject.toml"),
workspace.install_path().join("pyproject.toml"),
pyproject.to_string(),
)?;

writeln!(
printer.stderr(),
"Adding {} as member of workspace {}",
"Adding `{}` as member of workspace `{}`",
name.cyan(),
workspace
.workspace()
.install_path()
.simplified_display()
.cyan()
workspace.install_path().simplified_display().cyan()
)?;
}

Expand Down
Loading

0 comments on commit d232bfe

Please sign in to comment.