Don't panic for uv init / --name foo (#17983)

No test since we don't know where the test runner will have access to
`/` or not. I've manually confirmed that `uv init / --name foo` doesn't
panic anymore.

The error message for it is mediocre but passable (same as e.g. `uv init
/usr` on stable, no change just documenting it):

```
error: Failed to initialize Git repository at `/`
stdout:
stderr: /.git: Permission denied
```
This commit is contained in:
konsti
2026-02-16 17:40:46 +01:00
committed by GitHub
parent 4aa60c79c1
commit ac97bcf232
+13 -2
View File
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str::FromStr;
use anyhow::{Context, Result, anyhow};
use anyhow::{Context, Result, anyhow, bail};
use owo_colors::OwoColorize;
use toml_edit::{InlineTable, Value};
use tracing::{debug, trace, warn};
@@ -311,7 +311,18 @@ async fn init_project(
// Discover the current workspace, if it exists.
let workspace_cache = WorkspaceCache::default();
let workspace = {
let parent = path.parent().expect("Project path has no parent");
let parent = match path.parent() {
Some(parent) => parent,
None => {
if path.is_dir() {
// Support creating a project in the filesystem root (`/` on Unix).
path
} else {
// Not sure how we'd end up here, but we need to handle the case.
bail!("Project directory has no parent directory");
}
}
};
match Workspace::discover(
parent,
&DiscoveryOptions {