Skip to content

Commit be34874

Browse files
committed
improve terminal env activate on Linux
1 parent a21398d commit be34874

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

src/main/env.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,10 @@ export async function runCommandInEnvironment(
171171
const isWin = process.platform === 'win32';
172172
const baseCondaPath = getCondaPath();
173173
const condaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
174-
const commandScript = createCommandScriptInEnv(
175-
envPath,
176-
condaEnvPath,
174+
const commandScript = createCommandScriptInEnv(envPath, condaEnvPath, {
177175
command,
178-
' && '
179-
);
176+
joinStr: ' && '
177+
});
180178

181179
// TODO: implement timeout. in case there is network issues
182180

src/main/pythonenvdialog/pythonenvdialog.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,15 @@ export class ManagePythonEnvironmentDialog {
131131
const condaEnvPath = condaEnvPathForCondaExePath(condaPath);
132132
const activateCommand = createCommandScriptInEnv(
133133
envPath,
134-
condaEnvPath
134+
condaEnvPath,
135+
{ quoteChar: "'" }
135136
);
136137

137-
launchTerminalInDirectory(envPath, activateCommand);
138+
launchTerminalInDirectory({
139+
dirPath: envPath,
140+
commands: activateCommand,
141+
interactive: true
142+
});
138143
}
139144
},
140145
{
@@ -162,10 +167,18 @@ export class ManagePythonEnvironmentDialog {
162167
let launchCommand = createCommandScriptInEnv(
163168
envPath,
164169
condaEnvPath,
165-
`jupyter lab --notebook-dir="${workingDir}"`
170+
{
171+
command: `jupyter lab --notebook-dir='${workingDir}'`,
172+
quoteChar: "'",
173+
joinStr: ' && '
174+
}
166175
);
167176

168-
launchTerminalInDirectory(envPath, launchCommand);
177+
launchTerminalInDirectory({
178+
dirPath: envPath,
179+
commands: launchCommand,
180+
interactive: false
181+
});
169182
}
170183
},
171184
{

src/main/utils.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,11 @@ export function isBaseCondaEnv(envPath: string): boolean {
499499
export function createCommandScriptInEnv(
500500
envPath: string,
501501
baseCondaEnvPath: string,
502-
command?: string,
503-
joinStr?: string
502+
options?: {
503+
command?: string;
504+
quoteChar?: string;
505+
joinStr?: string;
506+
}
504507
): string {
505508
try {
506509
const stat = fs.lstatSync(envPath);
@@ -511,9 +514,9 @@ export function createCommandScriptInEnv(
511514
//
512515
}
513516

514-
if (joinStr === undefined) {
515-
joinStr = '\n';
516-
}
517+
const quoteChar = options?.quoteChar || '"';
518+
const joinStr = options?.joinStr || '\n';
519+
let command = options?.command;
517520
const isWin = process.platform === 'win32';
518521

519522
let activatePath = activatePathForEnvPath(envPath);
@@ -555,11 +558,11 @@ export function createCommandScriptInEnv(
555558
scriptLines.push(`CALL ${command}`);
556559
}
557560
} else {
558-
scriptLines.push(`source "${activatePath}"`);
561+
scriptLines.push(`source ${quoteChar}${activatePath}${quoteChar}`);
559562
if (isConda && isBaseCondaActivate) {
560-
scriptLines.push(`source "${condaSourcePath}"`);
563+
scriptLines.push(`source ${quoteChar}${condaSourcePath}${quoteChar}`);
561564
if (!isCondaCommand) {
562-
scriptLines.push(`conda activate "${envPath}"`);
565+
scriptLines.push(`conda activate ${quoteChar}${envPath}${quoteChar}`);
563566
}
564567
}
565568
if (command) {
@@ -668,15 +671,19 @@ export function openDirectoryInExplorer(dirPath: string): boolean {
668671
return true;
669672
}
670673

671-
export function launchTerminalInDirectory(
672-
dirPath: string,
673-
commands?: string
674-
): boolean {
674+
export function launchTerminalInDirectory(options: {
675+
dirPath: string;
676+
interactive: boolean;
677+
commands?: string;
678+
}): boolean {
679+
const { dirPath, interactive } = options;
675680
if (!(fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory())) {
676681
return false;
677682
}
678683

679684
const { platform } = process;
685+
let commands = options.commands;
686+
680687
if (platform === 'darwin') {
681688
let callCommands = '';
682689
if (commands) {
@@ -711,7 +718,11 @@ export function launchTerminalInDirectory(
711718
} else {
712719
let callCommands = '';
713720
if (commands) {
714-
callCommands = ` -- bash -c "${commands}; exec bash"`;
721+
// note that calling "exec bash" at the end will cause .bashrc to be reloaded,
722+
// which could possibly override python path (e.g. base conda initialization)
723+
callCommands = ` -- bash -c "${commands}${
724+
interactive ? '; exec bash' : ''
725+
}"`;
715726
}
716727
exec(`gnome-terminal --working-directory="${dirPath}"${callCommands}`);
717728
}

0 commit comments

Comments
 (0)