Skip to content

Commit c1d0fae

Browse files
author
Aziz Fikri
authored
Set storage drive before changing directory on windows (#17875)
1 parent 5859ddd commit c1d0fae

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

news/2 Fixes/14730.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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))

src/client/terminals/codeExecution/terminalCodeExecution.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
9292
}
9393
const fileDirPath = path.dirname(file.fsPath);
9494
if (fileDirPath.length > 0) {
95+
if (this.platformService.isWindows && /[a-z]\:/i.test(fileDirPath)) {
96+
const currentDrive =
97+
typeof this.workspace.rootPath === 'string'
98+
? this.workspace.rootPath.replace(/\:.*/g, '')
99+
: undefined;
100+
const fileDrive = fileDirPath.replace(/\:.*/g, '');
101+
if (fileDrive !== currentDrive) {
102+
await this.getTerminalService(file).sendText(`${fileDrive}:`);
103+
}
104+
}
95105
await this.getTerminalService(file).sendText(`cd ${fileDirPath.fileToCommandArgument()}`);
96106
}
97107
}

src/test/terminals/codeExecution/terminalCodeExec.unit.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,40 @@ suite('Terminal - Code Execution', () => {
163163
.returns(() => terminalService.object);
164164
});
165165

166+
async function ensureWeSetCurrentDriveBeforeChangingDirectory(_isWindows: boolean): Promise<void> {
167+
const file = Uri.file(path.join('d:', 'path', 'to', 'file', 'one.py'));
168+
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);
169+
workspace.setup((w) => w.rootPath).returns(() => path.join('c:', 'path', 'to'));
170+
workspaceFolder.setup((w) => w.uri).returns(() => Uri.file(path.join('c:', 'path', 'to')));
171+
platform.setup((p) => p.isWindows).returns(() => true);
172+
settings.setup((s) => s.pythonPath).returns(() => PYTHON_PATH);
173+
terminalSettings.setup((t) => t.launchArgs).returns(() => []);
174+
175+
await executor.executeFile(file);
176+
terminalService.verify(async (t) => t.sendText(TypeMoq.It.isValue('d:')), TypeMoq.Times.once());
177+
}
178+
test('Ensure we set current drive before changing directory on windows', async () => {
179+
await ensureWeSetCurrentDriveBeforeChangingDirectory(true);
180+
});
181+
182+
async function ensureWeDoNotChangeDriveIfDriveLetterSameAsFileDriveLetter(
183+
_isWindows: boolean,
184+
): Promise<void> {
185+
const file = Uri.file(path.join('c:', 'path', 'to', 'file', 'one.py'));
186+
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);
187+
workspace.setup((w) => w.rootPath).returns(() => path.join('c:', 'path', 'to'));
188+
workspaceFolder.setup((w) => w.uri).returns(() => Uri.file(path.join('c:', 'path', 'to')));
189+
platform.setup((p) => p.isWindows).returns(() => true);
190+
settings.setup((s) => s.pythonPath).returns(() => PYTHON_PATH);
191+
terminalSettings.setup((t) => t.launchArgs).returns(() => []);
192+
193+
await executor.executeFile(file);
194+
terminalService.verify(async (t) => t.sendText(TypeMoq.It.isValue('c:')), TypeMoq.Times.never());
195+
}
196+
test('Ensure we do not change drive if current drive letter is same as the file drive letter on windows', async () => {
197+
await ensureWeDoNotChangeDriveIfDriveLetterSameAsFileDriveLetter(true);
198+
});
199+
166200
async function ensureWeSetCurrentDirectoryBeforeExecutingAFile(_isWindows: boolean): Promise<void> {
167201
const file = Uri.file(path.join('c', 'path', 'to', 'file', 'one.py'));
168202
terminalSettings.setup((t) => t.executeInFileDir).returns(() => true);

0 commit comments

Comments
 (0)