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
5 changes: 5 additions & 0 deletions crates/uv-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ pub enum Error {
/// The non-virtual environment directory that would have been cleared.
path: PathBuf,
},
#[error("Virtual environment path is not valid UTF-8: {}", path.user_display())]
NonUtf8Path {
/// The non-UTF-8 virtual environment path.
path: PathBuf,
},
}

impl uv_errors::Hint for Error {
Expand Down
10 changes: 8 additions & 2 deletions crates/uv-virtualenv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ pub(crate) fn create(
.map(|path| path.simplified().to_str().unwrap().replace('\\', "\\\\"))
.join(path_sep);

let location_string = location
.simplified()
.to_str()
.ok_or_else(|| Error::NonUtf8Path {
path: location.clone(),
})?;
let virtual_env_dir = match (relocatable, name.to_owned()) {
(true, "activate") => Cow::Borrowed(
r#"'"$(dirname -- "$(dirname -- "$(realpath -- "$SCRIPT_PATH")")")"'"#,
Expand All @@ -491,10 +497,10 @@ pub(crate) fn create(
(true, "activate.nu") => Cow::Borrowed(r"(path self | path dirname | path dirname)"),
(false, "activate.nu") => Cow::Owned(format!(
"'{}'",
escape_posix_for_single_quotes(location.simplified().to_str().unwrap())
escape_posix_for_single_quotes(location_string)
)),
// Note: `activate.ps1` is already relocatable by default.
_ => escape_posix_for_single_quotes(location.simplified().to_str().unwrap()),
_ => escape_posix_for_single_quotes(location_string),
};

let activator = template
Expand Down
29 changes: 29 additions & 0 deletions crates/uv/tests/python/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use uv_static::EnvVars;

#[cfg(unix)]
use fs_err::os::unix::fs::symlink;
#[cfg(unix)]
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
#[cfg(windows)]
use std::{ffi::OsString, os::windows::ffi::OsStringExt};

use uv_test::{site_packages_path, uv_snapshot};

Expand Down Expand Up @@ -1489,6 +1493,31 @@ fn file_exists() -> Result<()> {
Ok(())
}

#[test]
fn non_utf8_path() {
let context = uv_test::test_context_with_versions!(&["3.12"]);
let path = cfg_select! {
unix => OsStr::from_bytes(b".venv-\xff"),
windows => OsString::from_wide(&[0x002e, 0x0076, 0x0065, 0x006e, 0x0076, 0x002d, 0xd800]),
};

uv_snapshot!(context.filters(), context.venv()
.arg(path)
.arg("--python")
.arg("3.12"), @"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Creating virtual environment at: .venv-�
error: Failed to create virtual environment
Caused by: Virtual environment path is not valid UTF-8: .venv-�
"
);
}

#[test]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

fn empty_dir_exists() -> Result<()> {
let context = uv_test::test_context_with_versions!(&["3.12"]);
Expand Down