Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if .ipynb document metadata isn't present, fall back to interpreting the kernelspec #2692

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/dotnet-interactive-vscode-common/src/metadataUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export function getNotebookDocumentMetadataFromInteractiveDocument(interactiveDo

export function getNotebookDocumentMetadataFromNotebookDocument(document: vscodeLike.NotebookDocument): NotebookDocumentMetadata {
const notebookMetadata = createDefaultNotebookDocumentMetadata();
let setDefaultKernel = false;
let setItems = false;

// .dib files will have their metadata at the root; .ipynb files will have their metadata a little deeper
const polyglot_notebook = document.metadata.polyglot_notebook ?? document.metadata?.custom?.metadata?.polyglot_notebook;
Expand All @@ -120,15 +122,35 @@ export function getNotebookDocumentMetadataFromNotebookDocument(document: vscode
if (typeof kernelInfo === 'object') {
if (typeof kernelInfo.defaultKernelName === 'string') {
notebookMetadata.kernelInfo.defaultKernelName = kernelInfo.defaultKernelName;
setDefaultKernel = true;
}

const items = kernelInfo.items;
if (Array.isArray(items) && items.every(item => typeof item === 'object')) {
notebookMetadata.kernelInfo.items = items;
setItems = true;
}
}
} else {
const x = 1;
}

// if nothing was found, populate it from the kernelspec metadata
if (isIpynbNotebook(document)) {
if (!setDefaultKernel) {
const kernelSpecMetadata = getKernelspecMetadataFromIpynbNotebookDocument(document);
if (kernelSpecMetadata.name.startsWith('.net-')) {
// the command `dotnet interactive jupyter install` lays down 3 well-known kernelspecs, all with the name `.net-<kernelName>`
notebookMetadata.kernelInfo.defaultKernelName = kernelSpecMetadata.name.substring('.net-'.length);
}
}

if (!setItems) {
notebookMetadata.kernelInfo.items = [
{
name: notebookMetadata.kernelInfo.defaultKernelName,
aliases: [],
}
];
}
}

notebookMetadata.kernelInfo.items = notebookMetadata.kernelInfo.items.map(item => ensureProperShapeForDocumentKernelInfo(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class DotNetNotebookCellStatusBarItemProvider {
return [];
}

const notebookMetadata = metadataUtilities.getNotebookDocumentMetadataFromNotebookDocument(cell.notebook);
const cellMetadata = metadataUtilities.getNotebookCellMetadataFromNotebookCellElement(cell);
const cellKernelName = cellMetadata.kernelName ?? 'csharp';
const cellKernelName = cellMetadata.kernelName ?? notebookMetadata.kernelInfo.defaultKernelName;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core of the fix; everything else is just making sure we have something meaningful here.

const notebookDocument = getNotebookDcoumentFromCellDocument(cell.document);
const client = await this.clientMapper.tryGetClient(notebookDocument!.uri); // don't force client creation
let displayText: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,38 @@ describe('metadata utility tests', async () => {
});
});

it('notebook document metadata can be synthesized from .ipynb kernelspec', () => {
const notebookDocument: vscodeLike.NotebookDocument = {
uri: {
fsPath: 'test-notebook.ipynb',
scheme: 'unused',
},
metadata: {
custom: {
metadata: {
kernelspec: {
display_name: '.NET (F#)',
language: 'F#',
name: '.net-fsharp',
}
}
}
}
};
const notebookDocumentMetadata = metadataUtilities.getNotebookDocumentMetadataFromNotebookDocument(notebookDocument);
expect(notebookDocumentMetadata).to.deep.equal({
kernelInfo: {
defaultKernelName: 'fsharp',
items: [
{
name: 'fsharp',
aliases: [],
}
]
}
});
});

it('kernel infos can be created from .dib notebook document', () => {
const notebookDocument: vscodeLike.NotebookDocument = {
uri: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ export class DotNetNotebookKernel {
const notebookMetadata = metadataUtilities.getNotebookDocumentMetadataFromNotebookDocument(notebook);
const cells = notebook.getCells();
const foundCell = cells.find(cell => cell.document === textDocument);
if (foundCell && foundCell.index > 0) {
// if we found the cell and it's not the first, ensure it has kernel metadata
if (foundCell) {
// if we found the cell ensure it has kernel metadata
const cellMetadata = metadataUtilities.getNotebookCellMetadataFromNotebookCellElement(foundCell);
if (!cellMetadata.kernelName) {
// no kernel metadata; copy from previous cell
const previousCell = cells[foundCell.index - 1];
const previousCellMetadata = metadataUtilities.getNotebookCellMetadataFromNotebookCellElement(previousCell);
await vscodeUtilities.setCellKernelName(foundCell, previousCellMetadata.kernelName ?? notebookMetadata.kernelInfo.defaultKernelName);
// no kernel name; set it from the notebook metadata
await vscodeUtilities.setCellKernelName(foundCell, notebookMetadata.kernelInfo.defaultKernelName);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part 2 of the core fix; when we first open a document, we ensure each cell has a kernel name set.

}
}
}
Expand Down