Skip to content

Commit 82d40fd

Browse files
fix(core): Fixes loading file on windows
When we were loading the file, we were locking it and then opening another file handle to read it. The test was loading it readonly rather than using a shared lock, so this didn't catch the edge case Signed-off-by: Taylor Thomas <[email protected]>
1 parent 873e77a commit 82d40fd

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resolver = "2"
44

55
[workspace.package]
66
edition = "2021"
7-
version = "0.8.2"
7+
version = "0.8.3"
88
authors = ["The Wasmtime Project Developers"]
99
license = "Apache-2.0 WITH LLVM-exception"
1010

@@ -36,9 +36,9 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
3636
"fmt",
3737
"env-filter",
3838
] }
39-
wasm-pkg-common = { version = "0.8.2", path = "crates/wasm-pkg-common" }
40-
wasm-pkg-client = { version = "0.8.2", path = "crates/wasm-pkg-client" }
39+
wasm-pkg-common = { version = "0.8.3", path = "crates/wasm-pkg-common" }
40+
wasm-pkg-client = { version = "0.8.3", path = "crates/wasm-pkg-client" }
4141
wasm-metadata = "0.219"
4242
wit-component = "0.219"
4343
wit-parser = "0.219"
44-
wasm-pkg-core = { version = "0.8.2", path = "crates/wasm-pkg-core" }
44+
wasm-pkg-core = { version = "0.8.3", path = "crates/wasm-pkg-core" }

crates/wasm-pkg-core/src/lock.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use semver::{Version, VersionReq};
1212
use serde::{Deserialize, Serialize};
1313
use tokio::{
1414
fs::{File, OpenOptions},
15-
io::{AsyncSeekExt, AsyncWriteExt},
15+
io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
1616
};
1717
use wasm_pkg_client::{ContentDigest, PackageRef};
1818

@@ -70,12 +70,14 @@ impl LockFile {
7070
/// Loads a lock file from the given path. If readonly is set to false, then an exclusive lock
7171
/// will be acquired on the file. This function will block until the lock is acquired.
7272
pub async fn load_from_path(path: impl AsRef<Path>, readonly: bool) -> Result<Self> {
73-
let locker = if readonly {
73+
let mut locker = if readonly {
7474
Locker::open_ro(path.as_ref()).await
7575
} else {
7676
Locker::open_rw(path.as_ref()).await
7777
}?;
78-
let contents = tokio::fs::read_to_string(path)
78+
let mut contents = String::new();
79+
locker
80+
.read_to_string(&mut contents)
7981
.await
8082
.context("unable to load lock file from path")?;
8183
let lock_file: LockFileIntermediate =
@@ -87,6 +89,13 @@ impl LockFile {
8789
lock_file.version
8890
));
8991
}
92+
// Rewind the file after reading just to be safe. We already do this before writing, but
93+
// just in case we add any future logic, we can reset the file here so as to not cause
94+
// issues
95+
locker
96+
.rewind()
97+
.await
98+
.context("Unable to reset file after reading")?;
9099
Ok(lock_file.into_lock_file(locker))
91100
}
92101

@@ -131,25 +140,24 @@ impl LockFile {
131140
pub async fn write(&mut self) -> Result<()> {
132141
let contents = toml::to_string_pretty(self)?;
133142
// Truncate the file before writing to it
134-
self.locker.file.rewind().await.with_context(|| {
143+
self.locker.rewind().await.with_context(|| {
135144
format!(
136145
"unable to rewind lock file at path {}",
137146
self.locker.path.display()
138147
)
139148
})?;
140-
self.locker.file.set_len(0).await.with_context(|| {
149+
self.locker.set_len(0).await.with_context(|| {
141150
format!(
142151
"unable to truncate lock file at path {}",
143152
self.locker.path.display()
144153
)
145154
})?;
146155

147-
self.locker.file.write_all(
156+
self.locker.write_all(
148157
b"# This file is automatically generated.\n# It is not intended for manual editing.\n",
149158
)
150159
.await.with_context(|| format!("unable to write lock file to path {}", self.locker.path.display()))?;
151160
self.locker
152-
.file
153161
.write_all(contents.as_bytes())
154162
.await
155163
.with_context(|| {
@@ -160,7 +168,7 @@ impl LockFile {
160168
})?;
161169
// Make sure to flush and sync just to be sure the file doesn't drop and the lock is
162170
// released too early
163-
self.locker.file.sync_all().await.with_context(|| {
171+
self.locker.sync_all().await.with_context(|| {
164172
format!(
165173
"unable to write lock file to path {}",
166174
self.locker.path.display()
@@ -860,7 +868,7 @@ mod tests {
860868

861869
// Now read the lock file again and make sure everything is correct (and we can lock it
862870
// properly)
863-
let lock = LockFile::load_from_path(&path, true)
871+
let lock = LockFile::load_from_path(&path, false)
864872
.await
865873
.expect("Shouldn't fail when loading lock file");
866874
assert_eq!(

0 commit comments

Comments
 (0)