Skip to content

Commit f65e037

Browse files
authored
Revert "[RFC][direnv-inspired] introduce export and hook commands, use in devbox shell, global and direnv (#1172)" (#1233)
## Summary Reverts #1172. While we feel architecturally this is a better approach, it would introduce some non-trivial risk. For the moment, bin-wrappers seem to have stabilized so we'll punt on this, and circle back to it later. ## How was it tested? compiles - [ ] testscripts should pass
1 parent 18bbfe9 commit f65e037

18 files changed

+38
-174
lines changed

devbox.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type Devbox interface {
2121
// Adding duplicate packages is a no-op.
2222
Add(ctx context.Context, pkgs ...string) error
2323
Config() *devconfig.Config
24-
ExportHook(shellName string) (string, error)
2524
ProjectDir() string
2625
// Generate creates the directory of Nix files and the Dockerfile that define
2726
// the devbox environment.

internal/boxcli/export.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

internal/boxcli/featureflag/prompt_hook.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

internal/boxcli/global.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func globalCmd() *cobra.Command {
2626
}
2727

2828
addCommandAndHideConfigFlag(globalCmd, addCmd())
29-
addCommandAndHideConfigFlag(globalCmd, hookCmd())
3029
addCommandAndHideConfigFlag(globalCmd, installCmd())
3130
addCommandAndHideConfigFlag(globalCmd, pathCmd())
3231
addCommandAndHideConfigFlag(globalCmd, pullCmd())
@@ -113,10 +112,7 @@ func setGlobalConfigForDelegatedCommands(
113112
}
114113

115114
func ensureGlobalEnvEnabled(cmd *cobra.Command, args []string) error {
116-
// Skip checking this for shellenv and hook sub-commands of devbox global
117-
// since these commands are what will enable the global environment when
118-
// invoked from the user's shellrc
119-
if cmd.Name() == "shellenv" || cmd.Name() == "hook" {
115+
if cmd.Name() == "shellenv" {
120116
return nil
121117
}
122118
path, err := ensureGlobalConfig(cmd)

internal/boxcli/hook.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

internal/boxcli/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ func RootCmd() *cobra.Command {
5353
command.AddCommand(authCmd())
5454
}
5555
command.AddCommand(createCmd())
56-
command.AddCommand(exportCmd())
5756
command.AddCommand(generateCmd())
5857
command.AddCommand(globalCmd())
59-
command.AddCommand(hookCmd())
6058
command.AddCommand(infoCmd())
6159
command.AddCommand(initCmd())
6260
command.AddCommand(installCmd())

internal/boxcli/shellenv.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ func shellEnvCmd() *cobra.Command {
3636
},
3737
}
3838

39-
registerShellEnvFlags(command, &flags)
40-
command.AddCommand(shellEnvOnlyPathWithoutWrappersCmd())
41-
42-
return command
43-
}
44-
45-
func registerShellEnvFlags(command *cobra.Command, flags *shellEnvCmdFlags) {
46-
4739
command.Flags().BoolVar(
4840
&flags.runInitHook, "init-hook", false, "runs init hook after exporting shell environment")
4941
command.Flags().BoolVar(
@@ -53,6 +45,10 @@ func registerShellEnvFlags(command *cobra.Command, flags *shellEnvCmdFlags) {
5345
&flags.pure, "pure", false, "If this flag is specified, devbox creates an isolated environment inheriting almost no variables from the current environment. A few variables, in particular HOME, USER and DISPLAY, are retained.")
5446

5547
flags.config.register(command)
48+
49+
command.AddCommand(shellEnvOnlyPathWithoutWrappersCmd())
50+
51+
return command
5652
}
5753

5854
func shellEnvFunc(cmd *cobra.Command, flags shellEnvCmdFlags) (string, error) {

internal/impl/devbox.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -817,23 +817,21 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
817817

818818
addEnvIfNotPreviouslySetByDevbox(env, pluginEnv)
819819

820-
envPaths := []string{}
821-
if !featureflag.PromptHook.Enabled() {
820+
env["PATH"] = JoinPathLists(
822821
// Prepend the bin-wrappers directory to the PATH. This ensures that
823822
// bin-wrappers execute before the unwrapped binaries.
824-
envPaths = append(envPaths, filepath.Join(d.projectDir, plugin.WrapperBinPath))
825-
}
826-
// Adding profile bin path is a temporary hack. Some packages .e.g. curl
827-
// don't export the correct bin in the package, instead they export
828-
// as a propagated build input. This can be fixed in 2 ways:
829-
// * have NixBins() recursively look for bins in propagated build inputs
830-
// * Turn existing planners into flakes (i.e. php, haskell) and use the bins
831-
// in the profile.
832-
// Landau: I prefer option 2 because it doesn't require us to re-implement
833-
// nix recursive bin lookup.
834-
envPaths = append(envPaths, nix.ProfileBinPath(d.projectDir), env["PATH"])
835-
836-
env["PATH"] = JoinPathLists(envPaths...)
823+
filepath.Join(d.projectDir, plugin.WrapperBinPath),
824+
// Adding profile bin path is a temporary hack. Some packages .e.g. curl
825+
// don't export the correct bin in the package, instead they export
826+
// as a propagated build input. This can be fixed in 2 ways:
827+
// * have NixBins() recursively look for bins in propagated build inputs
828+
// * Turn existing planners into flakes (i.e. php, haskell) and use the bins
829+
// in the profile.
830+
// Landau: I prefer option 2 because it doesn't require us to re-implement
831+
// nix recursive bin lookup.
832+
nix.ProfileBinPath(d.projectDir),
833+
env["PATH"],
834+
)
837835

838836
// Include env variables in devbox.json
839837
configEnv := d.configEnvs(env)

internal/impl/generate/devcontainer_util.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"runtime/trace"
1818
"strings"
1919

20-
"go.jetpack.io/devbox/internal/boxcli/featureflag"
2120
"go.jetpack.io/devbox/internal/debug"
2221
)
2322

@@ -160,9 +159,5 @@ func getDevcontainerContent(pkgs []string) *devcontainerObject {
160159
func EnvrcContent(w io.Writer) error {
161160
tmplName := "envrcContent.tmpl"
162161
t := template.Must(template.ParseFS(tmplFS, "tmpl/"+tmplName))
163-
return t.Execute(w, struct {
164-
PromptHookEnabled bool
165-
}{
166-
PromptHookEnabled: featureflag.PromptHook.Enabled(),
167-
})
162+
return t.Execute(w, nil)
168163
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use_devbox() {
22
watch_file devbox.json
3-
{{ if .PromptHookEnabled }}
4-
eval "$(devbox export --init-hook --install)"
5-
{{ else }}
63
eval "$(devbox shellenv --init-hook --install)"
7-
{{ end }}
84
}
95
use devbox

internal/impl/shell.go

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717

1818
"github.com/alessio/shellescape"
1919
"github.com/pkg/errors"
20-
"go.jetpack.io/devbox/internal/boxcli/featureflag"
21-
"go.jetpack.io/devbox/internal/shenv"
2220
"go.jetpack.io/devbox/internal/telemetry"
2321

2422
"go.jetpack.io/devbox/internal/debug"
@@ -326,25 +324,21 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
326324
}
327325

328326
err = tmpl.Execute(shellrcf, struct {
329-
ProjectDir string
330-
OriginalInit string
331-
OriginalInitPath string
332-
HooksFilePath string
333-
ShellName string
334-
ShellStartTime string
335-
HistoryFile string
336-
ExportEnv string
337-
PromptHookEnabled bool
327+
ProjectDir string
328+
OriginalInit string
329+
OriginalInitPath string
330+
HooksFilePath string
331+
ShellStartTime string
332+
HistoryFile string
333+
ExportEnv string
338334
}{
339-
ProjectDir: s.projectDir,
340-
OriginalInit: string(bytes.TrimSpace(userShellrc)),
341-
OriginalInitPath: s.userShellrcPath,
342-
HooksFilePath: s.hooksFilePath,
343-
ShellName: string(s.name),
344-
ShellStartTime: telemetry.FormatShellStart(s.shellStartTime),
345-
HistoryFile: strings.TrimSpace(s.historyFile),
346-
ExportEnv: exportify(s.env),
347-
PromptHookEnabled: featureflag.PromptHook.Enabled(),
335+
ProjectDir: s.projectDir,
336+
OriginalInit: string(bytes.TrimSpace(userShellrc)),
337+
OriginalInitPath: s.userShellrcPath,
338+
HooksFilePath: s.hooksFilePath,
339+
ShellStartTime: telemetry.FormatShellStart(s.shellStartTime),
340+
HistoryFile: strings.TrimSpace(s.historyFile),
341+
ExportEnv: exportify(s.env),
348342
})
349343
if err != nil {
350344
return "", fmt.Errorf("execute shellrc template: %v", err)
@@ -427,23 +421,3 @@ func filterPathList(pathList string, keep func(string) bool) string {
427421
}
428422
return strings.Join(filtered, string(filepath.ListSeparator))
429423
}
430-
431-
func (d *Devbox) ExportHook(shellName string) (string, error) {
432-
if !featureflag.PromptHook.Enabled() {
433-
return "", nil
434-
}
435-
436-
// TODO: use a single common "enum" for both shenv and DevboxShell
437-
hookTemplate, err := shenv.DetectShell(shellName).Hook()
438-
if err != nil {
439-
return "", err
440-
}
441-
442-
var buf bytes.Buffer
443-
err = template.Must(template.New("hookTemplate").Parse(hookTemplate)).
444-
Execute(&buf, struct{ ProjectDir string }{ProjectDir: d.projectDir})
445-
if err != nil {
446-
return "", errors.WithStack(err)
447-
}
448-
return buf.String(), nil
449-
}

internal/impl/shellrc.tmpl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,3 @@ if ! type refresh >/dev/null 2>&1; then
6767
alias refresh='eval $(devbox shellenv)'
6868
export DEVBOX_REFRESH_ALIAS="refresh"
6969
fi
70-
71-
{{- if .PromptHookEnabled }}
72-
# Ensure devbox shellenv is evaluated
73-
# TODO savil: how do I wrap ProjectDir in quotes?
74-
eval "$(devbox hook {{ .ShellName }} -c {{ .ProjectDir }})"
75-
{{ end }}

internal/impl/shellrc_fish.tmpl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,3 @@ if not type refresh >/dev/null 2>&1
7070
alias refresh='eval (devbox shellenv | string collect)'
7171
export DEVBOX_REFRESH_ALIAS="refresh"
7272
end
73-
74-
{{ if .PromptHookEnabled }}
75-
# Ensure devbox shellenv is evaluated
76-
devbox hook fish -c "{{ .ProjectDir }}" | source
77-
{{ end }}

internal/shenv/shell_bash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const bashHook = `
1111
_devbox_hook() {
1212
local previous_exit_status=$?;
1313
trap -- '' SIGINT;
14-
eval "$(devbox export --config {{ .ProjectDir }})";
14+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
1515
trap - SIGINT;
1616
return $previous_exit_status;
1717
};

internal/shenv/shell_fish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Fish Shell = fish{}
1212

1313
const fishHook = `
1414
function __devbox_shellenv_eval --on-event fish_prompt;
15-
devbox export --config {{ .ProjectDir }} | source;
15+
devbox shellenv --config {{ .ProjectDir }} | source;
1616
end;
1717
`
1818

internal/shenv/shell_ksh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var Ksh Shell = ksh{}
88
// um, this is ChatGPT writing it. I need to verify and test
99
const kshHook = `
1010
_devbox_hook() {
11-
eval "$(devbox export --config {{ .ProjectDir }})";
11+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
1212
}
1313
if [[ "$(typeset -f precmd)" != *"_devbox_hook"* ]]; then
1414
function precmd {

internal/shenv/shell_posix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const posixHook = `
1212
_devbox_hook() {
1313
local previous_exit_status=$?
1414
trap : INT
15-
eval "$(devbox export --config {{ .ProjectDir }})"
15+
eval "$(devbox shellenv --config {{ .ProjectDir }})"
1616
trap - INT
1717
return $previous_exit_status
1818
}

internal/shenv/shell_zsh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var Zsh Shell = zsh{}
99
const zshHook = `
1010
_devbox_hook() {
1111
trap -- '' SIGINT;
12-
eval "$(devbox export --config {{ .ProjectDir }})";
12+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
1313
trap - SIGINT;
1414
}
1515
typeset -ag precmd_functions;

0 commit comments

Comments
 (0)