From 81e941bc92aac37bbf2f385eeceec9e4c8cfb13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 3 Sep 2024 16:55:29 +0100 Subject: [PATCH] fix(install): recommend using `deno install -g` when using a single http url (#25388) Closes https://github.com/denoland/deno/issues/25361 --- cli/args/flags.rs | 1 - cli/tools/installer.rs | 20 +++++++++++++++++++ .../__test__.jsonc | 14 +++++++++++++ .../install_http.out | 2 ++ .../install_https.out | 2 ++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/specs/install/install_single_http_url_without_global_flag/__test__.jsonc create mode 100644 tests/specs/install/install_single_http_url_without_global_flag/install_http.out create mode 100644 tests/specs/install/install_single_http_url_without_global_flag/install_https.out diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 44f97010f0..2467de88f3 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -237,7 +237,6 @@ pub struct InstallFlagsGlobal { #[derive(Clone, Debug, Eq, PartialEq)] pub enum InstallKind { - #[allow(unused)] Local(Option), Global(InstallFlagsGlobal), } diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index c3f415dc7f..6965d5c6d3 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -15,6 +15,7 @@ use crate::factory::CliFactory; use crate::http_util::HttpClientProvider; use crate::util::fs::canonicalize_path_maybe_not_exists; +use deno_core::anyhow::bail; use deno_core::anyhow::Context; use deno_core::error::generic_error; use deno_core::error::AnyError; @@ -284,6 +285,24 @@ async fn install_local( Ok(()) } +fn check_if_installs_a_single_package_globally( + maybe_add_flags: Option<&AddFlags>, +) -> Result<(), AnyError> { + let Some(add_flags) = maybe_add_flags else { + return Ok(()); + }; + if add_flags.packages.len() != 1 { + return Ok(()); + } + let Ok(url) = Url::parse(&add_flags.packages[0]) else { + return Ok(()); + }; + if matches!(url.scheme(), "http" | "https") { + bail!("Failed to install \"{}\" specifier. If you are trying to install {} globally, run again with `-g` flag:\n deno install -g {}", url.scheme(), url.as_str(), url.as_str()); + } + Ok(()) +} + pub async fn install_command( flags: Arc, install_flags: InstallFlags, @@ -297,6 +316,7 @@ pub async fn install_command( install_global(flags, global_flags).await } InstallKind::Local(maybe_add_flags) => { + check_if_installs_a_single_package_globally(maybe_add_flags.as_ref())?; install_local(flags, maybe_add_flags).await } } diff --git a/tests/specs/install/install_single_http_url_without_global_flag/__test__.jsonc b/tests/specs/install/install_single_http_url_without_global_flag/__test__.jsonc new file mode 100644 index 0000000000..b906a846d0 --- /dev/null +++ b/tests/specs/install/install_single_http_url_without_global_flag/__test__.jsonc @@ -0,0 +1,14 @@ +{ + "steps": [ + { + "args": "install http://example.com", + "output": "install_http.out", + "exitCode": 1 + }, + { + "args": "install https://example.com", + "output": "install_https.out", + "exitCode": 1 + } + ] +} diff --git a/tests/specs/install/install_single_http_url_without_global_flag/install_http.out b/tests/specs/install/install_single_http_url_without_global_flag/install_http.out new file mode 100644 index 0000000000..17f4b286d0 --- /dev/null +++ b/tests/specs/install/install_single_http_url_without_global_flag/install_http.out @@ -0,0 +1,2 @@ +error: Failed to install "http" specifier. If you are trying to install http://example.com/ globally, run again with `-g` flag: + deno install -g http://example.com/ diff --git a/tests/specs/install/install_single_http_url_without_global_flag/install_https.out b/tests/specs/install/install_single_http_url_without_global_flag/install_https.out new file mode 100644 index 0000000000..87597de128 --- /dev/null +++ b/tests/specs/install/install_single_http_url_without_global_flag/install_https.out @@ -0,0 +1,2 @@ +error: Failed to install "https" specifier. If you are trying to install https://example.com/ globally, run again with `-g` flag: + deno install -g https://example.com/