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
2 changes: 2 additions & 0 deletions src/features/terminal/shells/bash/bashConstants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const BASH_ENV_KEY = 'VSCODE_PYTHON_BASH_ACTIVATE';
export const ZSH_ENV_KEY = 'VSCODE_PYTHON_ZSH_ACTIVATE';
export const BASH_OLD_ENV_KEY = 'VSCODE_BASH_ACTIVATE';
export const ZSH_OLD_ENV_KEY = 'VSCODE_ZSH_ACTIVATE';
export const BASH_SCRIPT_VERSION = '0.1.1';
12 changes: 8 additions & 4 deletions src/features/terminal/shells/bash/bashStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ShellConstants } from '../../../common/shellConstants';
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
import { shellIntegrationForActiveTerminal } from '../common/shellUtils';
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
import { BASH_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY } from './bashConstants';
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY, ZSH_OLD_ENV_KEY } from './bashConstants';

async function isBashLikeInstalled(): Promise<boolean> {
const result = await Promise.all([which('bash', { nothrow: true }), which('sh', { nothrow: true })]);
Expand Down Expand Up @@ -106,13 +106,13 @@ async function removeStartup(profile: string, key: string): Promise<boolean> {
const content = await fs.readFile(profile, 'utf8');
if (hasStartupCode(content, regionStart, regionEnd, [key])) {
await fs.writeFile(profile, removeStartupCode(content, regionStart, regionEnd));
traceInfo(`SHELL: Removed activation from profile at: ${profile}`);
traceInfo(`SHELL: Removed activation from profile at: ${profile}, for key: ${key}`);
} else {
traceVerbose(`Profile at ${profile} does not contain activation code`);
traceVerbose(`Profile at ${profile} does not contain activation code, for key: ${key}`);
}
return true;
} catch (err) {
traceVerbose(`Failed to remove ${profile} startup`, err);
traceVerbose(`Failed to remove ${profile} startup, for key: ${key}`, err);
return false;
}
}
Expand Down Expand Up @@ -171,6 +171,8 @@ export class BashStartupProvider implements ShellStartupScriptProvider {

try {
const bashProfile = await getBashProfiles();
// Remove old environment variable if it exists
await removeStartup(bashProfile, BASH_OLD_ENV_KEY);
const result = await removeStartup(bashProfile, BASH_ENV_KEY);
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
Expand Down Expand Up @@ -233,6 +235,7 @@ export class ZshStartupProvider implements ShellStartupScriptProvider {
}
try {
const zshProfiles = await getZshProfiles();
await removeStartup(zshProfiles, ZSH_OLD_ENV_KEY);
const result = await removeStartup(zshProfiles, ZSH_ENV_KEY);
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
Expand Down Expand Up @@ -293,6 +296,7 @@ export class GitBashStartupProvider implements ShellStartupScriptProvider {

try {
const bashProfiles = await getBashProfiles();
await removeStartup(bashProfiles, BASH_OLD_ENV_KEY);
const result = await removeStartup(bashProfiles, BASH_ENV_KEY);
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/features/terminal/shells/fish/fishConstants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const FISH_ENV_KEY = 'VSCODE_FISH_ACTIVATE';
export const FISH_ENV_KEY = 'VSCODE_PYTHON_FISH_ACTIVATE';
export const FISH_OLD_ENV_KEY = 'VSCODE_FISH_ACTIVATE';
export const FISH_SCRIPT_VERSION = '0.1.1';
8 changes: 5 additions & 3 deletions src/features/terminal/shells/fish/fishStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ShellConstants } from '../../../common/shellConstants';
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
import { shellIntegrationForActiveTerminal } from '../common/shellUtils';
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
import { FISH_ENV_KEY, FISH_SCRIPT_VERSION } from './fishConstants';
import { FISH_ENV_KEY, FISH_OLD_ENV_KEY, FISH_SCRIPT_VERSION } from './fishConstants';

async function isFishInstalled(): Promise<boolean> {
try {
Expand Down Expand Up @@ -93,11 +93,11 @@ async function removeFishStartup(profilePath: string, key: string): Promise<bool
const content = await fs.readFile(profilePath, 'utf8');
if (hasStartupCode(content, regionStart, regionEnd, [key])) {
await fs.writeFile(profilePath, removeStartupCode(content, regionStart, regionEnd));
traceInfo(`Removed activation from fish profile at: ${profilePath}`);
traceInfo(`Removed activation from fish profile at: ${profilePath}, for key: ${key}`);
}
return true;
} catch (err) {
traceVerbose(`Failed to remove fish startup`, err);
traceVerbose(`Failed to remove fish startup, for key: ${key}`, err);
return false;
}
}
Expand Down Expand Up @@ -149,6 +149,8 @@ export class FishStartupProvider implements ShellStartupScriptProvider {

try {
const fishProfile = await getFishProfile();
// Remove old environment variable if it exists
await removeFishStartup(fishProfile, FISH_OLD_ENV_KEY);
const success = await removeFishStartup(fishProfile, FISH_ENV_KEY);
return success ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/features/terminal/shells/pwsh/pwshConstants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const POWERSHELL_ENV_KEY = 'VSCODE_PYTHON_PWSH_ACTIVATE';
export const POWERSHELL_OLD_ENV_KEY = 'VSCODE_PWSH_ACTIVATE';
export const PWSH_SCRIPT_VERSION = '0.1.1';
19 changes: 11 additions & 8 deletions src/features/terminal/shells/pwsh/pwshStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
PROFILE_TAG_START,
shellIntegrationForActiveTerminal,
} from '../common/shellUtils';
import { POWERSHELL_ENV_KEY, PWSH_SCRIPT_VERSION } from './pwshConstants';
import { POWERSHELL_ENV_KEY, POWERSHELL_OLD_ENV_KEY, PWSH_SCRIPT_VERSION } from './pwshConstants';

const PWSH_PROFILE_PATH_CACHE_KEY = 'PWSH_PROFILE_PATH_CACHE';
const PS5_PROFILE_PATH_CACHE_KEY = 'PS5_PROFILE_PATH_CACHE';
Expand Down Expand Up @@ -146,7 +146,8 @@ async function isPowerShellStartupSetup(shell: string, profile: string): Promise

async function setupPowerShellStartup(shell: string, profile: string): Promise<boolean> {
if (shellIntegrationForActiveTerminal(shell, profile)) {
removePowerShellStartup(shell, profile);
removePowerShellStartup(shell, profile, POWERSHELL_OLD_ENV_KEY);
removePowerShellStartup(shell, profile, POWERSHELL_ENV_KEY);
return true;
}
const activationContent = getActivationContent();
Expand All @@ -172,22 +173,22 @@ async function setupPowerShellStartup(shell: string, profile: string): Promise<b
}
}

async function removePowerShellStartup(shell: string, profile: string): Promise<boolean> {
async function removePowerShellStartup(shell: string, profile: string, key: string): Promise<boolean> {
if (!(await fs.pathExists(profile))) {
return true;
}

try {
const content = await fs.readFile(profile, 'utf8');
if (hasStartupCode(content, regionStart, regionEnd, [POWERSHELL_ENV_KEY])) {
if (hasStartupCode(content, regionStart, regionEnd, [key])) {
await fs.writeFile(profile, removeStartupCode(content, regionStart, regionEnd));
traceInfo(`SHELL: Removed activation from ${shell} profile at: ${profile}`);
traceInfo(`SHELL: Removed activation from ${shell} profile at: ${profile}, for key: ${key}`);
} else {
traceInfo(`SHELL: No activation code found in ${shell} profile at: ${profile}`);
traceInfo(`SHELL: No activation code found in ${shell} profile at: ${profile}, for key: ${key}`);
}
return true;
} catch (err) {
traceError(`SHELL: Failed to remove startup code for ${shell} profile at: ${profile}`, err);
traceError(`SHELL: Failed to remove startup code for ${shell} profile at: ${profile}, for key: ${key}`, err);
return false;
}
}
Expand Down Expand Up @@ -302,7 +303,9 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {

try {
const profile = await getProfileForShell(shell);
const success = await removePowerShellStartup(shell, profile);
// Remove old environment variable if it exists
await removePowerShellStartup(shell, profile, POWERSHELL_OLD_ENV_KEY);
const success = await removePowerShellStartup(shell, profile, POWERSHELL_ENV_KEY);
anyEdited.push(success ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited);
} catch (err) {
traceError(`Failed to remove ${shell} startup`, err);
Expand Down
Loading