#23339: fix: use snapshot.parsed for env ref restoration during migrate
size: XS
Cluster:
Config Management Improvements
During config migrate, env var references like $TOKEN are being resolved to actual values and written to the config file. This fix uses snapshot.parsed (pre-substitution) instead of re-reading the file.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixed a bug where environment variable references like `${TOKEN}` were being resolved to actual values (e.g., `sk-ant-...`) when written back to the config file during migrations. The old code re-read the file from disk to get pre-substitution values, but during migrations the on-disk file may already be partially modified, causing the wrong baseline to be used. The fix uses `snapshot.parsed` (already loaded pre-substitution config) instead of re-reading from disk.
- Changed from re-reading the config file with `fs.readFile` + `parseConfigJson5` to using the existing `snapshot.parsed`
- `snapshot.parsed` is populated in `readConfigFileSnapshotInternal` at line 749 and contains the parsed JSON5 before `$include` resolution and `${ENV}` substitution
- This ensures env var references are properly preserved even when the file is being modified mid-migration
<h3>Confidence Score: 5/5</h3>
- Safe to merge - straightforward fix that uses already-loaded data instead of re-reading from disk
- The fix correctly addresses the issue by using `snapshot.parsed` which is already loaded and guaranteed to represent the pre-substitution config state. The change is minimal, well-documented in comments, and uses existing data structures. The logic is sound: during migrations, re-reading the file could pick up partial modifications, whereas `snapshot` was loaded at the start and represents a consistent state.
- No files require special attention
<sub>Last reviewed commit: bd7a96b</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#4897: fix: config logic issues (#4689, #4654)
by lailoo · 2026-01-30
74.3%
#15613: fix(config): align default pipelines across loadConfig and readConf...
by AI-Reviewer-QS · 2026-02-13
74.1%
#5040: fix(config): migrate audio.transcription with any CLI command
by shayan919293 · 2026-01-31
73.5%
#10258: fix(config): preserve ${ENV_VAR} references when writing config (#9...
by nu-gui · 2026-02-06
73.5%
#16991: fix(config): add missing defaults to config snapshot path
by AI-Reviewer-QS · 2026-02-15
72.4%
#14179: fix(config): resolve $include directives before validation in setup...
by ken2190 · 2026-02-11
72.0%
#12048: fix: deduplicate config warnings to log once instead of on every re...
by mcaxtr · 2026-02-08
71.9%
#12792: fix: exclude 'tokens' (plural) fields from config redaction
by jpaine · 2026-02-09
71.6%
#19449: fix: preserve .env during package/plugin updates
by Sid-V5 · 2026-02-17
71.6%
#13849: fix(configure): preserve custom config keys across wizard sections
by mcaxtr · 2026-02-11
71.4%