diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index c301c3015..387e16bc6 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -29,10 +29,7 @@ use contract_metadata::{ SourceLanguage, SourceWasm, User, }; use semver::Version; -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::{fs, path::PathBuf}; use url::Url; const METADATA_FILE: &str = "metadata.json"; @@ -123,12 +120,6 @@ impl GenerateMetadataCommand { if self.unstable_options.original_manifest { generate_metadata(&self.crate_metadata.manifest_path)?; } else { - let manifest_dir = match self.crate_metadata.manifest_path.directory() { - Some(dir) => dir, - None => Path::new("./"), - }; - let absolute_package_path = manifest_dir.canonicalize()?; - Workspace::new( &self.crate_metadata.cargo_meta, &self.crate_metadata.root_package.id, @@ -139,7 +130,7 @@ impl GenerateMetadataCommand { .with_profile_release_lto(false)?; Ok(()) })? - .with_metadata_gen_package(absolute_package_path)? + .with_metadata_gen_package(self.crate_metadata.manifest_path.absolute_directory()?)? .using_temp(generate_metadata)?; } diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 5633f0be2..95e11d131 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -43,11 +43,17 @@ impl CrateMetadata { /// Parses the contract manifest and returns relevant metadata. pub fn collect(manifest_path: &ManifestPath) -> Result { let (metadata, root_package) = get_cargo_metadata(manifest_path)?; - let target_directory = metadata.target_directory.as_path().join("ink"); + let mut target_directory = metadata.target_directory.as_path().join("ink"); // Normalize the package name. let package_name = root_package.name.replace("-", "_"); + let absolute_manifest_path = manifest_path.absolute_directory()?; + let absolute_workspace_root = metadata.workspace_root.canonicalize()?; + if absolute_manifest_path != absolute_workspace_root { + target_directory = target_directory.join(package_name.clone()); + } + // {target_dir}/wasm32-unknown-unknown/release/{package_name}.wasm let mut original_wasm = target_directory.clone(); original_wasm.push("wasm32-unknown-unknown"); diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index 40ef117c5..69d9a2723 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -66,6 +66,15 @@ impl ManifestPath { None } } + + /// Returns the absolute directory path of the manifest. + pub fn absolute_directory(&self) -> Result { + let directory = match self.directory() { + Some(dir) => dir, + None => Path::new("./"), + }; + directory.canonicalize() + } } impl TryFrom<&PathBuf> for ManifestPath { @@ -404,3 +413,30 @@ fn crate_type_exists(crate_type: &str, crate_types: &[value::Value]) -> bool { .iter() .any(|v| v.as_str().map_or(false, |s| s == crate_type)) } + +#[cfg(test)] +mod test { + use super::ManifestPath; + use crate::util::tests::with_tmp_dir; + use std::fs; + + #[test] + fn must_return_absolute_path_from_absolute_path() { + with_tmp_dir(|path| { + // given + let cargo_toml_path = path.join("Cargo.toml"); + let _ = fs::File::create(&cargo_toml_path).expect("file creation failed"); + let manifest_path = + ManifestPath::new(cargo_toml_path).expect("manifest path creation failed"); + + // when + let absolute_path = manifest_path + .absolute_directory() + .expect("absolute path extraction failed"); + + // then + assert_eq!(absolute_path.as_path(), path); + Ok(()) + }) + } +}