Fix auth switch writing to a stale config path - #1883
Open
jstoadv wants to merge 1 commit into
Open
Conversation
jstoadv
force-pushed
the
fix/auth-switch-stale-config-path
branch
from
July 17, 2026 17:46
dcc47c7 to
4d07690
Compare
`auth switch` writes the updated config to `viper.GetString("config")`, but
`writeConfig` persists `viper.AllSettings()` — which includes the bound
`config` flag — so a top-level `config:` key is saved into the file. On the
next run viper reads it back, and because a config-file value outranks a
flag's default, `GetString("config")` returns that stored path. When a config
is copied between machines the path is stale, so `auth switch` silently writes
there and prints success while the loaded config is left unchanged.
Write to `viper.ConfigFileUsed()` — the path resolved at init, before the
config file is read — so the write target can't be hijacked by the persisted
key. Add a regression test covering a config that carries a stale `config:`.
Fixes digitalocean#1882
jstoadv
force-pushed
the
fix/auth-switch-stale-config-path
branch
from
July 17, 2026 17:49
4d07690 to
ba2ff2e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1882
What
doctl auth switch --context <name>printsNow using context [<name>] by defaultbut silently fails to persist when the config file carries a top-levelconfig:key pointing somewhere other than the file in use. It writes the update to that stale path and leaves the live config unchanged.Why
writeConfig()marshalsviper.AllSettings(), which includes the boundconfigflag, so a top-levelconfig:key gets baked into the file on every write. On the next run viper reads it back, and because a config-file value outranks a flag's default,viper.GetString("config")returns that stored path.defaultConfigFileWriter()then uses it as the write target. When the config was copied between machines (e.g. macOS~/Library/Application Support/doctl/→ a Linux container's~/.config/doctl/), the stored path is stale and the write misses the live file.--config <path>masks the bug because an explicitly-set flag outranks the config-file value.Fix
Write to
viper.ConfigFileUsed()— the path resolved at init, before the config file is read — falling back toviper.GetString("config")only if it is unset. This can't be hijacked by the persisted key. The file still contains theconfig:key (unchanged behavior, still asserted byTestAuthInitConfig); only the write target is corrected.Test
TestDefaultConfigFileWriterUsesLoadedConfigloads a config that carries a staleconfig:path and asserts the writer targets the loaded file, not the stale path. It fails onmainand passes with this change.Reproduce (before this change)