Skip to content

[fish] Fix init hooks for fish #1741

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

Merged
merged 1 commit into from
Jan 23, 2024
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
1 change: 0 additions & 1 deletion internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ func (d *Devbox) Shell(ctx context.Context) error {
}

opts := []ShellOption{
WithHooksFilePath(shellgen.ScriptPath(d.ProjectDir(), shellgen.HooksFilename)),
WithHistoryFile(filepath.Join(d.projectDir, shellHistoryFile)),
WithProjectDir(d.projectDir),
WithEnvVariables(envs),
Expand Down
14 changes: 5 additions & 9 deletions internal/devbox/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/alessio/shellescape"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/shellgen"
"go.jetpack.io/devbox/internal/telemetry"

"go.jetpack.io/devbox/internal/debug"
Expand Down Expand Up @@ -59,8 +60,7 @@ type DevboxShell struct {
env map[string]string
userShellrcPath string

hooksFilePath string
historyFile string
historyFile string

// shellStartTime is the unix timestamp for when the command was invoked
shellStartTime time.Time
Expand Down Expand Up @@ -183,12 +183,6 @@ func WithHistoryFile(historyFile string) ShellOption {
}
}

func WithHooksFilePath(hooksFilePath string) ShellOption {
return func(s *DevboxShell) {
s.hooksFilePath = hooksFilePath
}
}

// TODO: Consider removing this once plugins add env vars directly to binaries via wrapper scripts.
func WithEnvVariables(envVariables map[string]string) ShellOption {
return func(s *DevboxShell) {
Expand Down Expand Up @@ -317,8 +311,10 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
}()

tmpl := shellrcTmpl
hooksFilePath := shellgen.ScriptPath(s.projectDir, shellgen.HooksFilename)
if s.name == shFish {
tmpl = fishrcTmpl
hooksFilePath = shellgen.ScriptPath(s.projectDir, shellgen.HooksFishFilename)
}

err = tmpl.Execute(shellrcf, struct {
Expand All @@ -337,7 +333,7 @@ func (s *DevboxShell) writeDevboxShellrc() (path string, err error) {
ProjectDir: s.projectDir,
OriginalInit: string(bytes.TrimSpace(userShellrc)),
OriginalInitPath: s.userShellrcPath,
HooksFilePath: s.hooksFilePath,
HooksFilePath: hooksFilePath,
ShellStartTime: telemetry.FormatShellStart(s.shellStartTime),
HistoryFile: strings.TrimSpace(s.historyFile),
ExportEnv: exportify(s.env),
Expand Down
3 changes: 1 addition & 2 deletions internal/devbox/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
s := &DevboxShell{
devbox: &Devbox{projectDir: projectDir},
env: test.env,
projectDir: "path/to/projectDir",
projectDir: "/path/to/projectDir",
userShellrcPath: test.shellrcPath,
hooksFilePath: test.hooksFilePath,
}
gotPath, err := s.writeDevboxShellrc()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/devbox/testdata/shellrc/basic/shellrc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export PS1="(devbox) $PS1"

# Run plugin and user init hooks from the devbox.json directory.
working_dir="$(pwd)"
cd "path/to/projectDir" || exit
cd "/path/to/projectDir" || exit

# Source the hooks file, which contains the project's init hooks and plugin hooks.
. /path/to/projectDir/.devbox/gen/scripts/.hooks.sh
Expand Down
2 changes: 1 addition & 1 deletion internal/devbox/testdata/shellrc/noshellrc/shellrc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export PS1="(devbox) $PS1"

# Run plugin and user init hooks from the devbox.json directory.
working_dir="$(pwd)"
cd "path/to/projectDir" || exit
cd "/path/to/projectDir" || exit

# Source the hooks file, which contains the project's init hooks and plugin hooks.
. /path/to/projectDir/.devbox/gen/scripts/.hooks.sh
Expand Down
24 changes: 16 additions & 8 deletions internal/shellgen/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ import (
//go:embed tmpl/init-hook.tmpl
var initHookTmpl string

//go:embed tmpl/init-hook-fish.tmpl
var initHookFishTmpl string

const scriptsDir = ".devbox/gen/scripts"

// HooksFilename is the name of the file that contains the project's init-hooks and plugin hooks
const HooksFilename = ".hooks"

// This is only used in shellrc_fish.tmpl. A bit of a hack, because scripts use
// sh instead of fish.
const HooksFishFilename = ".hooks.fish"

type devboxer interface {
Config() *devconfig.Config
Lockfile() *lock.File
Expand Down Expand Up @@ -62,11 +69,16 @@ func WriteScriptsToFiles(devbox devboxer) error {
}
hooks := strings.Join(append(pluginHooks, devbox.Config().InitHook().String()), "\n\n")
// always write it, even if there are no hooks, because scripts will source it.
err = writeInitHookFile(devbox, hooks)
err = writeInitHookFile(devbox, hooks, initHookTmpl, HooksFilename)
if err != nil {
return errors.WithStack(err)
}
written[HooksFilename] = struct{}{}
err = writeInitHookFile(devbox, hooks, initHookFishTmpl, HooksFishFilename)
if err != nil {
return errors.WithStack(err)
}
written[HooksFishFilename] = struct{}{}

// Write scripts to files.
for name, body := range devbox.Config().Scripts() {
Expand All @@ -91,25 +103,21 @@ func WriteScriptsToFiles(devbox devboxer) error {
return nil
}

func writeInitHookFile(devbox devboxer, body string) (err error) {
script, err := createScriptFile(devbox, HooksFilename)
func writeInitHookFile(devbox devboxer, body, tmpl, filename string) (err error) {
script, err := createScriptFile(devbox, filename)
if err != nil {
return errors.WithStack(err)
}
defer script.Close() // best effort: close file

t, err := template.New("init-hook-template").Parse(initHookTmpl)
t, err := template.New(filename).Parse(tmpl)
if err != nil {
return errors.WithStack(err)
}

return t.Execute(script, map[string]any{
"Body": body,
"InitHookHash": "__DEVBOX_INIT_HOOK_" + devbox.ProjectDirHash(),
// TODO put IsFish() in common place so we can call here and in devbox package
// without adding more stuff to interface
"IsFish": filepath.Base(os.Getenv("SHELL")) == "fish" ||
os.Getenv("FISH_VERSION") != "",
})
}

Expand Down
12 changes: 12 additions & 0 deletions internal/shellgen/tmpl/init-hook-fish.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{/* Fish version */ -}}
{{/* if hash is set, we're in recursive loop, just exit */ -}}

if set -q {{ .InitHookHash }}; then
return
end

export {{ .InitHookHash }}=true

{{ .Body }}

set -e {{ .InitHookHash }}
10 changes: 0 additions & 10 deletions internal/shellgen/tmpl/init-hook.tmpl
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@

{{/* if hash is set, we're in recursive loop, just exit */ -}}

{{ if .IsFish }}
if set -q {{ .InitHookHash }}; then
return
end
{{ else }}
if [ -n "${{ .InitHookHash }}" ]; then
return
fi
{{ end }}

export {{ .InitHookHash }}=true

{{ .Body }}

{{ if .IsFish }}
set -e {{ .InitHookHash }}
{{ else }}
unset {{ .InitHookHash }}
{{ end }}