Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine mode: production diagnostic output #2236

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions bundle/config/mutator/process_target_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/iamutil"
Expand Down Expand Up @@ -149,19 +150,35 @@

// We need to verify that there is only a single deployment of the current target.
// The best way to enforce this is to explicitly set root_path.
advice := fmt.Sprintf(
"set 'workspace.root_path' to make sure only one copy is deployed. A common practice is to use a username or principal name in this path, i.e. root_path: /Workspace/Users/%s/.bundle/${bundle.name}/${bundle.target}",
advice := "set 'workspace.root_path' to make sure only one copy is deployed"
adviceDetail := fmt.Sprintf(
"A common practice is to use a username or principal name in this path, i.e. use\n"+
"\n"+
" root_path: /Workspace/Users/%s/.bundle/${bundle.name}/${bundle.target}",
b.Config.Workspace.CurrentUser.UserName,
)
if !isExplicitRootSet(b) {
cmdio.LogString(ctx, fmt.Sprintf("root_path is not set: %s", b.Config.Bundle.Target))

Check failure on line 161 in bundle/config/mutator/process_target_mode.go

View workflow job for this annotation

GitHub Actions / lint

fmt.Sprintf can be replaced with string concatenation (perfsprint)
if isRunAsSet(r) || isPrincipalUsed {
// Just setting run_as is not enough to guarantee a single deployment,
// and neither is setting a principal.
// We only show a warning for these cases since we didn't historically
// report an error for them.
return diag.Recommendationf("target with 'mode: production' should %s", advice)
return diag.Diagnostics{
{
Severity: diag.Recommendation,
Summary: "target with 'mode: production' should " + advice,
Detail: adviceDetail,
},
}
}
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: "target with 'mode: production' must " + advice,
Detail: adviceDetail,
Copy link
Contributor Author

@lennartkats-db lennartkats-db Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pietern I'd really like to include location information here and in the other diagnostics in this file, but I'm not sure there is any good way to do that without reverting 6ea5306 (which was originally discussed in #1712 (comment))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which location do you want to include?

The conditional checks for absence, so the best possible location seems to be the target definition itself?

If the variables v2 proposal from @denik materializes, we can revisit access to the original targets section. The comment I made in that thread still holds now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd just include the target block as seen in 9466bdc. The same would apply to a couple of the other messages in process_target_mode.

So it seems that the location information would be another benefit of decomposing select_target. We may care more about that in a later stage of the current workspace work. I think we covered all the other trade-offs in the other thread.

Variable resolution is a good point though — it seems like we would want to resolve variables for the selected target. And we could preemptively clean up the other targets to avoid confusion.

In any case, I'd be okay to merge this as is for now. With the extra comments, the surprises of select_target are somewhat alleviated. And we can do without location information for now.

},
}
return diag.Errorf("target with 'mode: production' must %s", advice)
}
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions bundle/config/mutator/process_target_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,16 @@ func TestProcessTargetModeProduction(t *testing.T) {
b := mockBundle(config.Production)

diags := validateProductionMode(context.Background(), b, false)
require.ErrorContains(t, diags.Error(), "target with 'mode: production' must set 'workspace.root_path' to make sure only one copy is deployed. A common practice is to use a username or principal name in this path, i.e. root_path: /Workspace/Users/lennart@company.com/.bundle/${bundle.name}/${bundle.target}")
assert.ErrorContains(t, diags.Error(), "A common practice is to use a username or principal name in this path, i.e. use\n\n root_path: /Workspace/Users/lennart@company.com/.bundle/${bundle.name}/${bundle.target}")
assert.NotNil(t, diags[0].Locations)

b.Config.Workspace.StatePath = "/Shared/.bundle/x/y/state"
b.Config.Workspace.ArtifactPath = "/Shared/.bundle/x/y/artifacts"
b.Config.Workspace.FilePath = "/Shared/.bundle/x/y/files"
b.Config.Workspace.ResourcePath = "/Shared/.bundle/x/y/resources"

diags = validateProductionMode(context.Background(), b, false)
require.ErrorContains(t, diags.Error(), "target with 'mode: production' must set 'workspace.root_path' to make sure only one copy is deployed. A common practice is to use a username or principal name in this path, i.e. root_path: /Workspace/Users/lennart@company.com/.bundle/${bundle.name}/${bundle.target}")
assert.ErrorContains(t, diags.Error(), "A common practice is to use a username or principal name in this path, i.e. use\n\n root_path: /Workspace/Users/lennart@company.com/.bundle/${bundle.name}/${bundle.target}")

permissions := []resources.Permission{
{
Expand Down
Loading