@@ -12,7 +12,7 @@ use semver::{Version, VersionReq};
1212use serde:: { Deserialize , Serialize } ;
1313use tokio:: {
1414 fs:: { File , OpenOptions } ,
15- io:: { AsyncSeekExt , AsyncWriteExt } ,
15+ io:: { AsyncReadExt , AsyncSeekExt , AsyncWriteExt } ,
1616} ;
1717use 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( )
@@ -878,7 +886,7 @@ mod tests {
878886
879887 // Now read the lock file again and make sure everything is correct (and we can lock it
880888 // properly)
881- let lock = LockFile :: load_from_path ( & path, true )
889+ let lock = LockFile :: load_from_path ( & path, false )
882890 . await
883891 . expect ( "Shouldn't fail when loading lock file" ) ;
884892 assert_eq ! (
0 commit comments