Skip to content

Commit 452bd21

Browse files
committed
Fix context-backed env resolution and unified-host account classification
Use env.NewConfigLoader(ctx) in the doctor command's config loader chain so that all SDK config attributes (HOST, TOKEN, PROFILE, etc.) set via env.Set(ctx, ...) participate in auth resolution, not just CONFIG_FILE. Fix isAccountLevelConfig to treat unified-host profiles with both account_id and workspace_id as workspace-level, matching the profiler classification in libs/databrickscfg/profile/profiler.go. Co-authored-by: Isaac
1 parent d870502 commit 452bd21

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

cmd/doctor/checks.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ func checkCurrentProfile(cmd *cobra.Command, resolvedCfg *config.Config) CheckRe
143143

144144
func resolveConfig(cmd *cobra.Command) (*config.Config, error) {
145145
ctx := cmd.Context()
146-
cfg := &config.Config{}
147-
148-
if configFile := env.Get(ctx, "DATABRICKS_CONFIG_FILE"); configFile != "" {
149-
cfg.ConfigFile = configFile
146+
cfg := &config.Config{
147+
Loaders: []config.Loader{
148+
env.NewConfigLoader(ctx),
149+
config.ConfigAttributes,
150+
config.ConfigFile,
151+
},
150152
}
151153

152154
profileFlag := cmd.Flag("profile")
@@ -157,7 +159,8 @@ func resolveConfig(cmd *cobra.Command) (*config.Config, error) {
157159
// Apply the configured default profile when no explicit profile is set.
158160
// This mirrors cmd/root/auth.go's resolveDefaultProfile.
159161
if cfg.Profile == "" && env.Get(ctx, "DATABRICKS_CONFIG_PROFILE") == "" {
160-
resolvedProfile, err := databrickscfg.GetConfiguredDefaultProfile(ctx, cfg.ConfigFile)
162+
configFilePath := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
163+
resolvedProfile, err := databrickscfg.GetConfiguredDefaultProfile(ctx, configFilePath)
161164
if err != nil {
162165
log.Warnf(ctx, "Failed to load default profile: %v", err)
163166
} else if resolvedProfile != "" {
@@ -177,7 +180,8 @@ func isAccountLevelConfig(cfg *config.Config) bool {
177180
case config.AccountHost:
178181
return true
179182
case config.UnifiedHost:
180-
return true
183+
// Unified host with both account_id and workspace_id is workspace-level.
184+
return cfg.WorkspaceID == ""
181185
default:
182186
return false
183187
}

cmd/doctor/doctor_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ func TestResolveConfigUsesEnvVars(t *testing.T) {
295295
assert.Equal(t, "env-token", cfg.Token)
296296
}
297297

298+
func TestResolveConfigUsesContextEnvVars(t *testing.T) {
299+
clearConfigEnv(t)
300+
301+
ctx := cmdio.MockDiscard(t.Context())
302+
ctx = env.Set(ctx, "DATABRICKS_HOST", "https://ctx-env.example.com")
303+
ctx = env.Set(ctx, "DATABRICKS_TOKEN", "ctx-token")
304+
cmd := newTestCmd(ctx)
305+
306+
cfg, err := resolveConfig(cmd)
307+
require.NoError(t, err)
308+
assert.Equal(t, "https://ctx-env.example.com", cfg.Host)
309+
assert.Equal(t, "ctx-token", cfg.Token)
310+
}
311+
298312
func TestCheckIdentitySuccess(t *testing.T) {
299313
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
300314
if r.URL.Path == "/api/2.0/preview/scim/v2/Me" {
@@ -378,6 +392,16 @@ func TestIsAccountLevelConfig(t *testing.T) {
378392
},
379393
want: true,
380394
},
395+
{
396+
name: "unified host with account and workspace ID is workspace-level",
397+
cfg: &config.Config{
398+
Host: "https://myhost.databricks.com",
399+
AccountID: "test-account-123",
400+
WorkspaceID: "12345",
401+
Experimental_IsUnifiedHost: true,
402+
},
403+
want: false,
404+
},
381405
{
382406
name: "unified host without account ID is workspace",
383407
cfg: &config.Config{

0 commit comments

Comments
 (0)