Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9a85104

Browse files
committed
Make virtualenv creation in tidy more robust
1 parent cd7be92 commit 9a85104

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/tools/tidy/src/ext_tool_checks.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,22 +360,40 @@ fn create_venv_at_path(path: &Path) -> Result<(), Error> {
360360
return Err(ret);
361361
};
362362

363-
eprintln!("creating virtual environment at '{}' using '{sys_py}'", path.display());
364-
let out = Command::new(sys_py).args(["-m", "virtualenv"]).arg(path).output().unwrap();
363+
// First try venv, which should be packaged in the Python3 standard library.
364+
// If it is not available, try to create the virtual environment using the
365+
// virtualenv package.
366+
if try_create_venv(sys_py, path, "venv").is_ok() {
367+
return Ok(());
368+
}
369+
try_create_venv(sys_py, path, "virtualenv")
370+
}
371+
372+
fn try_create_venv(python: &str, path: &Path, module: &str) -> Result<(), Error> {
373+
eprintln!(
374+
"creating virtual environment at '{}' using '{python}' and '{module}'",
375+
path.display()
376+
);
377+
let out = Command::new(python).args(["-m", module]).arg(path).output().unwrap();
365378

366379
if out.status.success() {
367380
return Ok(());
368381
}
369382

370383
let stderr = String::from_utf8_lossy(&out.stderr);
371-
let err = if stderr.contains("No module named virtualenv") {
384+
let err = if stderr.contains(&format!("No module named {module}")) {
372385
Error::Generic(format!(
373-
"virtualenv not found: you may need to install it \
374-
(`{sys_py} -m pip install virtualenv`)"
386+
r#"{module} not found: you may need to install it:
387+
`{python} -m pip install {module}`
388+
If you see an error about "externally managed environment" when running the above command,
389+
either install `{module}` using your system package manager
390+
(e.g. `sudo apt-get install {python}-{module}`) or create a virtual environment manually, install
391+
`{module}` in it and then activate it before running tidy.
392+
"#
375393
))
376394
} else {
377395
Error::Generic(format!(
378-
"failed to create venv at '{}' using {sys_py}: {stderr}",
396+
"failed to create venv at '{}' using {python} -m {module}: {stderr}",
379397
path.display()
380398
))
381399
};

0 commit comments

Comments
 (0)