Skip to content

Commit

Permalink
Warn if tool binary directory is not on path
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 10, 2024
1 parent aff9c9b commit 15744ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/uv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ clap = { workspace = true, features = ["derive", "string", "wrap_help"] }
flate2 = { workspace = true, default-features = false }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
indicatif = { workspace = true }
indexmap = { workspace = true }
indicatif = { workspace = true }
itertools = { workspace = true }
miette = { workspace = true, features = ["fancy"] }
owo-colors = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
same-file = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
textwrap = { workspace = true }
Expand Down
48 changes: 47 additions & 1 deletion crates/uv/src/commands/tool/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ use uv_python::{
};
use uv_requirements::RequirementsSpecification;
use uv_tool::{entrypoint_paths, find_executable_directory, InstalledTools, Tool, ToolEntrypoint};
use uv_warnings::warn_user_once;
use uv_warnings::{warn_user, warn_user_once};

use crate::commands::project::{resolve_environment, sync_environment, update_environment};
use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::tool::common::resolve_requirements;
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;
use crate::shell::Shell;

/// Install a tool.
pub(crate) async fn install(
Expand Down Expand Up @@ -373,5 +374,50 @@ pub(crate) async fn install(
);
installed_tools.add_tool_receipt(&from.name, tool)?;

// If the executable directory isn't on the user's PATH, warn.
if !std::env::var_os("X")
.as_ref()
.iter()
.flat_map(std::env::split_paths)
.any(|path| same_file::is_same_file(&executable_directory, path).unwrap_or(false))
{
let export = match Shell::from_env() {
None => None,
Some(Shell::Nushell) => None,
Some(Shell::Bash | Shell::Zsh) => Some(format!(
"export PATH=\"{}:$PATH\"",
executable_directory.simplified_display(),
)),
Some(Shell::Fish) => Some(format!(
"fish_add_path {}",
serde_json::to_string(&executable_directory.simplified_display().to_string())
.unwrap()
)),
Some(Shell::Csh) => Some(format!(
"setenv PATH \"{}:$PATH\"",
executable_directory.simplified_display(),
)),
Some(Shell::Powershell) => Some(format!(
"$env:PATH = \"{};$env:PATH\"",
executable_directory.simplified_display(),
)),
Some(Shell::Cmd) => Some(format!(
"set PATH=\"{};%PATH%\"",
executable_directory.simplified_display(),
)),
};
if let Some(export) = export {
warn_user!(
"`{}` is not on your PATH. To use installed tools, run:\n {export}",
executable_directory.simplified_display()
);
} else {
warn_user!(
"`{}` is not on your PATH. To use installed tools, add the directory to your PATH",
executable_directory.simplified_display()
);
}
}

Ok(ExitStatus::Success)
}

0 comments on commit 15744ae

Please sign in to comment.