diff --git a/src/extension.ts b/src/extension.ts index 5dfa3db4..b00ebb1c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -89,7 +89,7 @@ import { cleanupStartupScripts } from './features/terminal/shellStartupSetupHand import { TerminalActivationImpl } from './features/terminal/terminalActivationState'; import { TerminalEnvVarInjector } from './features/terminal/terminalEnvVarInjector'; import { TerminalManager, TerminalManagerImpl } from './features/terminal/terminalManager'; -import { getEnvironmentForTerminal } from './features/terminal/utils'; +import { getEnvironmentForTerminal, migrateLegacyTerminalActivationSetting } from './features/terminal/utils'; import { openSearchSettings } from './features/views/envManagerSearch'; import { EnvManagerView } from './features/views/envManagersView'; import { ProjectView } from './features/views/projectView'; @@ -184,6 +184,12 @@ export async function activate(context: ExtensionContext): Promise('terminal.activateEnvironment', undefined); if (pythonActivateSetting === false) { - // Set autoActivationType to 'off' if python.terminal.activateEnvironment is false - pyEnvsConfig.update('terminal.autoActivationType', ACT_TYPE_OFF); return ACT_TYPE_OFF; } @@ -429,6 +428,33 @@ export function getAutoActivationType(): AutoActivationType { return ACT_TYPE_COMMAND; } +/** + * Migrates the disabled legacy terminal activation setting to User settings. + * + * The migration is skipped when `python-envs.terminal.autoActivationType` is already set. + * The returned promise rejects if the setting cannot be updated. + */ +export async function migrateLegacyTerminalActivationSetting(): Promise { + const pyEnvsConfig = getConfiguration('python-envs'); + const pyEnvsActivationType = pyEnvsConfig.inspect('terminal.autoActivationType'); + + if (pyEnvsActivationType) { + const activationType = pyEnvsActivationType as Record; + if ( + ('globalRemoteValue' in pyEnvsActivationType && activationType.globalRemoteValue !== undefined) || + ('globalLocalValue' in pyEnvsActivationType && activationType.globalLocalValue !== undefined) || + pyEnvsActivationType.globalValue !== undefined + ) { + return; + } + } + + const pythonConfig = getConfiguration('python'); + if (pythonConfig.get('terminal.activateEnvironment', undefined) === false) { + await setAutoActivationType(ACT_TYPE_OFF); + } +} + export async function setAutoActivationType(value: AutoActivationType): Promise { const config = getConfiguration('python-envs'); return await config.update('terminal.autoActivationType', value, true); diff --git a/src/test/features/terminal/utils.unit.test.ts b/src/test/features/terminal/utils.unit.test.ts index ff890a56..e47fdbbd 100644 --- a/src/test/features/terminal/utils.unit.test.ts +++ b/src/test/features/terminal/utils.unit.test.ts @@ -14,6 +14,7 @@ import { AutoActivationType, getAutoActivationType, getEnvironmentForTerminal, + migrateLegacyTerminalActivationSetting, shouldActivateInCurrentTerminal, shouldSkipTerminalActivation, waitForShellIntegration, @@ -484,19 +485,29 @@ suite('Terminal Utils - getAutoActivationType', () => { }); suite('Legacy Python Setting Fallback', () => { - test('should return ACT_TYPE_OFF and update config when python.terminal.activateEnvironment is false', () => { + test('should migrate python.terminal.activateEnvironment false to User settings only once', async () => { // Mock - no python-envs settings, python.terminal.activateEnvironment is false pyEnvsConfig.inspect.withArgs('terminal.autoActivationType').returns(undefined); pythonConfig.get.withArgs('terminal.activateEnvironment', undefined).returns(false); + pyEnvsConfig.update.rejects(new Error('Unable to update User settings')); // Run + await assert.rejects(migrateLegacyTerminalActivationSetting(), /Unable to update User settings/); const result = getAutoActivationType(); + const repeatedResult = getAutoActivationType(); // Assert assert.strictEqual(result, ACT_TYPE_OFF, 'Should return ACT_TYPE_OFF when legacy setting is false'); - assert.ok( - pyEnvsConfig.update.calledWithExactly('terminal.autoActivationType', ACT_TYPE_OFF), - 'Should update python-envs config to ACT_TYPE_OFF', + assert.strictEqual( + repeatedResult, + ACT_TYPE_OFF, + 'Should continue returning ACT_TYPE_OFF after a failed migration', + ); + sinon.assert.calledOnceWithExactly( + pyEnvsConfig.update, + 'terminal.autoActivationType', + ACT_TYPE_OFF, + true, ); }); @@ -572,7 +583,7 @@ suite('Terminal Utils - getAutoActivationType', () => { ); }); - test('should prioritize python-envs settings over legacy python settings', () => { + test('should prioritize python-envs settings over legacy python settings', async () => { // Mock - python-envs has globalValue, python has conflicting setting const mockInspectResult = { globalValue: ACT_TYPE_SHELL, @@ -581,6 +592,7 @@ suite('Terminal Utils - getAutoActivationType', () => { pythonConfig.get.withArgs('terminal.activateEnvironment', undefined).returns(false); // Run + await migrateLegacyTerminalActivationSetting(); const result = getAutoActivationType(); // Assert