Skip to content
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

uv init should not create nested workspace #5293

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konstin - Any reason not to include this? It could be the same as the root member, but for virtual workspaces I can't see another way to get it.

}

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 `{}`",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure about backticks around the inner colored text. I don't think we have a consistent style-guide for this, but is the color not sufficient?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do, actually #5209 covers use of backticks in these cases. We want the output to be readable in colorless contexts too.

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
Loading