diff --git a/src/client/datascience/interactive-ipynb/nativeEditorStorage.ts b/src/client/datascience/interactive-ipynb/nativeEditorStorage.ts index 10f2e29eb8ed..d9600bf8f483 100644 --- a/src/client/datascience/interactive-ipynb/nativeEditorStorage.ts +++ b/src/client/datascience/interactive-ipynb/nativeEditorStorage.ts @@ -282,10 +282,10 @@ export class NativeEditorStorage implements INotebookStorage { const dirtyContents = skipDirtyContents ? undefined : await this.getStoredContents(file, backupId); if (dirtyContents) { // This means we're dirty. Indicate dirty and load from this content - return this.loadContents(file, dirtyContents, true, contents, forVSCodeNotebook); + return this.loadContents(file, dirtyContents, true, forVSCodeNotebook); } else { // Load without setting dirty - return this.loadContents(file, contents, undefined, undefined, forVSCodeNotebook); + return this.loadContents(file, contents, undefined, forVSCodeNotebook); } } catch (ex) { // May not exist at this time. Should always have a single cell though @@ -308,7 +308,6 @@ export class NativeEditorStorage implements INotebookStorage { file: Uri, contents: string | undefined, isInitiallyDirty = false, - trueContents?: string, forVSCodeNotebook?: boolean ) { // tslint:disable-next-line: no-any @@ -348,18 +347,9 @@ export class NativeEditorStorage implements INotebookStorage { } const pythonNumber = json ? await this.extractPythonMainVersion(json) : 3; - /* As an optimization, we don't call trustNotebook for hot exit, since our hot exit backup code gets called by VS - Code whenever the notebook model changes. This means it's called very often, perhaps even as often as autosave. - Instead, when loading a file that is dirty, we check if the actual file contents on disk are trusted. If so, we treat - the dirty contents as trusted as well. */ - const contentsToCheck = isInitiallyDirty && trueContents !== undefined ? trueContents : contents; - const isTrusted = - contents === undefined || isUntitledFile(file) - ? true // If no contents or untitled, this is a newly created file, so it should be trusted - : await this.trustService.isNotebookTrusted(file, contentsToCheck!); - return this.factory.createModel( + const model = this.factory.createModel( { - trusted: isTrusted, + trusted: true, file, cells: remapped, notebookJson: json, @@ -369,6 +359,23 @@ export class NativeEditorStorage implements INotebookStorage { }, forVSCodeNotebook ); + + // If no contents or untitled, this is a newly created file + // If dirty, that means it's been edited before in our extension + if (contents !== undefined && !isUntitledFile(file) && !isInitiallyDirty) { + const isNotebookTrusted = await this.trustService.isNotebookTrusted(file, model.getContent()); + if (isNotebookTrusted !== model.isTrusted) { + model.update({ + source: 'user', + kind: 'updateTrust', + oldDirty: model.isDirty, + newDirty: model.isDirty, + isNotebookTrusted + }); + } + } + + return model; } private getStaticStorageKey(file: Uri): string { diff --git a/src/client/datascience/notebookStorage/baseModel.ts b/src/client/datascience/notebookStorage/baseModel.ts index 89fbaf7c5bad..452534c162e5 100644 --- a/src/client/datascience/notebookStorage/baseModel.ts +++ b/src/client/datascience/notebookStorage/baseModel.ts @@ -196,12 +196,8 @@ export abstract class BaseNotebookModel implements INotebookModel { this.ensureNotebookJson(); // Reuse our original json except for the cells. - const json = { - cells: this.cells.map((c) => pruneCell(c.data)), - metadata: this.notebookJson.metadata, - nbformat: this.notebookJson.nbformat, - nbformat_minor: this.notebookJson.nbformat_minor - }; + const json = { ...this.notebookJson }; + json.cells = this.cells.map((c) => pruneCell(c.data)); return JSON.stringify(json, null, this.indentAmount); } }