Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 4 additions & 12 deletions internal/boxcli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
package boxcli

import (
"sort"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
"go.jetpack.io/devbox/internal/debug"
"golang.org/x/exp/slices"
)

type runCmdFlags struct {
Expand Down Expand Up @@ -50,14 +47,6 @@ func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {
return errors.WithStack(err)
}

// Validate script exists.
scripts := box.ListScripts()
sort.Slice(scripts, func(i, j int) bool { return scripts[i] < scripts[j] })
if script == "" || !slices.Contains(scripts, script) {
return errors.Errorf("no script found with name \"%s\". "+
"Here's a list of the existing scripts in devbox.json: %v", script, scripts)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens when a user has a script in their devbox.json named "ls" and they run devbox run ls ?
Does the script alias take precedent over the command ls? or vice versa?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The script takes precedence


if devbox.IsDevboxShellEnabled() {
err = box.RunScriptInShell(script)
} else {
Expand All @@ -73,10 +62,13 @@ func parseScriptArgs(args []string, flags runCmdFlags) (string, string, []string
}

script := ""
scriptArgs := []string{}
var scriptArgs []string
if len(args) >= 1 {
script = args[0]
scriptArgs = args[1:]
} else {
// this should never happen because cobra should prevent it, but it's better to be defensive.
return "", "", nil, errors.New("no command or script provided")
}

return path, script, scriptArgs, nil
Expand Down
18 changes: 9 additions & 9 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,9 @@ func (d *Devbox) Shell() error {
return shell.Run(nixShellFilePath)
}

func (d *Devbox) RunScript(scriptName string, scriptArgs []string) error {
func (d *Devbox) RunScript(cmdName string, cmdArgs []string) error {
if featureflag.NixDevEnvRun.Disabled() {
return d.RunScriptInNewNixShell(scriptName)
}

script := d.cfg.Shell.Scripts[scriptName]
if script == nil {
return errors.Errorf("unable to find a script with name %s", scriptName)
return d.RunScriptInNewNixShell(cmdName)
}

if err := d.ensurePackagesAreInstalled(install); err != nil {
Expand All @@ -272,9 +267,14 @@ func (d *Devbox) RunScript(scriptName string, scriptArgs []string) error {
return err
}

cmdWithArgs := append([]string{cmdName}, cmdArgs...)
if _, ok := d.cfg.Shell.Scripts[cmdName]; ok {
// it's a script, so replace the command with the script file's path.
cmdWithArgs = append([]string{d.scriptPath(cmdName)}, cmdArgs...)
}

nixShellFilePath := filepath.Join(d.projectDir, ".devbox/gen/shell.nix")
scriptWithArgs := strings.Join(append([]string{d.scriptPath(scriptName)}, scriptArgs...), " ")
return nix.RunScript(nixShellFilePath, d.projectDir, scriptWithArgs, pluginEnv)
return nix.RunScript(nixShellFilePath, d.projectDir, strings.Join(cmdWithArgs, " "), pluginEnv)
}

// RunScriptInNewNixShell implements `devbox run` (from outside a devbox shell) using a nix shell.
Expand Down
8 changes: 4 additions & 4 deletions internal/nix/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"go.jetpack.io/devbox/internal/debug"
)

func RunScript(nixShellFilePath string, projectDir string, scriptPath string, additionalEnv []string) error {
if scriptPath == "" {
return errors.New("attempted to run script but did not specify script name")
func RunScript(nixShellFilePath string, projectDir string, cmdWithArgs string, additionalEnv []string) error {
if cmdWithArgs == "" {
return errors.New("attempted to run an empty command or script")
}

vaf, err := PrintDevEnv(nixShellFilePath)
Expand All @@ -28,7 +28,7 @@ func RunScript(nixShellFilePath string, projectDir string, scriptPath string, ad
}
}

cmd := exec.Command("sh", "-c", scriptPath)
cmd := exec.Command("sh", "-c", cmdWithArgs)
cmd.Env = append(nixEnv, additionalEnv...)
cmd.Dir = projectDir
cmd.Stdin = os.Stdin
Expand Down