fix: avoid NPE when failing to create missing dirs on writeFile (#7833)

Related to #7829

We have test coverage for this case (the directory is created automatically if missing), but apparently there's a scenario where it can't be created. This change makes the code handle such situations gracefully: instead of throwing an error, the file simply won't be created.

Added extra logging to help debug this issue further.
This commit is contained in:
Jakub Błach 2025-09-18 21:30:51 +02:00 • committed by GitHub
parent a64eb6dc42
commit e4ce400cf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,7 @@ import com.github.continuedev.continueintellijextension.FileStats
import com.github.continuedev.continueintellijextension.FileType
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
@ -24,10 +25,11 @@ class FileUtils(
fun writeFile(fileUri: String, content: String) {
val path = VfsUtilCore.urlToPath(fileUri)
val pathDirectory = VfsUtil.getParentDir(path)
?: return
?: return LOG.warn("Parent directory is null for $path")
val vfsDirectory = VfsUtil.createDirectories(pathDirectory)
?: return LOG.warn("Could not create directories for $path")
val pathFilename = VfsUtil.extractFileName(path)
?: return
?: return LOG.warn("Could not get filename for $path")
runWriteAction {
val newFile = vfsDirectory.createChildData(this, pathFilename)
VfsUtil.saveText(newFile, content)
@ -107,4 +109,8 @@ class FileUtils(
}
return fileUri
}
private companion object {
private val LOG = Logger.getInstance(FileUtils::class.java)
}
}