From f7b0b9f89e43e4e3d188ec7a12f0e36bb1e727ba Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 25 Aug 2023 11:46:50 +0000 Subject: [PATCH 1/3] feat(nargo): add support for optional directory --- crates/nargo_toml/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/nargo_toml/src/lib.rs b/crates/nargo_toml/src/lib.rs index 8372942931b..719e8827049 100644 --- a/crates/nargo_toml/src/lib.rs +++ b/crates/nargo_toml/src/lib.rs @@ -238,15 +238,18 @@ struct PackageMetadata { /// Enum representing the different types of ways to /// supply a source for the dependency enum DependencyConfig { - Github { git: String, tag: String }, + Github { git: String, tag: String, directory: Option }, Path { path: String }, } impl DependencyConfig { fn resolve_to_dependency(&self, pkg_root: &Path) -> Result { let dep = match self { - Self::Github { git, tag } => { - let dir_path = clone_git_repo(git, tag).map_err(ManifestError::GitError)?; + Self::Github { git, tag, directory } => { + let mut dir_path = clone_git_repo(git, tag).map_err(ManifestError::GitError)?; + if let Some(directory) = directory { + dir_path = dir_path.join(directory); + } let toml_path = dir_path.join("Nargo.toml"); let package = resolve_package_from_toml(&toml_path)?; Dependency::Remote { package } From 3e521fdf5995038e2e1ac09023c0f3b6e493575a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 25 Aug 2023 12:08:43 +0000 Subject: [PATCH 2/3] fix: validate directory in dependencyconfig --- crates/nargo_toml/src/lib.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/nargo_toml/src/lib.rs b/crates/nargo_toml/src/lib.rs index 719e8827049..7e0b7c3c3ac 100644 --- a/crates/nargo_toml/src/lib.rs +++ b/crates/nargo_toml/src/lib.rs @@ -246,11 +246,18 @@ impl DependencyConfig { fn resolve_to_dependency(&self, pkg_root: &Path) -> Result { let dep = match self { Self::Github { git, tag, directory } => { - let mut dir_path = clone_git_repo(git, tag).map_err(ManifestError::GitError)?; - if let Some(directory) = directory { - dir_path = dir_path.join(directory); - } - let toml_path = dir_path.join("Nargo.toml"); + let dir_path = clone_git_repo(git, tag).map_err(ManifestError::GitError)?; + let project_path = if let Some(directory) = directory { + let internal_path = dir_path.join(directory).normalize(); + assert!( + internal_path.starts_with(&dir_path), + "Directory must point to a subdirectory of the git dependency" + ); + internal_path + } else { + dir_path + }; + let toml_path = project_path.join("Nargo.toml"); let package = resolve_package_from_toml(&toml_path)?; Dependency::Remote { package } } From c88af449d72ed732b10071e980fa4b1212057a48 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 25 Aug 2023 16:02:18 +0000 Subject: [PATCH 3/3] fix: use a manifest error --- crates/nargo_toml/src/errors.rs | 3 +++ crates/nargo_toml/src/lib.rs | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/nargo_toml/src/errors.rs b/crates/nargo_toml/src/errors.rs index 2b68f681f92..9abeab97b61 100644 --- a/crates/nargo_toml/src/errors.rs +++ b/crates/nargo_toml/src/errors.rs @@ -44,6 +44,9 @@ pub enum ManifestError { #[error("{} found in {toml}", if name.is_empty() { "Empty dependency name".into() } else { format!("Invalid dependency name `{name}`") })] InvalidDependencyName { toml: PathBuf, name: String }, + #[error("Invalid directory path {directory} in {toml}: It must point to a subdirectory")] + InvalidDirectory { toml: PathBuf, directory: PathBuf }, + /// Encountered error while downloading git repository. #[error("{0}")] GitError(String), diff --git a/crates/nargo_toml/src/lib.rs b/crates/nargo_toml/src/lib.rs index 7e0b7c3c3ac..1dd6ac0e695 100644 --- a/crates/nargo_toml/src/lib.rs +++ b/crates/nargo_toml/src/lib.rs @@ -249,10 +249,12 @@ impl DependencyConfig { let dir_path = clone_git_repo(git, tag).map_err(ManifestError::GitError)?; let project_path = if let Some(directory) = directory { let internal_path = dir_path.join(directory).normalize(); - assert!( - internal_path.starts_with(&dir_path), - "Directory must point to a subdirectory of the git dependency" - ); + if !internal_path.starts_with(&dir_path) { + return Err(ManifestError::InvalidDirectory { + toml: pkg_root.join("Nargo.toml"), + directory: directory.into(), + }); + } internal_path } else { dir_path