Skip to content

Commit ce26292

Browse files
committed
Apply configured default profile in doctor's config resolution
resolveConfig now checks for [__settings__].default_profile when no explicit profile is set via --profile flag or DATABRICKS_CONFIG_PROFILE. This mirrors the resolution logic in cmd/root/auth.go so users who rely on `databricks auth switch` see the correct profile in doctor output. Co-authored-by: Isaac
1 parent 3525dd3 commit ce26292

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

cmd/doctor/checks.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/databricks/cli/internal/build"
13+
"github.com/databricks/cli/libs/databrickscfg"
1314
"github.com/databricks/cli/libs/databrickscfg/profile"
1415
"github.com/databricks/cli/libs/env"
1516
"github.com/databricks/cli/libs/log"
@@ -159,6 +160,17 @@ func resolveConfig(cmd *cobra.Command) (*config.Config, error) {
159160
cfg.Profile = profileFlag.Value.String()
160161
}
161162

163+
// Apply the configured default profile when no explicit profile is set.
164+
// This mirrors cmd/root/auth.go's resolveDefaultProfile.
165+
if cfg.Profile == "" && env.Get(ctx, "DATABRICKS_CONFIG_PROFILE") == "" {
166+
resolvedProfile, err := databrickscfg.GetConfiguredDefaultProfile(ctx, cfg.ConfigFile)
167+
if err != nil {
168+
log.Warnf(ctx, "Failed to load default profile: %v", err)
169+
} else if resolvedProfile != "" {
170+
cfg.Profile = resolvedProfile
171+
}
172+
}
173+
162174
return cfg, cfg.EnsureResolved()
163175
}
164176

cmd/doctor/doctor_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ func TestCheckCurrentProfileFlagOverridesEnv(t *testing.T) {
184184
assert.Equal(t, "from-flag", result.Message)
185185
}
186186

187+
func TestResolveConfigUsesConfiguredDefaultProfile(t *testing.T) {
188+
clearConfigEnv(t)
189+
190+
configFile := filepath.Join(t.TempDir(), ".databrickscfg")
191+
err := os.WriteFile(configFile, []byte("[__settings__]\ndefault_profile = my-ws\n\n[my-ws]\nhost = https://my-ws.cloud.databricks.com\ntoken = my-token\n"), 0o600)
192+
require.NoError(t, err)
193+
194+
ctx := cmdio.MockDiscard(t.Context())
195+
ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", configFile)
196+
cmd := newTestCmd(ctx)
197+
198+
cfg, resolveErr := resolveConfig(cmd)
199+
require.NoError(t, resolveErr)
200+
assert.Equal(t, "my-ws", cfg.Profile)
201+
assert.Equal(t, "https://my-ws.cloud.databricks.com", cfg.Host)
202+
}
203+
204+
func TestResolveConfigDefaultProfileIgnoredWhenFlagSet(t *testing.T) {
205+
clearConfigEnv(t)
206+
207+
configFile := filepath.Join(t.TempDir(), ".databrickscfg")
208+
err := os.WriteFile(configFile, []byte("[__settings__]\ndefault_profile = my-ws\n\n[my-ws]\nhost = https://my-ws.cloud.databricks.com\ntoken = ws-token\n\n[other]\nhost = https://other.cloud.databricks.com\ntoken = other-token\n"), 0o600)
209+
require.NoError(t, err)
210+
211+
ctx := cmdio.MockDiscard(t.Context())
212+
ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", configFile)
213+
cmd := newTestCmd(ctx)
214+
err = cmd.Flag("profile").Value.Set("other")
215+
require.NoError(t, err)
216+
cmd.Flag("profile").Changed = true
217+
218+
cfg, resolveErr := resolveConfig(cmd)
219+
require.NoError(t, resolveErr)
220+
assert.Equal(t, "other", cfg.Profile)
221+
assert.Equal(t, "https://other.cloud.databricks.com", cfg.Host)
222+
}
223+
187224
func TestCheckAuthSuccess(t *testing.T) {
188225
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
189226
w.WriteHeader(http.StatusOK)

0 commit comments

Comments
 (0)