diff --git a/src/cargo/sources/git/source.rs b/src/cargo/sources/git/source.rs index 4644a3ac710..5daad1004b8 100644 --- a/src/cargo/sources/git/source.rs +++ b/src/cargo/sources/git/source.rs @@ -191,7 +191,7 @@ impl<'cfg> Source for GitSource<'cfg> { .join("checkouts") .join(&self.ident) .join(short_id.as_str()); - db.copy_to(actual_rev, &checkout_path, self.config)?; + db.copy_to(actual_rev, &checkout_path, self.config, self.url())?; let source_id = self.source_id.with_precise(Some(actual_rev.to_string())); let path_source = PathSource::new_recursive(&checkout_path, source_id, self.config); diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 17fef7ca0f9..d8d031203ee 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -5,6 +5,7 @@ use crate::core::GitReference; use crate::util::errors::CargoResult; use crate::util::{network, Config, IntoUrl, MetricsCounter, Progress}; use anyhow::{anyhow, Context as _}; +use cargo_util::paths::normalize_path; use cargo_util::{paths, ProcessBuilder}; use curl::easy::List; use git2::{self, ErrorClass, ObjectType, Oid}; @@ -13,11 +14,12 @@ use serde::ser; use serde::Serialize; use std::env; use std::fmt; +use std::fs::canonicalize; use std::path::{Path, PathBuf}; use std::process::Command; use std::str; use std::time::{Duration, Instant}; -use url::Url; +use url::{ParseError, Url}; fn serialize_str(t: &T, s: S) -> Result where @@ -151,6 +153,7 @@ impl GitDatabase { rev: git2::Oid, dest: &Path, cargo_config: &Config, + parent_remote_url: &Url, ) -> CargoResult> { // If the existing checkout exists, and it is fresh, use it. // A non-fresh checkout can happen if the checkout operation was @@ -164,7 +167,7 @@ impl GitDatabase { Some(co) => co, None => GitCheckout::clone_into(dest, self, rev, cargo_config)?, }; - checkout.update_submodules(cargo_config)?; + checkout.update_submodules(cargo_config, parent_remote_url)?; Ok(checkout) } @@ -322,19 +325,25 @@ impl<'a> GitCheckout<'a> { Ok(()) } - fn update_submodules(&self, cargo_config: &Config) -> CargoResult<()> { - return update_submodules(&self.repo, cargo_config); + fn update_submodules(&self, cargo_config: &Config, parent_remote_url: &Url) -> CargoResult<()> { + return update_submodules(&self.repo, cargo_config, parent_remote_url); - fn update_submodules(repo: &git2::Repository, cargo_config: &Config) -> CargoResult<()> { + fn update_submodules( + repo: &git2::Repository, + cargo_config: &Config, + parent_remote_url: &Url, + ) -> CargoResult<()> { debug!("update submodules for: {:?}", repo.workdir().unwrap()); for mut child in repo.submodules()? { - update_submodule(repo, &mut child, cargo_config).with_context(|| { - format!( - "failed to update submodule `{}`", - child.name().unwrap_or("") - ) - })?; + update_submodule(repo, &mut child, cargo_config, parent_remote_url).with_context( + || { + format!( + "failed to update submodule `{}`", + child.name().unwrap_or("") + ) + }, + )?; } Ok(()) } @@ -343,12 +352,59 @@ impl<'a> GitCheckout<'a> { parent: &git2::Repository, child: &mut git2::Submodule<'_>, cargo_config: &Config, + parent_remote_url: &Url, ) -> CargoResult<()> { child.init(false)?; - let url = child.url().ok_or_else(|| { + let child_url_str = child.url().ok_or_else(|| { anyhow::format_err!("non-utf8 url for submodule {:?}?", child.path()) })?; + //There are a few possible cases here: + // 1. Submodule URL is not relative. + // Happy path, Url is just the submodule url. + // 2. Submodule URL is relative and does specify ssh/https/file/etc. + // We combine the parent path and relative path. + // 3. Submodule URL is relative and does not specify ssh/https/file/etc. + // In which case we fail to parse with the error ParseError::RelativeUrlWithoutBase. + // We then combine the relative url with the host/protocol from the parent url. + // 4. We fail to parse submodule url for some reason. + // Error. Gets passed up. + let url = match Url::parse(child_url_str) { + Ok(child_url) => { + if Path::new(child_url.path()).is_relative() { + let parent_remote_path = Path::new(parent_remote_url.path()); + let child_remote_path = Path::new(child_url.path()); + let canonical = canonicalize(normalize_path( + &parent_remote_path.join(child_remote_path), + ))?; + let mut final_path = parent_remote_url.clone(); + final_path.set_path(canonical.to_string_lossy().as_ref()); + final_path.to_string() + } else { + child_url.to_string() + } + } + Err(ParseError::RelativeUrlWithoutBase) => { + let path = parent_remote_url.path(); + let mut new_parent_remote_url = parent_remote_url.clone(); + new_parent_remote_url.set_path(format!("{}/", path).as_str()); + new_parent_remote_url = match new_parent_remote_url.join(child_url_str) { + Ok(x) => x, + Err(err) => Err(err) + .with_context(|| format!("Failed to parse child submodule url"))?, + }; + let final_path = canonicalize(Path::new(new_parent_remote_url.path()))?; + new_parent_remote_url.set_path(final_path.to_string_lossy().as_ref()); + new_parent_remote_url.to_string() + } + Err(err) => { + return Err(anyhow::format_err!( + "Error parsing submodule url: {:?}?", + err + )); + } + }; + // Skip the submodule if the config says not to update it. if child.update_strategy() == git2::SubmoduleUpdate::None { cargo_config.shell().status( @@ -379,7 +435,7 @@ impl<'a> GitCheckout<'a> { let mut repo = match head_and_repo { Ok((head, repo)) => { if child.head_id() == head { - return update_submodules(&repo, cargo_config); + return update_submodules(&repo, cargo_config, parent_remote_url); } repo } @@ -394,7 +450,7 @@ impl<'a> GitCheckout<'a> { cargo_config .shell() .status("Updating", format!("git submodule `{}`", url))?; - fetch(&mut repo, url, &reference, cargo_config).with_context(|| { + fetch(&mut repo, &url, &reference, cargo_config).with_context(|| { format!( "failed to fetch submodule `{}` from {}", child.name().unwrap_or(""), @@ -404,7 +460,7 @@ impl<'a> GitCheckout<'a> { let obj = repo.find_object(head, None)?; reset(&repo, &obj, cargo_config)?; - update_submodules(&repo, cargo_config) + update_submodules(&repo, cargo_config, parent_remote_url) } } } diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index c7d72b04e1d..58d9fa8bbef 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -935,6 +935,71 @@ fn dep_with_submodule() { .run(); } +#[cargo_test] +fn dep_with_relative_submodule() { + let foo = project(); + let base = git::new("base", |project| { + project + .file( + "Cargo.toml", + r#" + [package] + name = "base" + version = "0.5.0" + [dependencies] + deployment.path = "deployment" + "#, + ) + .file( + "src/lib.rs", + r#" + pub fn dep() { + deployment::deployment_func(); + } + "#, + ) + }); + let _deployment = git::new("deployment", |project| { + project + .file("src/lib.rs", "pub fn deployment_func() {}") + .file("Cargo.toml", &basic_lib_manifest("deployment")) + }); + + let base_repo = git2::Repository::open(&base.root()).unwrap(); + git::add_submodule(&base_repo, "../deployment", Path::new("deployment")); + git::commit(&base_repo); + + let project = foo + .file( + "Cargo.toml", + &format!( + r#" + [project] + name = "foo" + version = "0.5.0" + [dependencies.base] + git = '{}' + "#, + base.url() + ), + ) + .file("src/lib.rs", "pub fn foo() { }") + .build(); + + project + .cargo("build") + .with_stderr( + "\ +[UPDATING] git repository [..] +[UPDATING] git submodule `file://[..]/deployment` +[COMPILING] deployment [..] +[COMPILING] base [..] +[COMPILING] foo [..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]\n", + ) + .run(); +} + #[cargo_test] fn dep_with_bad_submodule() { let project = project();