Skip to content

Commit 69569c3

Browse files
committed
Clean up for old stale env var
1 parent 7532117 commit 69569c3

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/features/terminal/shells/bash/bashStartup.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ShellConstants } from '../../../common/shellConstants';
77
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
88
import { shellIntegrationForActiveTerminal } from '../common/shellUtils';
99
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
10-
import { BASH_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY } from './bashConstants';
10+
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY, ZSH_OLD_ENV_KEY } from './bashConstants';
1111

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

172172
try {
173173
const bashProfile = await getBashProfiles();
174+
// Remove old environment variable if it exists
175+
await removeStartup(bashProfile, BASH_OLD_ENV_KEY);
174176
const result = await removeStartup(bashProfile, BASH_ENV_KEY);
175177
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
176178
} catch (err) {
@@ -233,6 +235,7 @@ export class ZshStartupProvider implements ShellStartupScriptProvider {
233235
}
234236
try {
235237
const zshProfiles = await getZshProfiles();
238+
await removeStartup(zshProfiles, ZSH_OLD_ENV_KEY);
236239
const result = await removeStartup(zshProfiles, ZSH_ENV_KEY);
237240
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
238241
} catch (err) {
@@ -293,6 +296,7 @@ export class GitBashStartupProvider implements ShellStartupScriptProvider {
293296

294297
try {
295298
const bashProfiles = await getBashProfiles();
299+
await removeStartup(bashProfiles, BASH_OLD_ENV_KEY);
296300
const result = await removeStartup(bashProfiles, BASH_ENV_KEY);
297301
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
298302
} catch (err) {

src/features/terminal/shells/fish/fishStartup.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ShellConstants } from '../../../common/shellConstants';
88
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
99
import { shellIntegrationForActiveTerminal } from '../common/shellUtils';
1010
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
11-
import { FISH_ENV_KEY, FISH_SCRIPT_VERSION } from './fishConstants';
11+
import { FISH_ENV_KEY, FISH_OLD_ENV_KEY, FISH_SCRIPT_VERSION } from './fishConstants';
1212

1313
async function isFishInstalled(): Promise<boolean> {
1414
try {
@@ -92,11 +92,11 @@ async function removeFishStartup(profilePath: string, key: string): Promise<bool
9292
const content = await fs.readFile(profilePath, 'utf8');
9393
if (hasStartupCode(content, regionStart, regionEnd, [key])) {
9494
await fs.writeFile(profilePath, removeStartupCode(content, regionStart, regionEnd));
95-
traceInfo(`Removed activation from fish profile at: ${profilePath}`);
95+
traceInfo(`Removed activation from fish profile at: ${profilePath}, for key: ${key}`);
9696
}
9797
return true;
9898
} catch (err) {
99-
traceVerbose(`Failed to remove fish startup`, err);
99+
traceVerbose(`Failed to remove fish startup, for key: ${key}`, err);
100100
return false;
101101
}
102102
}
@@ -148,6 +148,8 @@ export class FishStartupProvider implements ShellStartupScriptProvider {
148148

149149
try {
150150
const fishProfile = await getFishProfile();
151+
// Remove old environment variable if it exists
152+
await removeFishStartup(fishProfile, FISH_OLD_ENV_KEY);
151153
const success = await removeFishStartup(fishProfile, FISH_ENV_KEY);
152154
return success ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
153155
} catch (err) {

src/features/terminal/shells/pwsh/pwshStartup.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
PROFILE_TAG_START,
1818
shellIntegrationForActiveTerminal,
1919
} from '../common/shellUtils';
20-
import { POWERSHELL_ENV_KEY, PWSH_SCRIPT_VERSION } from './pwshConstants';
20+
import { POWERSHELL_ENV_KEY, POWERSHELL_OLD_ENV_KEY, PWSH_SCRIPT_VERSION } from './pwshConstants';
2121

2222
const PWSH_PROFILE_PATH_CACHE_KEY = 'PWSH_PROFILE_PATH_CACHE';
2323
const PS5_PROFILE_PATH_CACHE_KEY = 'PS5_PROFILE_PATH_CACHE';
@@ -171,22 +171,22 @@ async function setupPowerShellStartup(shell: string, profile: string): Promise<b
171171
}
172172
}
173173

174-
async function removePowerShellStartup(shell: string, profile: string): Promise<boolean> {
174+
async function removePowerShellStartup(shell: string, profile: string, key: string): Promise<boolean> {
175175
if (!(await fs.pathExists(profile))) {
176176
return true;
177177
}
178178

179179
try {
180180
const content = await fs.readFile(profile, 'utf8');
181-
if (hasStartupCode(content, regionStart, regionEnd, [POWERSHELL_ENV_KEY])) {
181+
if (hasStartupCode(content, regionStart, regionEnd, [key])) {
182182
await fs.writeFile(profile, removeStartupCode(content, regionStart, regionEnd));
183-
traceInfo(`SHELL: Removed activation from ${shell} profile at: ${profile}`);
183+
traceInfo(`SHELL: Removed activation from ${shell} profile at: ${profile}, for key: ${key}`);
184184
} else {
185-
traceInfo(`SHELL: No activation code found in ${shell} profile at: ${profile}`);
185+
traceInfo(`SHELL: No activation code found in ${shell} profile at: ${profile}, for key: ${key}`);
186186
}
187187
return true;
188188
} catch (err) {
189-
traceError(`SHELL: Failed to remove startup code for ${shell} profile at: ${profile}`, err);
189+
traceError(`SHELL: Failed to remove startup code for ${shell} profile at: ${profile}, for key: ${key}`, err);
190190
return false;
191191
}
192192
}
@@ -301,7 +301,9 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
301301

302302
try {
303303
const profile = await getProfileForShell(shell);
304-
const success = await removePowerShellStartup(shell, profile);
304+
// Remove old environment variable if it exists
305+
await removePowerShellStartup(shell, profile, POWERSHELL_OLD_ENV_KEY);
306+
const success = await removePowerShellStartup(shell, profile, POWERSHELL_ENV_KEY);
305307
anyEdited.push(success ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited);
306308
} catch (err) {
307309
traceError(`Failed to remove ${shell} startup`, err);

0 commit comments

Comments
 (0)