From a53021a0ef98ac4b99746cdb2b550e0f9866ba49 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 13:31:27 +0100 Subject: [PATCH 1/9] Debug messages for pre and post fingerprint --- crates/build/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 38a7731be..eff035344 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -660,6 +660,12 @@ pub fn execute(args: ExecuteArgs) -> Result { ) })?; + tracing::debug!( + "Fingerprint before build: {:?}, after build: {:?}", + pre_fingerprint, + post_fingerprint + ); + let dest_wasm_path = crate_metadata.dest_wasm.clone(); if pre_fingerprint == Some(post_fingerprint) From 96ca9a9c50f6a47a847de6cac61555b3489da52d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 13:32:47 +0100 Subject: [PATCH 2/9] Copy Cargo.lock to tmp workspace root --- crates/build/src/workspace/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index a51847fd8..a5ab80632 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -213,8 +213,8 @@ impl Workspace { // copy the `Cargo.lock` file let src_lockfile = self.workspace_root.clone().join("Cargo.lock"); - let dest_lockfile = tmp_root_manifest_path - .absolute_directory()? + let dest_lockfile = tmp_dir + .path() .join("Cargo.lock"); if src_lockfile.exists() { tracing::debug!( From 6892492e8742d7cce4809c181f10d3f7b5d25a1b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 13:33:04 +0100 Subject: [PATCH 3/9] Cargo.lock --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 190faab29..310c56368 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,7 +92,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" dependencies = [ - "anstyle 1.0.0", + "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", @@ -131,7 +131,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" dependencies = [ - "anstyle 1.0.0", + "anstyle", "windows-sys 0.48.0", ] @@ -729,7 +729,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" dependencies = [ "anstream", - "anstyle 1.0.0", + "anstyle", "bitflags", "clap_lex", "strsim", From 3808d566ee41f6bb5f58315f4998d9bba71fd188 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 16:28:46 +0100 Subject: [PATCH 4/9] Only copy and rewrite the target contract manifest --- crates/build/src/lib.rs | 4 +- crates/build/src/metadata.rs | 5 +- crates/build/src/workspace/manifest.rs | 33 +++--- crates/build/src/workspace/mod.rs | 135 +++++-------------------- 4 files changed, 47 insertions(+), 130 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index eff035344..d667d88d0 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -328,7 +328,9 @@ fn exec_cargo_dylint(crate_metadata: &CrateMetadata, verbosity: Verbosity) -> Re Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { - manifest.with_dylint()?; + manifest + .delete_workspace_table() + .with_dylint()?; Ok(()) })? .using_temp(|manifest_path| { diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index 28db8ca84..2811f751f 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -186,13 +186,12 @@ pub(crate) fn execute( Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { manifest + .delete_workspace_table() .with_added_crate_type("rlib")? .with_profile_release_lto(false)?; Ok(()) })? - .with_metadata_gen_package( - crate_metadata.manifest_path.absolute_directory()?, - )? + .with_metadata_gen_package()? .using_temp(generate_metadata)?; } diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index d0afd7d97..3f2c24243 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -142,11 +142,6 @@ impl Manifest { }) } - /// Get the path of the manifest file - pub(super) fn path(&self) -> &ManifestPath { - &self.path - } - /// Get mutable reference to `[lib] crate-types = []` section fn get_crate_types_mut(&mut self) -> Result<&mut value::Array> { let lib = self @@ -343,15 +338,20 @@ impl Manifest { /// - `[dependencies]` /// /// Dependencies with package names specified in `exclude_deps` will not be rewritten. - pub fn rewrite_relative_paths(&mut self, exclude_deps: &[String]) -> Result<()> { + pub fn rewrite_relative_paths(&mut self) -> Result<()> { let manifest_dir = self.path.absolute_directory()?; let path_rewrite = PathRewrite { - exclude_deps, manifest_dir, }; path_rewrite.rewrite_relative_paths(&mut self.toml) } + /// Remove the `[workspace]` section from the manifest. + pub fn delete_workspace_table(&mut self) -> &mut Self { + self.toml.remove("workspace"); + self + } + /// Writes the amended manifest to the given path. pub fn write(&self, manifest_path: &ManifestPath) -> Result<()> { if let Some(dir) = manifest_path.directory() { @@ -413,12 +413,11 @@ impl Manifest { } /// Replace relative paths with absolute paths with the working directory. -struct PathRewrite<'a> { - exclude_deps: &'a [String], +struct PathRewrite { manifest_dir: PathBuf, } -impl<'a> PathRewrite<'a> { +impl PathRewrite { /// Replace relative paths with absolute paths with the working directory. fn rewrite_relative_paths(&self, toml: &mut value::Table) -> Result<()> { // Rewrite `[lib] path = /path/to/lib.rs` @@ -516,14 +515,12 @@ impl<'a> PathRewrite<'a> { package_name.to_string() }; - if !self.exclude_deps.contains(&package_name) { - if let Some(dependency) = value.as_table_mut() { - if let Some(dep_path) = dependency.get_mut("path") { - self.to_absolute_path( - format!("dependency {package_name}"), - dep_path, - )?; - } + if let Some(dependency) = value.as_table_mut() { + if let Some(dep_path) = dependency.get_mut("path") { + self.to_absolute_path( + format!("dependency {package_name}"), + dep_path, + )?; } } } diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index a5ab80632..ac1465744 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -35,7 +35,6 @@ use cargo_metadata::{ }; use std::{ - collections::HashMap, path::{ Path, PathBuf, @@ -50,43 +49,26 @@ use std::{ /// directly. pub struct Workspace { workspace_root: PathBuf, - root_package: PackageId, - members: HashMap, + root_package: Package, + root_manifest: Manifest, } impl Workspace { /// Create a new Workspace from the supplied cargo metadata. pub fn new(metadata: &CargoMetadata, root_package: &PackageId) -> Result { - let member_manifest = - |package_id: &PackageId| -> Result<(PackageId, (Package, Manifest))> { - let package = metadata - .packages - .iter() - .find(|p| p.id == *package_id) - .unwrap_or_else(|| { - panic!( - "Package '{package_id}' is a member and should be in the packages list" - ) - }); - let manifest_path = ManifestPath::new(&package.manifest_path)?; - let manifest = Manifest::new(manifest_path)?; - Ok((package_id.clone(), (package.clone(), manifest))) - }; - - let members = metadata - .workspace_members + let root_package = metadata + .packages .iter() - .map(member_manifest) - .collect::>>()?; + .find(|p| p.id == *root_package) + .ok_or_else(|| anyhow::anyhow!("The root package should be a workspace member"))?; - if !members.contains_key(root_package) { - anyhow::bail!("The root package should be a workspace member") - } + let manifest_path = ManifestPath::new(&root_package.manifest_path)?; + let root_manifest = Manifest::new(manifest_path)?; Ok(Workspace { workspace_root: metadata.workspace_root.clone().into(), root_package: root_package.clone(), - members, + root_manifest, }) } @@ -100,62 +82,16 @@ impl Workspace { where F: FnOnce(&mut Manifest) -> Result<()>, { - let root_package_manifest = self - .members - .get_mut(&self.root_package) - .map(|(_, m)| m) - .expect("The root package should be a workspace member"); - f(root_package_manifest)?; - Ok(self) - } - - /// Amend the manifest of the package at `package_path` using the supplied function. - pub fn with_contract_manifest( - &mut self, - package_path: &Path, - f: F, - ) -> Result<&mut Self> - where - F: FnOnce(&mut Manifest) -> Result<()>, - { - let manifest = self - .members - .iter_mut() - .find_map(|(_, (_, manifest))| { - // `package_path` is always absolute and canonicalized. Thus we need to - // canonicalize the manifest's directory path as well in order to compare - // both of them. - let manifest_path = manifest.path().directory()?; - let manifest_path = manifest_path.canonicalize().unwrap_or_else(|_| { - panic!("Cannot canonicalize {}", manifest_path.display()) - }); - if manifest_path == package_path { - Some(manifest) - } else { - None - } - }) - .ok_or_else(|| { - anyhow::anyhow!( - "Cannot find package with package path {} in workspace members", - package_path.display(), - ) - })?; - f(manifest)?; + f(&mut self.root_manifest)?; Ok(self) } /// Generates a package to invoke for generating contract metadata. /// /// The contract metadata will be generated for the package found at `package_path`. - pub(super) fn with_metadata_gen_package( - &mut self, - package_path: PathBuf, - ) -> Result<&mut Self> { - self.with_contract_manifest(&package_path, |manifest| { - manifest.with_metadata_package()?; - Ok(()) - }) + pub(super) fn with_metadata_gen_package(&mut self) -> Result<&mut Self> { + self.root_manifest.with_metadata_package()?; + Ok(self) } /// Writes the amended manifests to the `target` directory, retaining the workspace directory @@ -168,25 +104,18 @@ impl Workspace { pub fn write>( &mut self, target: P, - ) -> Result> { - let exclude_member_package_names = self - .members - .iter() - .map(|(_, (p, _))| p.name.clone()) - .collect::>(); - let mut new_manifest_paths = Vec::new(); - for (package_id, (package, manifest)) in self.members.iter_mut() { - // replace the original workspace root with the temporary directory - let mut new_path: PathBuf = target.as_ref().into(); - new_path.push(package.manifest_path.strip_prefix(&self.workspace_root)?); - let new_manifest = ManifestPath::new(new_path)?; - - manifest.rewrite_relative_paths(&exclude_member_package_names)?; - manifest.write(&new_manifest)?; - - new_manifest_paths.push((package_id.clone(), new_manifest)); - } - Ok(new_manifest_paths) + ) -> Result { + // replace the original workspace root with the temporary directory + let mut new_path: PathBuf = target.as_ref().into(); + new_path.push(self.root_package.manifest_path.strip_prefix(&self.workspace_root)?); + let new_manifest = ManifestPath::new(new_path)?; + + // tracing::info!("Rewriting manifest {} to {}", self.root_manifest.manifest_path, new_manifest.display()); + + self.root_manifest.rewrite_relative_paths()?; + self.root_manifest.write(&new_manifest)?; + + Ok(new_manifest) } /// Copy the workspace with amended manifest files to a temporary directory, executing the @@ -199,17 +128,7 @@ impl Workspace { .prefix("cargo-contract_") .tempdir()?; tracing::debug!("Using temp workspace at '{}'", tmp_dir.path().display()); - let new_paths = self.write(&tmp_dir)?; - let tmp_root_manifest_path = new_paths - .iter() - .find_map(|(pid, path)| { - if *pid == self.root_package { - Some(path) - } else { - None - } - }) - .expect("root package should be a member of the temp workspace"); + let tmp_root_manifest_path = self.write(&tmp_dir)?; // copy the `Cargo.lock` file let src_lockfile = self.workspace_root.clone().join("Cargo.lock"); @@ -225,6 +144,6 @@ impl Workspace { std::fs::copy(src_lockfile, dest_lockfile)?; } - f(tmp_root_manifest_path) + f(&tmp_root_manifest_path) } } From 2f5af8eadca8f7bbcbbd6b1fc5d62814e97c178f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 16:36:31 +0100 Subject: [PATCH 5/9] Delete workspace table after build --- crates/build/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index d667d88d0..f13593442 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -284,9 +284,10 @@ fn exec_cargo_for_wasm_target( Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { manifest + .delete_workspace_table() .with_crate_types(["cdylib"])? .with_profile_release_defaults(Profile::default_contract_release())? - .with_workspace()?; + .with_workspace()?; // todo: combine with delete_workspace_table? Ok(()) })? .using_temp(cargo_build)?; From 3b2818f35db7b9da064a5dfb525d898e8415fd51 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 Apr 2023 16:36:46 +0100 Subject: [PATCH 6/9] Fmt --- crates/build/src/lib.rs | 4 +--- crates/build/src/workspace/manifest.rs | 4 +--- crates/build/src/workspace/mod.rs | 27 +++++++++++++------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index f13593442..83c65aa84 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -329,9 +329,7 @@ fn exec_cargo_dylint(crate_metadata: &CrateMetadata, verbosity: Verbosity) -> Re Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { - manifest - .delete_workspace_table() - .with_dylint()?; + manifest.delete_workspace_table().with_dylint()?; Ok(()) })? .using_temp(|manifest_path| { diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index 3f2c24243..1d51f570c 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -340,9 +340,7 @@ impl Manifest { /// Dependencies with package names specified in `exclude_deps` will not be rewritten. pub fn rewrite_relative_paths(&mut self) -> Result<()> { let manifest_dir = self.path.absolute_directory()?; - let path_rewrite = PathRewrite { - manifest_dir, - }; + let path_rewrite = PathRewrite { manifest_dir }; path_rewrite.rewrite_relative_paths(&mut self.toml) } diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index ac1465744..f4d177b9b 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -34,11 +34,9 @@ use cargo_metadata::{ PackageId, }; -use std::{ - path::{ - Path, - PathBuf, - }, +use std::path::{ + Path, + PathBuf, }; /// Make a copy of a cargo workspace, maintaining only the directory structure and manifest @@ -60,7 +58,9 @@ impl Workspace { .packages .iter() .find(|p| p.id == *root_package) - .ok_or_else(|| anyhow::anyhow!("The root package should be a workspace member"))?; + .ok_or_else(|| { + anyhow::anyhow!("The root package should be a workspace member") + })?; let manifest_path = ManifestPath::new(&root_package.manifest_path)?; let root_manifest = Manifest::new(manifest_path)?; @@ -101,13 +101,14 @@ impl Workspace { /// intra-workspace relative dependency paths which will be preserved. /// /// Returns the paths of the new manifests. - pub fn write>( - &mut self, - target: P, - ) -> Result { + pub fn write>(&mut self, target: P) -> Result { // replace the original workspace root with the temporary directory let mut new_path: PathBuf = target.as_ref().into(); - new_path.push(self.root_package.manifest_path.strip_prefix(&self.workspace_root)?); + new_path.push( + self.root_package + .manifest_path + .strip_prefix(&self.workspace_root)?, + ); let new_manifest = ManifestPath::new(new_path)?; // tracing::info!("Rewriting manifest {} to {}", self.root_manifest.manifest_path, new_manifest.display()); @@ -132,9 +133,7 @@ impl Workspace { // copy the `Cargo.lock` file let src_lockfile = self.workspace_root.clone().join("Cargo.lock"); - let dest_lockfile = tmp_dir - .path() - .join("Cargo.lock"); + let dest_lockfile = tmp_dir.path().join("Cargo.lock"); if src_lockfile.exists() { tracing::debug!( "Copying '{}' to ' '{}'", From 190f70b98310d2520f9da3b09513ba117b49941e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 Apr 2023 11:36:51 +0100 Subject: [PATCH 7/9] Combine delete workspace and set empty workspace --- crates/build/src/lib.rs | 7 ++++--- crates/build/src/metadata.rs | 4 ++-- crates/build/src/workspace/manifest.rs | 21 +++++++-------------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 83c65aa84..4fff670b6 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -284,10 +284,9 @@ fn exec_cargo_for_wasm_target( Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { manifest - .delete_workspace_table() .with_crate_types(["cdylib"])? .with_profile_release_defaults(Profile::default_contract_release())? - .with_workspace()?; // todo: combine with delete_workspace_table? + .with_empty_workspace(); Ok(()) })? .using_temp(cargo_build)?; @@ -329,7 +328,9 @@ fn exec_cargo_dylint(crate_metadata: &CrateMetadata, verbosity: Verbosity) -> Re Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { - manifest.delete_workspace_table().with_dylint()?; + manifest + .with_dylint()? + .with_empty_workspace(); Ok(()) })? .using_temp(|manifest_path| { diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index 2811f751f..f929b75d3 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -186,9 +186,9 @@ pub(crate) fn execute( Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { manifest - .delete_workspace_table() .with_added_crate_type("rlib")? - .with_profile_release_lto(false)?; + .with_profile_release_lto(false)? + .with_empty_workspace(); Ok(()) })? .with_metadata_gen_package()? diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index 1d51f570c..3ddc8b6d8 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -225,15 +225,14 @@ impl Manifest { Ok(self) } - /// Set empty `[workspace]` section if it does not exist. + /// Set `[workspace]` section to an empty table. When building a contract project any workspace + /// members are not copied to the temporary workspace, so need to be removed. /// - /// Ignores the `workspace` from the parent `Cargo.toml`. - /// This can reduce the size of the contract in some cases. - pub fn with_workspace(&mut self) -> Result<&mut Self> { - if let toml::map::Entry::Vacant(value) = self.toml.entry("workspace") { - value.insert(value::Value::Table(Default::default())); - } - Ok(self) + /// Additionally, where no workspace is already specified, this can in some cases reduce the + /// size of the contract. + pub fn with_empty_workspace(&mut self) -> &mut Self { + self.toml.insert("workspace".into(), value::Value::Table(Default::default())); + self } /// Get mutable reference to `[profile.release]` section @@ -344,12 +343,6 @@ impl Manifest { path_rewrite.rewrite_relative_paths(&mut self.toml) } - /// Remove the `[workspace]` section from the manifest. - pub fn delete_workspace_table(&mut self) -> &mut Self { - self.toml.remove("workspace"); - self - } - /// Writes the amended manifest to the given path. pub fn write(&self, manifest_path: &ManifestPath) -> Result<()> { if let Some(dir) = manifest_path.directory() { From 4124ea8e52d429f953a8f0f03cac2f2bd1326937 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 Apr 2023 11:37:26 +0100 Subject: [PATCH 8/9] Fmt --- crates/build/src/lib.rs | 4 +--- crates/build/src/workspace/manifest.rs | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index 4fff670b6..8d7e4f5af 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -328,9 +328,7 @@ fn exec_cargo_dylint(crate_metadata: &CrateMetadata, verbosity: Verbosity) -> Re Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)? .with_root_package_manifest(|manifest| { - manifest - .with_dylint()? - .with_empty_workspace(); + manifest.with_dylint()?.with_empty_workspace(); Ok(()) })? .using_temp(|manifest_path| { diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index 3ddc8b6d8..13246fcb8 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -231,7 +231,8 @@ impl Manifest { /// Additionally, where no workspace is already specified, this can in some cases reduce the /// size of the contract. pub fn with_empty_workspace(&mut self) -> &mut Self { - self.toml.insert("workspace".into(), value::Value::Table(Default::default())); + self.toml + .insert("workspace".into(), value::Value::Table(Default::default())); self } From 0128c565978412dd5ee18c51a4a6c3ae14ef1566 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 Apr 2023 11:52:48 +0100 Subject: [PATCH 9/9] Update comments --- crates/build/src/workspace/manifest.rs | 2 -- crates/build/src/workspace/mod.rs | 23 +++++++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index 13246fcb8..6662307ec 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -336,8 +336,6 @@ impl Manifest { /// /// - `[lib]/path` /// - `[dependencies]` - /// - /// Dependencies with package names specified in `exclude_deps` will not be rewritten. pub fn rewrite_relative_paths(&mut self) -> Result<()> { let manifest_dir = self.path.absolute_directory()?; let path_rewrite = PathRewrite { manifest_dir }; diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index f4d177b9b..86b6aa461 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -39,9 +39,8 @@ use std::path::{ PathBuf, }; -/// Make a copy of a cargo workspace, maintaining only the directory structure and manifest -/// files. Relative paths to source files and non-workspace dependencies are rewritten to absolute -/// paths to the original locations. +/// Make a copy of a contract project manifest, allow modifications to be made to it, rewrite the +/// paths to point to the original project files, then write to a temporary directory. /// /// This allows custom amendments to be made to the manifest files without editing the originals /// directly. @@ -52,7 +51,8 @@ pub struct Workspace { } impl Workspace { - /// Create a new Workspace from the supplied cargo metadata. + /// Create a new Workspace from the supplied cargo metadata and the id of the root contract + /// package. pub fn new(metadata: &CargoMetadata, root_package: &PackageId) -> Result { let root_package = metadata .packages @@ -94,13 +94,10 @@ impl Workspace { Ok(self) } - /// Writes the amended manifests to the `target` directory, retaining the workspace directory - /// structure, but only with the `Cargo.toml` files. + /// Writes the amended manifest to the `target` directory. Relative paths will be rewritten to + /// absolute paths from the original project root. /// - /// Relative paths will be rewritten to absolute paths from the original workspace root, except - /// intra-workspace relative dependency paths which will be preserved. - /// - /// Returns the paths of the new manifests. + /// Returns the path of the new manifest. pub fn write>(&mut self, target: P) -> Result { // replace the original workspace root with the temporary directory let mut new_path: PathBuf = target.as_ref().into(); @@ -111,16 +108,14 @@ impl Workspace { ); let new_manifest = ManifestPath::new(new_path)?; - // tracing::info!("Rewriting manifest {} to {}", self.root_manifest.manifest_path, new_manifest.display()); - self.root_manifest.rewrite_relative_paths()?; self.root_manifest.write(&new_manifest)?; Ok(new_manifest) } - /// Copy the workspace with amended manifest files to a temporary directory, executing the - /// supplied function with the root manifest path before the directory is cleaned up. + /// Write the amended manifest file to a temporary directory, then execute the supplied function + /// with the temporary manifest path before the directory is cleaned up. pub fn using_temp(&mut self, f: F) -> Result<()> where F: FnOnce(&ManifestPath) -> Result<()>,