@@ -484,9 +484,9 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
484484 Flag : varUseKeyring ,
485485 Env : envUseKeyring ,
486486 Description : "Store and retrieve session tokens using the operating system " +
487- "keyring. Enabled by default. If the keyring is not supported on the " +
488- "current platform, file-based storage is used automatically. Set to " +
489- "false to force file-based storage." ,
487+ "keyring. This flag is ignored and file-based storage is used when " +
488+ "--global-config is set or keyring usage is not supported on the current " +
489+ "platform. Set to false to force file-based storage on supported platforms ." ,
490490 Default : "true" ,
491491 Value : serpent .BoolOf (& r .useKeyring ),
492492 Group : globalGroup ,
@@ -537,11 +537,12 @@ type RootCmd struct {
537537 disableDirect bool
538538 debugHTTP bool
539539
540- disableNetworkTelemetry bool
541- noVersionCheck bool
542- noFeatureWarning bool
543- useKeyring bool
544- keyringServiceName string
540+ disableNetworkTelemetry bool
541+ noVersionCheck bool
542+ noFeatureWarning bool
543+ useKeyring bool
544+ keyringServiceName string
545+ useKeyringWithGlobalConfig bool
545546}
546547
547548// InitClient creates and configures a new client with authentication, telemetry,
@@ -722,8 +723,14 @@ func (r *RootCmd) createUnauthenticatedClient(ctx context.Context, serverURL *ur
722723// flag.
723724func (r * RootCmd ) ensureTokenBackend () sessionstore.Backend {
724725 if r .tokenBackend == nil {
726+ // Checking for the --global-config directory being set is a bit wonky but necessary
727+ // to allow extensions that invoke the CLI with this flag (e.g. VS code) to continue
728+ // working without modification. In the future we should modify these extensions to
729+ // either access the credential in the keyring (like Coder Desktop) or some other
730+ // approach that doesn't rely on the session token being stored on disk.
731+ assumeExtensionInUse := r .globalConfig != config .DefaultDir () && ! r .useKeyringWithGlobalConfig
725732 keyringSupported := runtime .GOOS == "windows" || runtime .GOOS == "darwin"
726- if r .useKeyring && keyringSupported {
733+ if r .useKeyring && ! assumeExtensionInUse && keyringSupported {
727734 serviceName := sessionstore .DefaultServiceName
728735 if r .keyringServiceName != "" {
729736 serviceName = r .keyringServiceName
@@ -743,6 +750,13 @@ func (r *RootCmd) WithKeyringServiceName(serviceName string) {
743750 r .keyringServiceName = serviceName
744751}
745752
753+ // UseKeyringWithGlobalConfig enables the use of the keyring storage backend
754+ // when the --global-config directory is set. This is only intended as an override
755+ // for tests, which require specifying the global config directory for test isolation.
756+ func (r * RootCmd ) UseKeyringWithGlobalConfig () {
757+ r .useKeyringWithGlobalConfig = true
758+ }
759+
746760type AgentAuth struct {
747761 // Agent Client config
748762 agentToken string
0 commit comments