Skip to content

Commit

Permalink
use empty string to "unset" variables
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkhl committed Oct 27, 2022
1 parent 267f173 commit 79a4841
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? '']),
),
)
}
Expand All @@ -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)
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/workspace/.envrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# shellcheck shell=bash
export VARIABLE=value
unset PREDEFINED
4 changes: 2 additions & 2 deletions test/workspace/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
},
]
Expand Down

0 comments on commit 79a4841

Please sign in to comment.