xxfast / KStore

A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and kotlinx.io
https://xxfast.github.io/KStore/
Apache License 2.0
540 stars 18 forks source link

.temp File does not exist on 0.9.0-SNAPSHOT #130

Closed SultanArshad closed 1 month ago

SultanArshad commented 1 month ago

App crashes on start-up after using 0.9.0-SNAPSHOT.

What I have done so far/How to reproduce:

Logs:

FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: packageName, PID: 7697 java.io.FileNotFoundException: File does not exist: fileName.json.temp at kotlinx.io.files.FileSystemJvmKt$SystemFileSystem$1.delete(FileSystemJvm.kt:61) at kotlinx.io.files.FileSystem.delete$default(FileSystem.kt:50) at io.github.xxfast.kstore.file.FileCodec.encode(FileCodec.kt:68) at io.github.xxfast.kstore.KStore$write$2.invokeSuspend(KStore.kt:56) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101) at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:823) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e0ff0d, Dispatchers.Default]

h-forhad commented 1 month ago

I might be wrong, but after a bit of investigation, I think the issue is with the FileCodec declaration. The parent directory for the file seems to be missing from the following declaration:

public inline fun <reified T : @Serializable Any> FileCodec(
  file: Path,
  tempFile: Path = Path("${file.name}.temp"),
  json: Json = DefaultJson,
): FileCodec<T> = FileCodec(
  file = file,
  tempFile = tempFile,
  json = json,
  serializer = json.serializersModule.serializer(),
)

Probable Solutions:

public inline fun <reified T : @Serializable Any> FileCodec(
  file: Path,
  tempFile: Path = Path("${file.parent}/${file.name}.temp"),
  json: Json = DefaultJson,
): FileCodec<T> = FileCodec(
  file = file,
  tempFile = tempFile,
  json = json,
  serializer = json.serializersModule.serializer(),
)
xxfast commented 1 month ago

Nice catch guys!

I think we should be able to do just

public inline fun <reified T : @Serializable Any> FileCodec(
  file: Path,
  tempFile: Path = Path("$file.temp"),
  json: Json = DefaultJson,
): FileCodec<T> = FileCodec(
  file = file,
  tempFile = tempFile,
  json = json,
  serializer = json.serializersModule.serializer(),
)

PRs welcome :)

xxfast commented 1 month ago

Done! Released on 0.9.0. Thank you for the contribution