Repository navigation
fix(config): make vfox config write only the key it sets - #739
Conversation
`vfox config <key> <value>` loaded the merged shared and user configuration, changed one field and saved all of it into the user's config.yaml. The user's file then held a copy of every section, the shared aliases included, and because a section that the user's file declares replaces the shared one, a later change of the shared configuration no longer applied. `--unset` did not remove the key either: it wrote the default value. Edit the user's file as a YAML document instead, and touch only the key that is set or unset. File is the generalization of AliasFile, which is now a view of the `aliases` key of a File, so `vfox alias` is unchanged. - Set checks the key against the yaml names of Config and the value against the type of the setting, and writes the value as that type. The other keys and the comments stay, a section the file does not declare stays undeclared, and a replaced value keeps the comment after it. A key that does not exist, a section, a list and a table are refused, and so is a boolean that is neither true nor false (it used to become false without a word). - Unset removes the key and the sections it leaves empty: a section without a key would still be one the file declares, and it would replace the shared one. A key that the file does not have is not an error, and the file is not rewritten. - Save refuses to write a file that does not load as a Config, so a change can no longer leave a config.yaml that stops every command (a cache duration under a second used to be written as ""). - A section that a file declares with one key keeps the defaults of the others when they are not zero values. Path and Diagnostics already did; LegacyVersionFile did not, so `vfox config legacyVersionFile.strategy x` would have switched the parsing of legacy version files off. Tests: with a shared config present, setting one key copies nothing else (on the config level and through the CLI), `--unset` hands the shared value back, and a test walks every setting of Config so that a new one of a type `vfox config` cannot set fails it. The old test of configSet moves to the new API with all its assertions. `vfox config` runs inside the test process now: it no longer changes the defaults that every command shares, which is why it had a process of its own. Part of version-fox#737.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #739 +/- ##
==========================================
+ Coverage 51.39% 56.29% +4.89%
==========================================
Files 111 115 +4
Lines 8240 8564 +324
==========================================
+ Hits 4235 4821 +586
+ Misses 3583 3279 -304
- Partials 422 464 +42 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
aooohan
left a comment
There was a problem hiding this comment.
Found a YAML alias regression in the single-key editor. The same CLI regression test passes with the previous command implementation and fails on this PR (Go 1.24.0). I am preparing a fix and regression tests.
| appendEntry(mapping, name, node) | ||
| case old.Kind == yaml.ScalarNode && node.Kind == yaml.ScalarNode: | ||
| node.HeadComment, node.LineComment, node.FootComment = old.HeadComment, old.LineComment, old.FootComment | ||
| *old = *node |
There was a problem hiding this comment.
[P2] Preserve other settings when editing an anchored node
With proxy: &options {enable: true} and gitignore: *options, running vfox config proxy.enable false also changes gitignore.enable to false. Mutating the anchored mapping in place leaves its aliases reading the updated value, and Save accepts the resulting configuration. Please materialize the YAML references before editing so other settings retain their original values, and add regression coverage. Scalar anchors need coverage too: replacing an anchored timeout drops its anchor and makes Save fail with an unknown-anchor error.
There was a problem hiding this comment.
Fixed in 0c61143, pushed to this PR branch. The editor materializes YAML aliases as independent values before mutation, preserving other settings when an anchor is edited or removed. Comments are retained; cyclic references and excessive expansion are rejected before any write.
Added unit regressions for anchored and aliased sections, scalar anchors, removal, merge references, comments, and expansion guards, plus a CLI regression for the reported proxy/gitignore case. The six reference-editing cases failed before the fix.
Validation with Go 1.24.0: CI=true go test ./..., go build ., and go vet ./internal/config ./cmd/commands pass. CI mode skips the existing printer test that requires interactive input.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain, and the targeted configuration-editing changes are supported by expanded unit and CLI coverage.
No open issues 🎉
What changed in this PR
This pull request updates vfox config to modify only the requested key in the user’s YAML configuration, preserving unrelated settings, comments, and shared configuration behavior. It also refactors alias handling around the new file editor and expands validation, tests, and documentation.
Changes:
- Added typed
config.Fileediting with targetedSet, key-removingUnset, comment preservation, alias handling, and save-time validation. - Updated the CLI and alias commands to edit the user’s configuration file directly.
- Added extensive unit and CLI tests, English and Chinese documentation, and updated project guidance.
| File | Description | Review notes |
|---|---|---|
internal/config/setting.go |
Resolves configuration keys and parses typed command-line values. | Moderate: Positive fractional cache durations such as 1500ms may be serialized as 1s, silently changing the requested value. |
internal/config/setting_test.go |
Tests setting-type discovery and value parsing. | — |
internal/config/legacy_version_file.go |
Preserves defaults for omitted legacy-version fields during YAML decoding. | — |
internal/config/file.go |
Implements YAML-document editing, comment retention, alias expansion, validation, and saving. | Nit: Replacing a null document with an inline comment does not preserve the comment. |
internal/config/file_test.go |
Covers edits, unsets, validation, comments, aliases, and shared configuration behavior. | — |
internal/config/config.go |
Extracts configuration decoding and documents SaveConfig behavior. |
Moderate: Shared decoding does not validate cache durations below one second, so Save can accept a configuration that vfox cannot load. |
internal/config/alias_file.go |
Refactors alias persistence to use config.File. |
— |
internal/config/alias_file_test.go |
Moves top-level mapping coverage to the generic file editor tests. | — |
docs/zh-hans/guides/configuration.md |
Documents key-specific setting and unsetting behavior in Chinese. | — |
docs/guides/configuration.md |
Documents key-specific setting and unsetting behavior in English. | — |
cmd/commands/config.go |
Makes the CLI edit only the requested user-file key. | Moderate: Creating the SDK manager validates the existing configuration before repair commands run, preventing config --unset from repairing legacy-invalid files. |
cmd/commands/config_http_test.go |
Removes the test for the replaced configuration mutation API. | — |
cmd/commands/config_cli_test.go |
Adds CLI coverage for selective writes, unsets, errors, and YAML aliases. | — |
cmd/commands/cli_test.go |
Runs config in-process now that it no longer mutates shared defaults. |
— |
AGENTS.md |
Updates configuration-editing guidance and references the new file API. | — |
Review effort: Lite
Part of #737 (item 4, "
vfox configshould write only the key it sets").What changes
vfox config <key> <value>loaded the merged shared and user configuration and saved all of it into the user'sconfig.yaml;--unsetwrote the default value. Now the command edits the user's file as a YAML document and touches only the key that is set or unset, keeping the other keys and the comments.config.AliasFileis generalized intoconfig.Filefor it;AliasFileis now a view of thealiaseskey of aFile, sovfox aliasbehaves as before and its tests pass as they were; only the test of the top-level mapping moved toFile, together with the code it tests.Checked with real binaries, built from
mainand from this branch. The sharedconfig.yamlhaspath.mode: baseline,diagnostics.onUse: false,cache.availableHookDuration: 24hand an alias; the user is new and runsvfox config proxy.url http://localhost:7890:mainpath,diagnosticsandaliasesin the user's filepath.modetoaggressivebaselineaggressivevfox config --unset proxy.urlurl: ""stays in the filebaseline;config path.mode legacy, then--unset path.modelegacystays in the file and winspathis gone, the user getsbaselineagainCode
File.Setchecks the key against the yaml names ofConfigand the value against the type of the setting, and writes the value as that type. The other keys and the comments stay, a section the file does not declare stays undeclared, and a replaced value keeps the comment after it. Strings that YAML would read as something else ("true","21") are quoted.File.Unsetremoves the key and the sections it leaves empty. That part matters: a section without a key is still a section the file declares, and it would replace the shared one. A key the file does not have is not an error, and the file is not rewritten. A whole section can be unset (vfox config --unset path), which also cleans up the copies that earlier versions wrote.File.Saverefuses to write a file that does not load as aConfig. Before,vfox config cache.availableHookDuration 500mswroteavailableHookDuration: "", and aconfig.yamlthat does not load stops every command, including the one that could fix it.LegacyVersionFilegets theUnmarshalYAMLthatPathandDiagnosticshave, so a section declared with one key keeps the defaults of the others. Without itvfox config legacyVersionFile.strategy latest_installedon a file that lacks the section would have switched legacy version files off:enableistrueby default andfalseonce the section exists without it.AGENTS.mdsays that a section like this has to do the same.decodeConfigis whatNewConfigWithPathdid to read a file, extracted so thatSavecan check with the same code.Behavior that changes with it, all of them cases that used to go wrong without a word:
proxy.ulr) is an error. Before, nothing was set and the whole file was rewritten.truenorfalse(yes) is an error. Before, it becamefalse.proxy), a list or a table (path.preserve,diagnostics.ignore,aliases) is refused with a message. Lists and tables can still be edited in the file, and removed with--unset;aliaseshasvfox alias.cache.availableHookDurationother than-1, and one under a second, is refused. The messages for the HTTP timeouts name the key (plugin.http.downloadTimeout: must not be negative; it saidtimeoutfor both).Tests
internal/config:Fileagainst files with comments, no content, only a comment, a null document,{}, an empty or null section and the flow style; every type of setting; everything that has to be refused (the file stays as it was);Unsetstep by step through a file, including the sections that go with their last key; the shared-config scenario of Follow-ups of #736: plugin updates, the path.mode default and vfox config #737 (the file vfox creates on its first run, one setting changed, the shared config changed afterwards, the user's own value set and unset);Saverefusing a file that would not load; and a test that walks every setting ofConfig, so that a new setting of a typevfox configcannot set fails it.cmd/commands: with a shared config present, setting one key copies nothing else (a user with a file and a new user);--unsethands the shared value back; the errors leave the file alone. The oldTestConfigSetHTTPTimeoutmoved to the new API with all its assertions (TestSetHTTPTimeouts), and now also checks that unsetting removes the keys.configruns inside the test process now (inProcessincli_test.go). It had a process of its own because the oldconfigSetchanged the defaults that every command shares (EmptyPathand the like):TestConfigCommandHandlesTheNewSettingsfails onmain'sconfig.goonceconfigis ininProcess. The new code does not touch them.main'sconfig.go. I broke the implementation ten ways (no pruning of empty sections, the comment of a replaced value dropped, theSavecheck removed, the flow style kept for the first key, a null section not turned into a mapping,LegacyVersionFilewithout its defaults, the boolean error ignored, a negative timeout accepted, unknown keys accepted, a float setting without a parser) and a test failed each time.Docs
docs/guides/configuration.mdand the zh-hans copy: whatvfox config <key> <value>and--unsetdo to the file, that lists and tables are edited in the file, and a callout thatpathanddiagnosticsstill replace the shared section as a whole once the file declares them (sovfox config path.mode baselinedrops apath.preserveof the shared config for that user, andvfox config --unset pathgives it back).AGENTS.mdsays where this lives and replaces the sentence thatvfox configsaves the merged config.Not in this PR
pathanddiagnosticskey by key would need to know which keys a file declared; the callout above documents the rule as it is.proxy,gitignoreorlegacyVersionFile, the shared values of those sections do not apply.NewConfigWithPathputs the default in the missing section, andmergeProxy,mergeGitignoreandmergeLegacyVersionFiletake any non-nil user section as the user's. The other sections follow the shared config (path,diagnosticsandaliasesby design,plugin.httpper key,registry,storageandcachethrough the empty or default value). So--unsethands the shared value back for those, and the default for these three. I left the merge alone; it deserves its own issue.Config.SaveConfigis only used by tests now. It stays, with a comment that it is not how the user's file is changed; removing it touches five unrelated test files.path.preserve,diagnostics.ignore) from the command line.Draft for the release notes
Verification
go teston all packages exceptinternal/shared/printer(it needs a TTY): pass, on the commit as it is and on the tree that merges it with #738 (they do not conflict).go vetand the compilation of the tests forGOOS=linux,windowsanddarwin: clean. The docs site builds (VitePress 1.0.0 as inpackage-lock.json, no dead links). The CLI tests skip on Windows, as the fixture does (#737, item 5); theinternal/configtests run there.CI on my fork for the head commit (d4ebc1e) is green, with the Go 1.24.0 of
go.mod:VersionFox CI(ubuntu, macOS, Windows, the Windows regressions, Fish) andvfox E2E Test(Unix, macOS, Windows, MSIX packaging): CI, E2E.