From 79a4841ea6700cedcaa7849126c1acf0b5726e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20K=C3=BChl?= Date: Thu, 27 Oct 2022 20:11:24 +0200 Subject: [PATCH] use empty string to "unset" variables the memento[1] and environment variable collection[2] apis don't support "unsetting" variables, only deleting them from the collection, which leads to those variables being left along. this change uses the empty string instead. posix calls this value "null" for environment variables, and it's technically different from the variable being unset but the closest we can get. [1]: https://code.visualstudio.com/api/references/vscode-api#Memento [2]: https://code.visualstudio.com/api/references/vscode-api#EnvironmentVariableCollection --- CHANGELOG.md | 4 ++++ src/extension.ts | 12 ++++-------- src/test/runTest.ts | 3 ++- test/workspace/.envrc | 1 + test/workspace/.vscode/tasks.json | 4 ++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 874853f7..360bc210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## [Unreleased] ### Changed - Require VSCode 1.66 +- "Unset" variables by setting them to the empty string +### Fixed +- Avoid continuously asking to restart + If this keeps happening to you, please let us know! ## [0.6.1] - 2022-03-25 ### Fixed diff --git a/src/extension.ts b/src/extension.ts index efb058cb..1f1f99e9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -112,7 +112,7 @@ class Direnv implements vscode.Disposable { await this.cache.update( Cached.environment, Object.fromEntries( - [...this.backup.entries()].map(([key]) => [key, process.env[key]]), + [...this.backup.entries()].map(([key]) => [key, process.env[key] ?? '']), ), ) } @@ -124,13 +124,9 @@ class Direnv implements vscode.Disposable { this.backup.set(key, process.env[key]) } - if (value !== null) { - process.env[key] = value - this.environment.replace(key, value) - } else { - delete process.env[key] - this.environment.delete(key) // can't unset the variable - } + value ??= '' // can't unset, set to empty instead + process.env[key] = value + this.environment.replace(key, value) }) } diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 7c16ccbc..f2964e32 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -6,10 +6,11 @@ async function main() { try { const extensionDevelopmentPath = path.resolve(__dirname, '../../') const extensionTestsPath = path.resolve(__dirname, './suite/index') + const extensionTestsEnv = { ['PREDEFINED']: 'value' } const workspacePath = path.resolve(__dirname, '../../test/workspace') const disableExtensions = '--disable-extensions' const launchArgs = [workspacePath, disableExtensions] - await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs }) + await runTests({ extensionDevelopmentPath, extensionTestsPath, extensionTestsEnv, launchArgs }) } catch (err) { console.error('Failed to run tests') process.exit(1) diff --git a/test/workspace/.envrc b/test/workspace/.envrc index bc3c74d5..d81472e2 100644 --- a/test/workspace/.envrc +++ b/test/workspace/.envrc @@ -1,2 +1,3 @@ # shellcheck shell=bash export VARIABLE=value +unset PREDEFINED diff --git a/test/workspace/.vscode/tasks.json b/test/workspace/.vscode/tasks.json index 4194c5d6..9863ebde 100644 --- a/test/workspace/.vscode/tasks.json +++ b/test/workspace/.vscode/tasks.json @@ -7,14 +7,14 @@ "label": "test-task", "detail": "test that the .envrc environment is available in the task shell", "type": "shell", - "command": "test -n \"$VARIABLE\"", + "command": "test -n \"$VARIABLE\" && test -z \"$PREDEFINED\"", "problemMatcher": [] }, { "label": "test-process", "detail": "test that the .envrc environment is available in process.env", "type": "shell", - "command": "test -n \"${env:VARIABLE}\"", + "command": "test -n \"${env:VARIABLE}\" && test -z \"${env:PREDEFINED}\"", "problemMatcher": [] }, ]