diff --git a/Cargo.lock b/Cargo.lock index 3eb4d45..5f92b10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,9 +130,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.91" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +checksum = "74f37166d7d48a0284b99dd824694c26119c700b53bf0d1540cdb147dbdaaf13" [[package]] name = "arraydeque" @@ -4250,7 +4250,7 @@ dependencies = [ [[package]] name = "wasm-pkg-client" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "async-trait", @@ -4284,7 +4284,7 @@ dependencies = [ [[package]] name = "wasm-pkg-common" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "bytes", @@ -4304,7 +4304,7 @@ dependencies = [ [[package]] name = "wasm-pkg-core" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "futures-util", @@ -4690,7 +4690,7 @@ dependencies = [ [[package]] name = "wkg" -version = "0.8.2" +version = "0.8.3" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index d32c316..dca2fa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ resolver = "2" [workspace.package] edition = "2021" -version = "0.8.2" +version = "0.8.3" authors = ["The Wasmtime Project Developers"] license = "Apache-2.0 WITH LLVM-exception" @@ -36,9 +36,9 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features = "fmt", "env-filter", ] } -wasm-pkg-common = { version = "0.8.2", path = "crates/wasm-pkg-common" } -wasm-pkg-client = { version = "0.8.2", path = "crates/wasm-pkg-client" } +wasm-pkg-common = { version = "0.8.3", path = "crates/wasm-pkg-common" } +wasm-pkg-client = { version = "0.8.3", path = "crates/wasm-pkg-client" } wasm-metadata = "0.219" wit-component = "0.219" wit-parser = "0.219" -wasm-pkg-core = { version = "0.8.2", path = "crates/wasm-pkg-core" } +wasm-pkg-core = { version = "0.8.3", path = "crates/wasm-pkg-core" } diff --git a/crates/wasm-pkg-core/src/lock.rs b/crates/wasm-pkg-core/src/lock.rs index 0ed59d5..1bb5a00 100644 --- a/crates/wasm-pkg-core/src/lock.rs +++ b/crates/wasm-pkg-core/src/lock.rs @@ -12,7 +12,7 @@ use semver::{Version, VersionReq}; use serde::{Deserialize, Serialize}; use tokio::{ fs::{File, OpenOptions}, - io::{AsyncSeekExt, AsyncWriteExt}, + io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}, }; use wasm_pkg_client::{ContentDigest, PackageRef}; @@ -70,12 +70,14 @@ impl LockFile { /// Loads a lock file from the given path. If readonly is set to false, then an exclusive lock /// will be acquired on the file. This function will block until the lock is acquired. pub async fn load_from_path(path: impl AsRef, readonly: bool) -> Result { - let locker = if readonly { + let mut locker = if readonly { Locker::open_ro(path.as_ref()).await } else { Locker::open_rw(path.as_ref()).await }?; - let contents = tokio::fs::read_to_string(path) + let mut contents = String::new(); + locker + .read_to_string(&mut contents) .await .context("unable to load lock file from path")?; let lock_file: LockFileIntermediate = @@ -87,6 +89,13 @@ impl LockFile { lock_file.version )); } + // Rewind the file after reading just to be safe. We already do this before writing, but + // just in case we add any future logic, we can reset the file here so as to not cause + // issues + locker + .rewind() + .await + .context("Unable to reset file after reading")?; Ok(lock_file.into_lock_file(locker)) } @@ -131,25 +140,24 @@ impl LockFile { pub async fn write(&mut self) -> Result<()> { let contents = toml::to_string_pretty(self)?; // Truncate the file before writing to it - self.locker.file.rewind().await.with_context(|| { + self.locker.rewind().await.with_context(|| { format!( "unable to rewind lock file at path {}", self.locker.path.display() ) })?; - self.locker.file.set_len(0).await.with_context(|| { + self.locker.set_len(0).await.with_context(|| { format!( "unable to truncate lock file at path {}", self.locker.path.display() ) })?; - self.locker.file.write_all( + self.locker.write_all( b"# This file is automatically generated.\n# It is not intended for manual editing.\n", ) .await.with_context(|| format!("unable to write lock file to path {}", self.locker.path.display()))?; self.locker - .file .write_all(contents.as_bytes()) .await .with_context(|| { @@ -160,7 +168,7 @@ impl LockFile { })?; // Make sure to flush and sync just to be sure the file doesn't drop and the lock is // released too early - self.locker.file.sync_all().await.with_context(|| { + self.locker.sync_all().await.with_context(|| { format!( "unable to write lock file to path {}", self.locker.path.display() @@ -860,7 +868,7 @@ mod tests { // Now read the lock file again and make sure everything is correct (and we can lock it // properly) - let lock = LockFile::load_from_path(&path, true) + let lock = LockFile::load_from_path(&path, false) .await .expect("Shouldn't fail when loading lock file"); assert_eq!(