Skip to content

Commit b72efb9

Browse files
pieternclaude
andauthored
Return early from script mutator if no script is defined (#4711)
## Summary - Move the empty command check to the top of `script.Apply` so that `NewCommandExecutor` (which requires a shell to be available) is not called when no script hook is configured. - Simplify `executeHook` by passing the resolved command directly instead of re-fetching it from the bundle. Found out about this dependency during investigation of #4710. ## Test plan - [x] Existing unit tests pass - [x] No new behavior introduced; this is a refactor that reorders existing checks 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 11d89ff commit b72efb9

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

bundle/scripts/scripts.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ func (m *script) Name() string {
3232
}
3333

3434
func (m *script) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
35+
command := getCommand(b, m.scriptHook)
36+
if command == "" {
37+
log.Debugf(ctx, "No script defined for %s, skipping", m.scriptHook)
38+
return nil
39+
}
40+
3541
executor, err := exec.NewCommandExecutor(b.BundleRootPath)
3642
if err != nil {
3743
return diag.FromErr(err)
3844
}
3945

40-
cmd, out, err := executeHook(ctx, executor, b, m.scriptHook)
46+
cmd, out, err := executeHook(ctx, executor, command)
4147
if err != nil {
4248
return diag.FromErr(fmt.Errorf("failed to execute script: %w", err))
4349
}
44-
if cmd == nil {
45-
log.Debugf(ctx, "No script defined for %s, skipping", m.scriptHook)
46-
return nil
47-
}
4850

4951
cmdio.LogString(ctx, fmt.Sprintf("Executing '%s' script", m.scriptHook))
5052

@@ -63,12 +65,7 @@ func (m *script) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
6365
return nil
6466
}
6567

66-
func executeHook(ctx context.Context, executor *exec.Executor, b *bundle.Bundle, hook config.ScriptHook) (exec.Command, io.Reader, error) {
67-
command := getCommmand(b, hook)
68-
if command == "" {
69-
return nil, nil, nil
70-
}
71-
68+
func executeHook(ctx context.Context, executor *exec.Executor, command config.Command) (exec.Command, io.Reader, error) {
7269
// Don't run any arbitrary code when restricted execution is enabled.
7370
if _, ok := env.RestrictedExecution(ctx); ok {
7471
return nil, nil, errors.New("running scripts is not allowed when DATABRICKS_BUNDLE_RESTRICTED_CODE_EXECUTION is set")
@@ -82,7 +79,7 @@ func executeHook(ctx context.Context, executor *exec.Executor, b *bundle.Bundle,
8279
return cmd, io.MultiReader(cmd.Stdout(), cmd.Stderr()), nil
8380
}
8481

85-
func getCommmand(b *bundle.Bundle, hook config.ScriptHook) config.Command {
82+
func getCommand(b *bundle.Bundle, hook config.ScriptHook) config.Command {
8683
if b.Config.Experimental == nil || b.Config.Experimental.Scripts == nil {
8784
return ""
8885
}

0 commit comments

Comments
 (0)