Fixes bug in wasm-builder with cargo publish (#7299)

* Fixes bug in wasm-builder with cargo publish

There was a bug in wasm-builder which resulted in generating a
`Cargo.lock` in the project directory because of running `cargo
metadata`. This resulted in commands like `cargo publish` to fail (if
there was no `Cargo.lock` before building), because it checks that the
project directory isn't modified.

* Update utils/wasm-builder/src/wasm_project.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-10-12 12:29:53 +02:00
committed by GitHub
parent 5172777c7d
commit 1a01bca647
10 changed files with 42 additions and 23 deletions
+1 -1
View File
@@ -8906,7 +8906,7 @@ dependencies = [
[[package]]
name = "substrate-wasm-builder"
version = "2.0.0"
version = "2.0.1"
dependencies = [
"ansi_term 0.12.1",
"atty",
+1 -1
View File
@@ -3,7 +3,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates("2.0.0")
.with_wasm_builder_from_crates("2.0.1")
.export_heap_base()
.import_memory()
.build()
+1 -1
View File
@@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
@@ -20,7 +20,7 @@ fn main() {
// regular build
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build();
@@ -28,7 +28,7 @@ fn main() {
// and building with tracing activated
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.set_file_name("wasm_binary_with_tracing.rs")
@@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
@@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
+1 -1
View File
@@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("2.0.0", "../../utils/wasm-builder")
.with_wasm_builder_from_crates_or_path("2.0.1", "../../utils/wasm-builder")
.export_heap_base()
// Note that we set the stack-size to 1MB explicitly even though it is set
// to this value by default. This is because some of our tests (`restoration_of_globals`)
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "substrate-wasm-builder"
version = "2.0.0"
version = "2.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Utility for building WASM binaries"
edition = "2018"
+10 -10
View File
@@ -181,16 +181,16 @@ pub fn build_project_with_default_rustflags(
};
write_file_if_changed(
file_name.into(),
format!(
r#"
pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}"));
pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}"));
"#,
wasm_binary = wasm_binary,
wasm_binary_bloaty = wasm_binary_bloaty,
),
);
file_name.into(),
format!(
r#"
pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}"));
pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}"));
"#,
wasm_binary = wasm_binary,
wasm_binary_bloaty = wasm_binary_bloaty,
),
);
}
/// Checks if the build of the WASM binary should be skipped.
@@ -84,6 +84,28 @@ impl Drop for WorkspaceLock {
}
}
fn crate_metadata(cargo_manifest: &Path) -> Metadata {
let mut cargo_lock = cargo_manifest.to_path_buf();
cargo_lock.set_file_name("Cargo.lock");
let cargo_lock_existed = cargo_lock.exists();
let crate_metadata = MetadataCommand::new()
.manifest_path(cargo_manifest)
.exec()
.expect("`cargo metadata` can not fail on project `Cargo.toml`; qed");
// If the `Cargo.lock` didn't exist, we need to remove it after
// calling `cargo metadata`. This is required to ensure that we don't change
// the build directory outside of the `target` folder. Commands like
// `cargo publish` require this.
if !cargo_lock_existed {
let _ = fs::remove_file(&cargo_lock);
}
crate_metadata
}
/// Creates the WASM project, compiles the WASM binary and compacts the WASM binary.
///
/// # Returns
@@ -98,10 +120,7 @@ pub fn create_and_compile(
// Lock the workspace exclusively for us
let _lock = WorkspaceLock::new(&wasm_workspace_root);
let crate_metadata = MetadataCommand::new()
.manifest_path(cargo_manifest)
.exec()
.expect("`cargo metadata` can not fail on project `Cargo.toml`; qed");
let crate_metadata = crate_metadata(cargo_manifest);
let project = create_project(cargo_manifest, &wasm_workspace, &crate_metadata);
create_wasm_workspace_project(&wasm_workspace, &crate_metadata.workspace_root);