refreshenv overwrites volatile environment variables (APPDATA, LOCALAPPDATA, USERPROFILE, etc.)
- Dominant language
- C#
- Stars
- 11.5k
- Forks
- 960
- PR merge metrics
- No merged PRs in 30d
Description
### Checklist
- [x] I confirm there are no unresolved issues reported on the [Chocolatey Status page](https://status.chocolatey.org).
- [x] I have verified this is the correct repository for opening this issue.
- [x] I have verified no other issues exist related to my problem.
- [x] I have verified this is not an issue for a specific package.
- [x] I have verified this issue is not security related.
- [x] I confirm I am using **official**, and **not** unofficial, or modified, Chocolatey products.
### What You Are Seeing?
Running `refreshenv` overwrites volatile environment variables like `APPDATA, LOCALAPPDATA, USERPROFILE, HOMEDRIVE, and HOMEPATH` if they exist in the registry. This is the same class of bug as #3681 (fixed for TEMP/TMP in v2.6.0) and related to #3021 (PATH entries being overwritten).
In my case, `HKCU\Environment` contained an incorrect `APPDATA` entry `(C:\Users\Username\AppData` instead of `C:\Users\Username\AppData\Roaming`). After running `refreshenv`, the session's `APPDATA` was corrupted, causing VS Code to create its data directory in the wrong location (`AppData\Code instead of AppData\Roaming\Code`).
### What is Expected?
`refreshenv `should preserve volatile environment variables that Windows generates at login. The fix in #3681 preserves `USERNAME, PROCESSOR_ARCHITECTURE, TEMP, and TMP` but does not protect other volatile variables.
Namely:
- APPDATA
- LOCALAPPDATA
- USERPROFILE
- HOMEDRIVE
- HOMEPATH
### How Did You Get This To Happen?
1. Have an incorrect `APPDATA `entry in `HKCU\Environment `(can happen via misbehaving installer or manual error)
2. Run `refreshenv`
3. Check `$env:APPDATA` - it now has the wrong value from the registry
### System Details
* Operating System: Windows 11 24H2 LTSC Version 10.0.26100.6899]
* Windows PowerShell version: 7.5.4
* Chocolatey CLI Version: 2.6.0
* Chocolatey Licensed Extension version: N/A
* Chocolatey License type: Open Source
* Terminal/Emulator: Windows Terminal (pwsh)
### Installed Packages
```bash
N/A
```
### Output Log
```bash
Unable to provide output log as the issue has been fixed at my end (removed the bad actor in environment)
```
### Additional Context
_No response_
Contributor guide
Research direction
Start at the refreshenv entry point and compare its handling of registry environment variables with the existing preservation behavior for USERNAME, PROCESSOR_ARCHITECTURE, TEMP, and TMP described in the issue. Reproduce the APPDATA case using an incorrect HKCU\Environment value, then add coverage for the listed volatile variables and verify that refreshenv preserves the session values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- cli, operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100