Skip to content
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
18 changes: 15 additions & 3 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,27 @@ pub(crate) async fn init(
let name = match name {
Some(name) => name,
None => {
let name = path
let directory_name = path
.file_name()
.and_then(|path| path.to_str())
.context("Missing directory name")?;

// Pre-normalize the package name by removing any leading or trailing
// whitespace, and replacing any internal whitespace with hyphens.
let name = name.trim().replace(' ', "-");
PackageName::from_owned(name)?
let candidate = directory_name.trim().replace(' ', "-");
match PackageName::from_owned(candidate) {
Ok(name) => name,
Err(_) => {
let directory_description = if explicit_path.is_some() {
"target directory"
} else {
"current directory"
};
anyhow::bail!(
"The {directory_description} (`{directory_name}`) is not a valid package name. Please provide a package name with `--name`."
);
}
}
}
};

Expand Down
24 changes: 23 additions & 1 deletion crates/uv/tests/it/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2659,10 +2659,32 @@ fn init_hidden() {
----- stdout -----

----- stderr -----
error: Not a valid package or extra name: ".foo". Names must start and end with a letter or digit and may only contain -, _, ., and alphanumeric characters.
error: The target directory (`.foo`) is not a valid package name. Please provide a package name with `--name`.
"###);
}

#[test]
fn init_non_ascii_directory() -> Result<()> {
let context = TestContext::new("3.12");

let directory = context.temp_dir.child("püthon");
directory.create_dir_all()?;

let mut command = context.init();
command.current_dir(directory.path());

uv_snapshot!(context.filters(), command, @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: The current directory (`püthon`) is not a valid package name. Please provide a package name with `--name`.
"###);

Ok(())
}

/// Run `uv init` with an invalid `pyproject.toml` in a parent directory.
#[test]
fn init_failure() -> Result<()> {
Expand Down
Loading