Skip to content

Commit 1a9d0e8

Browse files
authored
Deprecate shell -- <cmd> in favor of run <cmd> (#642)
## Summary Makes `devbox shell -- <cmd>` print a Warning and recommend using `devbox run <cmd>` instead. This behavior is protected behind the UNIFIED_ENV feature flag. Note that `devbox services` internally uses `devbox shell -- <cmd>`, so when UNIFIED_ENV is enabled, it'll use `devbox run` instead. But it's a bit circular... when outside of a shell, running `devbox services start` is exactly equivalent to running `devbox run devbox services start`. ## How was it tested? ``` DEVBOX_FEATURE_UNIFIED_ENV=0 ./devbox shell --help DEVBOX_FEATURE_UNIFIED_ENV=1 ./devbox shell --help DEVBOX_FEATURE_UNIFIED_ENV=0 ./devbox shell -- echo "foo" # no warning DEVBOX_FEATURE_UNIFIED_ENV=1 ./devbox shell -- echo "foo" # see warning DEVBOX_FEATURE_UNIFIED_ENV=1 ./devbox shell -- echo '$PATH' DEVBOX_FEATURE_UNIFIED_ENV=1 ./devbox run echo '$PATH' # same paths ```
1 parent 39e13f6 commit 1a9d0e8

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

internal/boxcli/shell.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/spf13/cobra"
1111
"go.jetpack.io/devbox"
12+
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1213
"go.jetpack.io/devbox/internal/boxcli/usererr"
14+
"go.jetpack.io/devbox/internal/ux"
1315
)
1416

1517
type shellCmdFlags struct {
@@ -18,15 +20,25 @@ type shellCmdFlags struct {
1820
}
1921

2022
func ShellCmd() *cobra.Command {
21-
flags := shellCmdFlags{}
22-
command := &cobra.Command{
23-
Use: "shell -- [<cmd>]",
24-
Short: "Start a new shell or run a command with access to your packages",
25-
Long: "Start a new shell or run a command with access to your packages.\n\n" +
23+
var longHelp string
24+
if featureflag.UnifiedEnv.Enabled() {
25+
longHelp = "Start a new shell with access to your packages.\n\n" +
26+
"The shell will be started using the devbox.json found in the --config flag directory. " +
27+
"If --config isn't set, then devbox recursively searches the current directory and its parents.\n\n" +
28+
"[Deprecated] If invoked as devbox shell -- <cmd>, devbox will run the command in a shell and then exit. " +
29+
"This behavior is deprecated and will be removed. Please use devbox run -- <cmd> instead."
30+
} else {
31+
longHelp = "Start a new shell or run a command with access to your packages.\n\n" +
2632
"If invoked without `cmd`, devbox will start an interactive shell.\n" +
2733
"If invoked with a `cmd`, devbox will run the command in a shell and then exit.\n" +
2834
"In both cases, the shell will be started using the devbox.json found in the --config flag directory. " +
29-
"If --config isn't set, then devbox recursively searches the current directory and its parents.",
35+
"If --config isn't set, then devbox recursively searches the current directory and its parents."
36+
}
37+
flags := shellCmdFlags{}
38+
command := &cobra.Command{
39+
Use: "shell -- [<cmd>]",
40+
Short: "Start a new shell with access to your packages",
41+
Long: longHelp,
3042
Args: validateShellArgs,
3143
PreRunE: ensureNixInstalled,
3244
RunE: func(cmd *cobra.Command, args []string) error {
@@ -68,6 +80,10 @@ func runShellCmd(cmd *cobra.Command, args []string, flags shellCmdFlags) error {
6880
}
6981

7082
if len(cmds) > 0 {
83+
if featureflag.UnifiedEnv.Enabled() {
84+
ux.Fwarning(cmd.ErrOrStderr(), "\"devbox shell -- <cmd>\" is deprecated and will disappear "+
85+
"in a future version. Use \"devbox run -- <cmd>\" instead\n")
86+
}
7187
err = box.Exec(cmds...)
7288
} else {
7389
err = box.Shell()

internal/impl/devbox.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ func (d *Devbox) ListScripts() []string {
406406
return keys
407407
}
408408

409-
func (d *Devbox) Exec(cmds ...string) error {
409+
// TODO: deprecate in favor of RunScript().
410+
func (d *Devbox) ExecWithShell(cmds ...string) error {
410411
if err := d.ensurePackagesAreInstalled(ensure); err != nil {
411412
return err
412413
}
@@ -426,8 +427,20 @@ func (d *Devbox) Exec(cmds ...string) error {
426427
pathWithProfileBin := fmt.Sprintf("PATH=%s%s:$PATH", virtenvBinPath, profileBinPath)
427428
cmds = append([]string{pathWithProfileBin}, cmds...)
428429

429-
nixDir := filepath.Join(d.projectDir, ".devbox/gen/shell.nix")
430-
return nix.Exec(nixDir, cmds, env)
430+
return nix.Exec(d.nixShellFilePath(), cmds, env)
431+
}
432+
433+
// TODO: deprecate in favor of RunScript().
434+
func (d *Devbox) Exec(cmds ...string) error {
435+
if featureflag.UnifiedEnv.Disabled() {
436+
return d.ExecWithShell(cmds...)
437+
} else {
438+
if len(cmds) > 0 {
439+
return d.RunScript(cmds[0], cmds[1:])
440+
} else {
441+
return errors.Errorf("cannot execute empty command: %v", cmds)
442+
}
443+
}
431444
}
432445

433446
func (d *Devbox) PrintEnv() (string, error) {

testscripts/run/command.test.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exec devbox init
2+
3+
# We can run an arbitrary command
4+
exec devbox run echo "foo"
5+
stdout 'foo'
6+
7+
# devbox shell -- <cmd> prints warning and is implemented with devbox run.
8+
# NOTE that devbox run passes DEVBOX_* vars through, but devbox shell does not.
9+
env DEVBOX_FOO=bar
10+
exec devbox shell -- echo '$DEVBOX_FOO'
11+
stdout 'bar'
12+
stderr '[Warning]' # for some reason, putting these two assertions in a single line makes the test fail.
13+
stderr '"devbox shell -- <cmd>" is deprecated and will disappear in a future version.' # Use "devbox run -- <cmd>" instead'

0 commit comments

Comments
 (0)