Skip to content

Commit 577ca76

Browse files
authored
fix: default to DEFAULT profile in init command (#4508)
## Changes - We default to the DEFAULT profile in the init command - This is the behavior of the Databricks WorkspaeClient Go SDK when selecting the host. Basically, priorities are: Flag > Env var > DEFAULT profile > Prompt. [code pointer](https://github.com/databricks/databricks-sdk-go/blob/main/config/config_file.go#L90) - However, the profile used by the Go SDK is not persisted in the CLI `Config.Profile` attribute in this scenario (ie, when the DEFAULT one is used). ## Why - The `init` command needs to a profile to resolve the host and instantiate the template. It does so successfully but then fails in the `deploy` step because the profile is ambiguous, despite a specific profile having been used earlier. This behavior is incoherent. ## Tests <!-- How have you tested the changes? --> <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent 787d4a3 commit 577ca76

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

cmd/apps/init.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
appkitRepoURL = "https://github.com/databricks/appkit"
2929
appkitTemplateDir = "template"
3030
appkitDefaultBranch = "main"
31+
defaultProfile = "DEFAULT"
3132
)
3233

3334
// normalizeVersion ensures the version string has a "v" prefix if it looks like a semver.
@@ -808,20 +809,24 @@ func runCreate(ctx context.Context, opts createOptions) error {
808809
if err := os.Chdir(absOutputDir); err != nil {
809810
return fmt.Errorf("failed to change to project directory: %w", err)
810811
}
812+
if profile == "" {
813+
// If the profile is not set, it means the DEFAULT profile was used to infer the workspace host, we set it so that it's used for the deploy and dev-remote commands
814+
profile = defaultProfile
815+
}
811816
}
812817

813818
if shouldDeploy {
814819
cmdio.LogString(ctx, "")
815820
cmdio.LogString(ctx, "Deploying app...")
816-
if err := runPostCreateDeploy(ctx); err != nil {
821+
if err := runPostCreateDeploy(ctx, profile); err != nil {
817822
cmdio.LogString(ctx, fmt.Sprintf("⚠ Deploy failed: %v", err))
818823
cmdio.LogString(ctx, " You can deploy manually with: databricks apps deploy")
819824
}
820825
}
821826

822827
if runMode != prompt.RunModeNone {
823828
cmdio.LogString(ctx, "")
824-
if err := runPostCreateDev(ctx, runMode, projectInitializer, absOutputDir); err != nil {
829+
if err := runPostCreateDev(ctx, runMode, projectInitializer, absOutputDir, profile); err != nil {
825830
return err
826831
}
827832
}
@@ -830,20 +835,25 @@ func runCreate(ctx context.Context, opts createOptions) error {
830835
}
831836

832837
// runPostCreateDeploy runs the deploy command in the current directory.
833-
func runPostCreateDeploy(ctx context.Context) error {
838+
func runPostCreateDeploy(ctx context.Context, profile string) error {
834839
executable, err := os.Executable()
835840
if err != nil {
836841
return fmt.Errorf("failed to get executable path: %w", err)
837842
}
838-
cmd := exec.CommandContext(ctx, executable, "apps", "deploy")
843+
args := []string{"apps", "deploy"}
844+
if profile != "" {
845+
// We ensure the same profile is used for the deploy command as the one used for the init command
846+
args = append(args, "--profile", profile)
847+
}
848+
cmd := exec.CommandContext(ctx, executable, args...)
839849
cmd.Stdout = os.Stdout
840850
cmd.Stderr = os.Stderr
841851
cmd.Stdin = os.Stdin
842852
return cmd.Run()
843853
}
844854

845855
// runPostCreateDev runs the dev or dev-remote command in the current directory.
846-
func runPostCreateDev(ctx context.Context, mode prompt.RunMode, projectInit initializer.Initializer, workDir string) error {
856+
func runPostCreateDev(ctx context.Context, mode prompt.RunMode, projectInit initializer.Initializer, workDir, profile string) error {
847857
switch mode {
848858
case prompt.RunModeDev:
849859
if projectInit != nil {
@@ -858,7 +868,12 @@ func runPostCreateDev(ctx context.Context, mode prompt.RunMode, projectInit init
858868
if err != nil {
859869
return fmt.Errorf("failed to get executable path: %w", err)
860870
}
861-
cmd := exec.CommandContext(ctx, executable, "apps", "dev-remote")
871+
args := []string{"apps", "dev-remote"}
872+
if profile != "" {
873+
// We ensure the same profile is used for the dev-remote command as the one used for the init command
874+
args = append(args, "--profile", profile)
875+
}
876+
cmd := exec.CommandContext(ctx, executable, args...)
862877
cmd.Stdout = os.Stdout
863878
cmd.Stderr = os.Stderr
864879
cmd.Stdin = os.Stdin

0 commit comments

Comments
 (0)