Skip to content
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
1 change: 1 addition & 0 deletions news/2 Fixes/14730.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change drive first before changing directory in windows, to anticipate running file outside working directory with different storage drive. (thanks [afikrim](https://github.com/afikrim))
10 changes: 10 additions & 0 deletions src/client/terminals/codeExecution/terminalCodeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
}
const fileDirPath = path.dirname(file.fsPath);
if (fileDirPath.length > 0) {
if (this.platformService.isWindows && /[a-z]\:/i.test(fileDirPath)) {
const currentDrive =
typeof this.workspace.rootPath === 'string'
? this.workspace.rootPath.replace(/\:.*/g, '')
: undefined;
const fileDrive = fileDirPath.replace(/\:.*/g, '');
if (fileDrive !== currentDrive) {
await this.getTerminalService(file).sendText(`${fileDrive}:`);
}
}
await this.getTerminalService(file).sendText(`cd ${fileDirPath.fileToCommandArgument()}`);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/terminals/codeExecution/terminalCodeExec.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ suite('Terminal - Code Execution', () => {
.returns(() => terminalService.object);
});

async function ensureWeSetCurrentDriveBeforeChangingDirectory(_isWindows: boolean): Promise<void> {
const file = Uri.file(path.join('d:', 'path', 'to', 'file', 'one.py'));
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);
workspace.setup((w) => w.rootPath).returns(() => path.join('c:', 'path', 'to'));
workspaceFolder.setup((w) => w.uri).returns(() => Uri.file(path.join('c:', 'path', 'to')));
platform.setup((p) => p.isWindows).returns(() => true);
settings.setup((s) => s.pythonPath).returns(() => PYTHON_PATH);
terminalSettings.setup((t) => t.launchArgs).returns(() => []);

await executor.executeFile(file);
terminalService.verify(async (t) => t.sendText(TypeMoq.It.isValue('d:')), TypeMoq.Times.once());
}
test('Ensure we set current drive before changing directory on windows', async () => {
await ensureWeSetCurrentDriveBeforeChangingDirectory(true);
});

async function ensureWeDoNotChangeDriveIfDriveLetterSameAsFileDriveLetter(
_isWindows: boolean,
): Promise<void> {
const file = Uri.file(path.join('c:', 'path', 'to', 'file', 'one.py'));
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);
workspace.setup((w) => w.rootPath).returns(() => path.join('c:', 'path', 'to'));
workspaceFolder.setup((w) => w.uri).returns(() => Uri.file(path.join('c:', 'path', 'to')));
platform.setup((p) => p.isWindows).returns(() => true);
settings.setup((s) => s.pythonPath).returns(() => PYTHON_PATH);
terminalSettings.setup((t) => t.launchArgs).returns(() => []);

await executor.executeFile(file);
terminalService.verify(async (t) => t.sendText(TypeMoq.It.isValue('c:')), TypeMoq.Times.never());
}
test('Ensure we do not change drive if current drive letter is same as the file drive letter on windows', async () => {
await ensureWeDoNotChangeDriveIfDriveLetterSameAsFileDriveLetter(true);
});

async function ensureWeSetCurrentDirectoryBeforeExecutingAFile(_isWindows: boolean): Promise<void> {
const file = Uri.file(path.join('c', 'path', 'to', 'file', 'one.py'));
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);
Expand Down