|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | 2 | // Licensed under the MIT License.
|
3 | 3 | 'use strict';
|
| 4 | +import '../../common/extensions'; |
| 5 | + |
4 | 6 | import { nbformat } from '@jupyterlab/coreutils';
|
5 | 7 | import * as fs from 'fs-extra';
|
6 | 8 | import { inject, injectable } from 'inversify';
|
7 | 9 | import * as os from 'os';
|
8 | 10 | import * as path from 'path';
|
9 |
| -import '../../common/extensions'; |
10 | 11 |
|
11 | 12 | import { IWorkspaceService } from '../../common/application/types';
|
| 13 | +import { traceError } from '../../common/logger'; |
12 | 14 | import { IFileSystem, IPlatformService } from '../../common/platform/types';
|
13 | 15 | import { IConfigurationService, IDisposableRegistry } from '../../common/types';
|
14 | 16 | import * as localize from '../../common/utils/localize';
|
@@ -138,35 +140,39 @@ export class JupyterImporter implements INotebookImporter {
|
138 | 140 | // When importing a file, calculate if we can create a %cd so that the relative paths work
|
139 | 141 | private async calculateDirectoryChange(notebookFile: string): Promise<string | undefined> {
|
140 | 142 | let directoryChange: string | undefined;
|
141 |
| - // Make sure we don't already have an import/export comment in the file |
142 |
| - const contents = await this.fileSystem.readFile(notebookFile); |
143 |
| - const haveChangeAlready = contents.includes(CodeSnippits.ChangeDirectoryCommentIdentifier); |
144 |
| - |
145 |
| - if (!haveChangeAlready) { |
146 |
| - const notebookFilePath = path.dirname(notebookFile); |
147 |
| - // First see if we have a workspace open, this only works if we have a workspace root to be relative to |
148 |
| - if (this.workspaceService.hasWorkspaceFolders) { |
149 |
| - const workspacePath = this.workspaceService.workspaceFolders![0].uri.fsPath; |
150 |
| - |
151 |
| - // Make sure that we have everything that we need here |
152 |
| - if (workspacePath && path.isAbsolute(workspacePath) && notebookFilePath && path.isAbsolute(notebookFilePath)) { |
153 |
| - directoryChange = path.relative(workspacePath, notebookFilePath); |
| 143 | + try { |
| 144 | + // Make sure we don't already have an import/export comment in the file |
| 145 | + const contents = await this.fileSystem.readFile(notebookFile); |
| 146 | + const haveChangeAlready = contents.includes(CodeSnippits.ChangeDirectoryCommentIdentifier); |
| 147 | + |
| 148 | + if (!haveChangeAlready) { |
| 149 | + const notebookFilePath = path.dirname(notebookFile); |
| 150 | + // First see if we have a workspace open, this only works if we have a workspace root to be relative to |
| 151 | + if (this.workspaceService.hasWorkspaceFolders) { |
| 152 | + const workspacePath = this.workspaceService.workspaceFolders![0].uri.fsPath; |
| 153 | + |
| 154 | + // Make sure that we have everything that we need here |
| 155 | + if (workspacePath && path.isAbsolute(workspacePath) && notebookFilePath && path.isAbsolute(notebookFilePath)) { |
| 156 | + directoryChange = path.relative(workspacePath, notebookFilePath); |
| 157 | + } |
154 | 158 | }
|
155 | 159 | }
|
156 |
| - } |
157 | 160 |
|
158 |
| - // If path.relative can't calculate a relative path, then it just returns the full second path |
159 |
| - // so check here, we only want this if we were able to calculate a relative path, no network shares or drives |
160 |
| - if (directoryChange && !path.isAbsolute(directoryChange)) { |
| 161 | + // If path.relative can't calculate a relative path, then it just returns the full second path |
| 162 | + // so check here, we only want this if we were able to calculate a relative path, no network shares or drives |
| 163 | + if (directoryChange && !path.isAbsolute(directoryChange)) { |
161 | 164 |
|
162 |
| - // Escape windows path chars so they end up in the source escaped |
163 |
| - if (this.platform.isWindows) { |
164 |
| - directoryChange = directoryChange.replace('\\', '\\\\'); |
165 |
| - } |
| 165 | + // Escape windows path chars so they end up in the source escaped |
| 166 | + if (this.platform.isWindows) { |
| 167 | + directoryChange = directoryChange.replace('\\', '\\\\'); |
| 168 | + } |
166 | 169 |
|
167 |
| - return directoryChange; |
168 |
| - } else { |
169 |
| - return undefined; |
| 170 | + return directoryChange; |
| 171 | + } else { |
| 172 | + return undefined; |
| 173 | + } |
| 174 | + } catch (e) { |
| 175 | + traceError(e); |
170 | 176 | }
|
171 | 177 | }
|
172 | 178 |
|
|
0 commit comments