-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn when files could override Python modules (#10787)
* Misc * Fixes * Changes * misc * Add tests * Fixes * Misc * Fix tests * Misc * Remove console logging * More fixes * Added tests * Misc * Move files * More tests * Misc * Address code review comments
- Loading branch information
1 parent
31c561d
commit 4d1a5c7
Showing
45 changed files
with
1,950 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Warn users when a Python file could override an existing Python package (there by interfering with the kernels. [More info](https://aka.ms/JupyterKernelStartFailureOverrideReservedName)). | ||
This feature could be turned off via the setting `"jupyter.diagnostics.reservedPythonNames.enabled": false`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import { inject, injectable, optional } from 'inversify'; | ||
import { Uri } from 'vscode'; | ||
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../platform/common/application/types'; | ||
import { | ||
IBrowserService, | ||
IConfigurationService, | ||
IExtensions, | ||
IsWebExtension, | ||
Resource | ||
} from '../../platform/common/types'; | ||
import { DataScience, Common } from '../../platform/common/utils/localize'; | ||
import { IKernelDependencyService } from '../types'; | ||
import { IJupyterInterpreterDependencyManager, IJupyterServerUriStorage } from '../jupyter/types'; | ||
import * as path from '../../platform/vscode-path/resources'; | ||
import { IReservedPythonNamedProvider } from '../../platform/interpreter/types'; | ||
import { JupyterKernelStartFailureOverrideReservedName } from '../../platform/interpreter/constants'; | ||
import { DataScienceErrorHandler } from './kernelErrorHandler'; | ||
import { getDisplayPath } from '../../platform/common/platform/fs-paths'; | ||
|
||
@injectable() | ||
export class DataScienceErrorHandlerNode extends DataScienceErrorHandler { | ||
constructor( | ||
@inject(IApplicationShell) applicationShell: IApplicationShell, | ||
@inject(IJupyterInterpreterDependencyManager) | ||
@optional() | ||
dependencyManager: IJupyterInterpreterDependencyManager | undefined, | ||
@inject(IBrowserService) browser: IBrowserService, | ||
@inject(IConfigurationService) configuration: IConfigurationService, | ||
@inject(IKernelDependencyService) | ||
@optional() | ||
kernelDependency: IKernelDependencyService | undefined, | ||
@inject(IWorkspaceService) workspaceService: IWorkspaceService, | ||
@inject(IJupyterServerUriStorage) serverUriStorage: IJupyterServerUriStorage, | ||
@inject(ICommandManager) commandManager: ICommandManager, | ||
@inject(IsWebExtension) isWebExtension: boolean, | ||
@inject(IExtensions) extensions: IExtensions, | ||
@inject(IReservedPythonNamedProvider) private readonly reservedPythonNames: IReservedPythonNamedProvider | ||
) { | ||
super( | ||
applicationShell, | ||
dependencyManager, | ||
browser, | ||
configuration, | ||
kernelDependency, | ||
workspaceService, | ||
serverUriStorage, | ||
commandManager, | ||
isWebExtension, | ||
extensions | ||
); | ||
} | ||
protected override async addErrorMessageIfPythonArePossiblyOverridingPythonModules( | ||
messages: string[], | ||
resource: Resource | ||
) { | ||
// Looks like some other module is missing. | ||
// Sometimes when you create files like xml.py, then kernel startup fails due to xml.dom module not being found. | ||
const problematicFiles = await this.getFilesInWorkingDirectoryThatCouldPotentiallyOverridePythonModules( | ||
resource | ||
); | ||
if (problematicFiles.length > 0) { | ||
const cwd = resource ? path.dirname(resource) : undefined; | ||
const fileLinks = problematicFiles.map((item) => { | ||
if (item.type === 'file') { | ||
const displayPath = resource ? getDisplayPath(item.uri, [], cwd) : path.basename(item.uri); | ||
return `<a href='${item.uri.toString()}?line=1'>${displayPath}</a>`; | ||
} else { | ||
const displayPath = resource | ||
? getDisplayPath(item.uri, [], cwd) | ||
: `${path.basename(path.dirname(item.uri))}/__init__.py`; | ||
return `<a href='${item.uri.toString()}?line=1'>${displayPath}</a>`; | ||
} | ||
}); | ||
let files = ''; | ||
if (fileLinks.length === 1) { | ||
files = fileLinks[0]; | ||
} else { | ||
files = `${fileLinks.slice(0, -1).join(', ')} ${Common.and()} ${fileLinks.slice(-1)}`; | ||
} | ||
messages.push( | ||
DataScience.filesPossiblyOverridingPythonModulesMayHavePreventedKernelFromStarting().format(files) | ||
); | ||
messages.push(DataScience.listOfFilesWithLinksThatMightNeedToBeRenamed().format(files)); | ||
messages.push(Common.clickHereForMoreInfoWithHtml().format(JupyterKernelStartFailureOverrideReservedName)); | ||
} | ||
} | ||
protected override async getFilesInWorkingDirectoryThatCouldPotentiallyOverridePythonModules( | ||
resource: Resource | ||
): Promise<{ uri: Uri; type: 'file' | '__init__' }[]> { | ||
return resource ? this.reservedPythonNames.getUriOverridingReservedPythonNames(path.dirname(resource)) : []; | ||
} | ||
} |
Oops, something went wrong.