From f3dc545b47200acdb49b28b15c66d528bf6afb67 Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Sun, 12 Jul 2020 17:19:13 -0700 Subject: [PATCH] Recover from ENOENT due to missing parent dir --- .../interactive-ipynb/digestStorage.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/client/datascience/interactive-ipynb/digestStorage.ts b/src/client/datascience/interactive-ipynb/digestStorage.ts index cba0fa78c91e..0932ee64120b 100644 --- a/src/client/datascience/interactive-ipynb/digestStorage.ts +++ b/src/client/datascience/interactive-ipynb/digestStorage.ts @@ -25,7 +25,21 @@ export class DigestStorage implements IDigestStorage { public async saveDigest(uri: Uri, signature: string) { const fileLocation = await this.getFileLocation(uri); // Since the signature is a hex digest, the character 'z' is being used to delimit the start and end of a single digest - await this.fs.appendFile(fileLocation, `z${signature}z\n`); + try { + await this.fs.appendFile(fileLocation, `z${signature}z\n`); + } catch (err) { + // The nbsignatures dir is only initialized on extension activation. + // If the user deletes it to reset trust, the next attempt to trust + // an untrusted notebook in the same session will fail because the parent + // directory does not exist. + if (isFileNotFoundError(err)) { + // Gracefully recover from such errors by reinitializing directory and retrying + await this.initDir(); + await this.fs.appendFile(fileLocation, `z${signature}z\n`); + } else { + traceError(err); + } + } } public async containsDigest(uri: Uri, signature: string) {