Skip to content

Commit b87ae28

Browse files
committed
Heuristically identify git URL by matching the pattern "git" in the domain. Add a special case error with a fixit hint to help users rerun the correct cargo install command
1 parent 1564bc0 commit b87ae28

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use std::task::Poll;
99
use anyhow::{bail, format_err, Context as _};
1010
use serde::{Deserialize, Serialize};
1111
use toml_edit::easy as toml;
12+
use url::Url;
1213

1314
use crate::core::compiler::Freshness;
1415
use crate::core::{Dependency, FeatureValue, Package, PackageId, Source, SourceId};
1516
use crate::ops::{self, CompileFilter, CompileOptions};
1617
use crate::sources::PathSource;
1718
use crate::util::errors::CargoResult;
19+
use crate::util::interning::InternedString;
1820
use crate::util::Config;
1921
use crate::util::{FileLock, Filesystem};
2022

@@ -520,6 +522,25 @@ pub fn path_source(source_id: SourceId, config: &Config) -> CargoResult<PathSour
520522
Ok(PathSource::new(&path, source_id, config))
521523
}
522524

525+
fn is_package_name_a_git_url(package_name: &InternedString) -> bool {
526+
if let Ok(url) = Url::parse(package_name) {
527+
if let Some(domain) = url.domain() {
528+
// REVIEW
529+
// Are there any other git services without "git"
530+
// in the domain?
531+
// bitbucket?
532+
// Is it possible to ask the cargo/crates team for
533+
// some stats where crates projects are currently hosted on
534+
return domain.contains("git");
535+
}
536+
}
537+
false
538+
}
539+
540+
fn was_git_url_miscategorised_as_a_registry_dep(dep: &Dependency) -> bool {
541+
return dep.source_id().is_registry() && is_package_name_a_git_url(&dep.package_name());
542+
}
543+
523544
/// Gets a Package based on command-line requirements.
524545
pub fn select_dep_pkg<T>(
525546
source: &mut T,
@@ -565,12 +586,22 @@ where
565586
source.source_id()
566587
)
567588
} else {
568-
bail!(
569-
"could not find `{}` in {} with version `{}`",
570-
dep.package_name(),
571-
source.source_id(),
572-
dep.version_req(),
573-
)
589+
if was_git_url_miscategorised_as_a_registry_dep(&dep) {
590+
bail!(
591+
"could not find `{}` in {} with version `{}`. Try adding `--git {}`",
592+
dep.package_name(),
593+
source.source_id(),
594+
dep.version_req(),
595+
dep.package_name(),
596+
)
597+
} else {
598+
bail!(
599+
"could not find `{}` in {} with version `{}`",
600+
dep.package_name(),
601+
source.source_id(),
602+
dep.version_req(),
603+
)
604+
}
574605
}
575606
}
576607
}

tests/testsuite/install.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,19 @@ fn test_install_git_cannot_be_a_base_url() {
14181418
.run();
14191419
}
14201420

1421+
#[cargo_test]
1422+
fn test_install_from_git_generate_suggestion() {
1423+
registry::init();
1424+
cargo_process("install https://github.com/rust-lang/cargo")
1425+
.with_status(101)
1426+
.with_stderr(
1427+
"\
1428+
[UPDATING] `[..]` index
1429+
error: could not find `https://github.com/rust-lang/cargo` in registry `crates-io` with version `*`. Try adding `--git https://github.com/rust-lang/cargo`",
1430+
)
1431+
.run();
1432+
}
1433+
14211434
#[cargo_test]
14221435
fn uninstall_multiple_and_specifying_bin() {
14231436
cargo_process("uninstall foo bar --bin baz")

0 commit comments

Comments
 (0)