* When we override the built in modules errors are of the form * `ImportError: cannot import name 'Random' from 'random' (/home/don/samples/pySamples/sample/kernel_crash/no_start/random.py)`
(
moduleName: string,
fileName: string | undefined,
workspaceFolders: readonly WorkspaceFolder[],
pythonSysPrefix?: string
)
| 623 | * `ImportError: cannot import name 'Random' from 'random' (/home/don/samples/pySamples/sample/kernel_crash/no_start/random.py)` |
| 624 | */ |
| 625 | function isBuiltInModuleOverwritten( |
| 626 | moduleName: string, |
| 627 | fileName: string | undefined, |
| 628 | workspaceFolders: readonly WorkspaceFolder[], |
| 629 | pythonSysPrefix?: string |
| 630 | ): KernelFailure | undefined { |
| 631 | // If we cannot tell whether this belongs to python environment, |
| 632 | // then we cannot tell if it overwrites any built in modules. |
| 633 | if (!pythonSysPrefix) { |
| 634 | return; |
| 635 | } |
| 636 | // If we cannot tell whether this is part of a worksapce folder, |
| 637 | // then we cannot tell if the user created this. |
| 638 | if (workspaceFolders.length === 0) { |
| 639 | return; |
| 640 | } |
| 641 | if (!fileName) { |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | if (fileName.toLowerCase().startsWith(pythonSysPrefix)) { |
| 646 | // This is a python file, not created by the user. |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | // eslint-disable-next-line local-rules/dont-use-fspath |
| 651 | if (!workspaceFolders.some((folder) => fileName.toLowerCase().startsWith(folder.uri.fsPath.toLowerCase()))) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | return { |
| 656 | reason: KernelFailureReason.overridingBuiltinModules, |
| 657 | fileName, |
| 658 | moduleName, |
| 659 | message: DataScience.fileSeemsToBeInterferingWithKernelStartup( |
| 660 | getDisplayPath(Uri.file(fileName), workspaceFolders || []) |
| 661 | ), |
| 662 | moreInfoLink: 'https://aka.ms/kernelFailuresOverridingBuiltInModules', |
| 663 | telemetrySafeTags: ['import.error', 'override.modules'] |
| 664 | }; |
| 665 | } |
| 666 | |
| 667 | export function createOutputWithErrorMessageForDisplay(errorMessage: string) { |
| 668 | if (!errorMessage) { |
no test coverage detected