@@ -26,9 +26,28 @@ export default class InstallState {
2626
2727 state . storeUrl = Url . join ( INSTALL_STATE_STORAGE , state . id ) ;
2828 if ( await fsOperation ( state . storeUrl ) . exists ( ) ) {
29- state . store = JSON . parse (
30- await fsOperation ( state . storeUrl ) . readFile ( "utf-8" ) ,
31- ) ;
29+ let raw = "{}" ;
30+ try {
31+ raw = await fsOperation ( state . storeUrl ) . readFile ( "utf-8" ) ;
32+ state . store = JSON . parse ( raw ) ;
33+ } catch ( err ) {
34+ console . error (
35+ "InstallState: Failed to parse state file, deleting:" ,
36+ err ,
37+ ) ;
38+ // Delete corrupted state file to avoid parse errors such as 'Unexpected end of JSON'
39+ state . store = { } ;
40+ try {
41+ await fsOperation ( state . storeUrl ) . delete ( ) ;
42+ // Recreate a fresh empty file to keep invariant
43+ await fsOperation ( INSTALL_STATE_STORAGE ) . createFile ( state . id ) ;
44+ } catch ( writeErr ) {
45+ console . error (
46+ "InstallState: Failed to recreate state file:" ,
47+ writeErr ,
48+ ) ;
49+ }
50+ }
3251
3352 const patchedStore = { } ;
3453 for ( const [ key , value ] of Object . entries ( state . store ) ) {
@@ -101,7 +120,19 @@ export default class InstallState {
101120 try {
102121 this . store = { } ;
103122 this . updatedStore = { } ;
104- await fsOperation ( this . storeUrl ) . writeFile ( "{}" ) ;
123+ // Delete the state file entirely to avoid corrupted/partial JSON issues
124+ if ( await fsOperation ( this . storeUrl ) . exists ( ) ) {
125+ try {
126+ await fsOperation ( this . storeUrl ) . delete ( ) ;
127+ } catch ( delErr ) {
128+ console . error (
129+ "InstallState: Failed to delete state file during clear:" ,
130+ delErr ,
131+ ) ;
132+ // As a fallback, overwrite with a valid empty JSON
133+ await fsOperation ( this . storeUrl ) . writeFile ( "{}" ) ;
134+ }
135+ }
105136 } catch ( error ) {
106137 console . error ( "Failed to clear install state:" , error ) ;
107138 }
0 commit comments