Skip to content

Commit 9c31e46

Browse files
authored
Fixes for persisting trust (#12950)
1 parent da5f1bb commit 9c31e46

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/client/datascience/interactive-ipynb/nativeEditorStorage.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ export class NativeEditorStorage implements INotebookStorage {
282282
const dirtyContents = skipDirtyContents ? undefined : await this.getStoredContents(file, backupId);
283283
if (dirtyContents) {
284284
// This means we're dirty. Indicate dirty and load from this content
285-
return this.loadContents(file, dirtyContents, true, contents, forVSCodeNotebook);
285+
return this.loadContents(file, dirtyContents, true, forVSCodeNotebook);
286286
} else {
287287
// Load without setting dirty
288-
return this.loadContents(file, contents, undefined, undefined, forVSCodeNotebook);
288+
return this.loadContents(file, contents, undefined, forVSCodeNotebook);
289289
}
290290
} catch (ex) {
291291
// May not exist at this time. Should always have a single cell though
@@ -308,7 +308,6 @@ export class NativeEditorStorage implements INotebookStorage {
308308
file: Uri,
309309
contents: string | undefined,
310310
isInitiallyDirty = false,
311-
trueContents?: string,
312311
forVSCodeNotebook?: boolean
313312
) {
314313
// tslint:disable-next-line: no-any
@@ -348,18 +347,9 @@ export class NativeEditorStorage implements INotebookStorage {
348347
}
349348
const pythonNumber = json ? await this.extractPythonMainVersion(json) : 3;
350349

351-
/* As an optimization, we don't call trustNotebook for hot exit, since our hot exit backup code gets called by VS
352-
Code whenever the notebook model changes. This means it's called very often, perhaps even as often as autosave.
353-
Instead, when loading a file that is dirty, we check if the actual file contents on disk are trusted. If so, we treat
354-
the dirty contents as trusted as well. */
355-
const contentsToCheck = isInitiallyDirty && trueContents !== undefined ? trueContents : contents;
356-
const isTrusted =
357-
contents === undefined || isUntitledFile(file)
358-
? true // If no contents or untitled, this is a newly created file, so it should be trusted
359-
: await this.trustService.isNotebookTrusted(file, contentsToCheck!);
360-
return this.factory.createModel(
350+
const model = this.factory.createModel(
361351
{
362-
trusted: isTrusted,
352+
trusted: true,
363353
file,
364354
cells: remapped,
365355
notebookJson: json,
@@ -369,6 +359,23 @@ export class NativeEditorStorage implements INotebookStorage {
369359
},
370360
forVSCodeNotebook
371361
);
362+
363+
// If no contents or untitled, this is a newly created file
364+
// If dirty, that means it's been edited before in our extension
365+
if (contents !== undefined && !isUntitledFile(file) && !isInitiallyDirty) {
366+
const isNotebookTrusted = await this.trustService.isNotebookTrusted(file, model.getContent());
367+
if (isNotebookTrusted !== model.isTrusted) {
368+
model.update({
369+
source: 'user',
370+
kind: 'updateTrust',
371+
oldDirty: model.isDirty,
372+
newDirty: model.isDirty,
373+
isNotebookTrusted
374+
});
375+
}
376+
}
377+
378+
return model;
372379
}
373380

374381
private getStaticStorageKey(file: Uri): string {

src/client/datascience/notebookStorage/baseModel.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,8 @@ export abstract class BaseNotebookModel implements INotebookModel {
196196
this.ensureNotebookJson();
197197

198198
// Reuse our original json except for the cells.
199-
const json = {
200-
cells: this.cells.map((c) => pruneCell(c.data)),
201-
metadata: this.notebookJson.metadata,
202-
nbformat: this.notebookJson.nbformat,
203-
nbformat_minor: this.notebookJson.nbformat_minor
204-
};
199+
const json = { ...this.notebookJson };
200+
json.cells = this.cells.map((c) => pruneCell(c.data));
205201
return JSON.stringify(json, null, this.indentAmount);
206202
}
207203
}

0 commit comments

Comments
 (0)