Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,21 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
}

lock.SetIgnoreShellMismatch(true)
env, err := d.ensureStateIsUpToDateAndComputeEnv(ctx)
if err != nil {
return err

var env map[string]string
if d.IsEnvEnabled() {
// Skip ensureStateIsUpToDate if we are already in a shell of this devbox-project
env = envir.PairsToMap(os.Environ())

// We set this to ensure that init-hooks do NOT re-run. They would have
// run when initializing the Devbox Environment in the current shell.
env[d.SkipInitHookEnvName()] = "true"
} else {
var err error
env, err = d.ensureStateIsUpToDateAndComputeEnv(ctx)
if err != nil {
return err
}
}

// Used to determine whether we're inside a shell (e.g. to prevent shell inception)
Expand Down
4 changes: 4 additions & 0 deletions internal/devbox/envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ func (d *Devbox) IsEnvEnabled() bool {
pathStack := envpath.Stack(fakeEnv, envir.PairsToMap(os.Environ()))
return pathStack.Has(d.ProjectDirHash())
}

func (d *Devbox) SkipInitHookEnvName() string {
return "__DEVBOX_SKIP_INIT_HOOK_" + d.ProjectDirHash()
}
43 changes: 7 additions & 36 deletions internal/shellgen/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package shellgen

import (
"bytes"
_ "embed"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"text/template"

_ "embed"

"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/featureflag"
"go.jetpack.io/devbox/internal/debug"
Expand All @@ -24,26 +23,17 @@ import (
var scriptWrapperTmplString string
var scriptWrapperTmpl = template.Must(template.New("script-wrapper").Parse(scriptWrapperTmplString))

//go:embed tmpl/init-hook-wrapper.tmpl
var initHookWrapperString string
var initHookWrapperTmpl = template.Must(template.New("init-hook-wrapper").Parse(initHookWrapperString))

const scriptsDir = ".devbox/gen/scripts"

// HooksFilename is the name of the file that contains a wrapper of the
// project's init-hooks and plugin hooks
const (
HooksFilename = ".hooks"
rawHooksFilename = ".raw-hooks"
)
const HooksFilename = ".hooks"

type devboxer interface {
Config() *devconfig.Config
Lockfile() *lock.File
InstallablePackages() []*devpkg.Package
PluginManager() *plugin.Manager
ProjectDir() string
ProjectDirHash() string
SkipInitHookEnvName() string
}

// WriteScriptsToFiles writes scripts defined in devbox.json into files inside .devbox/gen/scripts.
Expand All @@ -68,12 +58,6 @@ func WriteScriptsToFiles(devbox devboxer) error {
if err != nil {
return errors.WithStack(err)
}
written[rawHooksFilename] = struct{}{}

err = writeInitHookWrapperFile(devbox)
if err != nil {
return errors.WithStack(err)
}
written[HooksFilename] = struct{}{}

// Write scripts to files.
Expand Down Expand Up @@ -104,7 +88,7 @@ func WriteScriptsToFiles(devbox devboxer) error {
}

func writeRawInitHookFile(devbox devboxer, body string) (err error) {
script, err := createScriptFile(devbox, rawHooksFilename)
script, err := createScriptFile(devbox, HooksFilename)
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -114,19 +98,6 @@ func writeRawInitHookFile(devbox devboxer, body string) (err error) {
return errors.WithStack(err)
}

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

return initHookWrapperTmpl.Execute(script, map[string]string{
"InitHookHash": "__DEVBOX_INIT_HOOK_" + devbox.ProjectDirHash(),
"RawHooksFile": ScriptPath(devbox.ProjectDir(), rawHooksFilename),
})
}

func WriteScriptFile(devbox devboxer, name, body string) (err error) {
script, err := createScriptFile(devbox, name)
if err != nil {
Expand Down Expand Up @@ -168,9 +139,9 @@ func ScriptPath(projectDir, scriptName string) string {
func ScriptBody(d devboxer, body string) (string, error) {
var buf bytes.Buffer
err := scriptWrapperTmpl.Execute(&buf, map[string]string{
"Body": body,
"InitHookHash": "__DEVBOX_INIT_HOOK_" + d.ProjectDirHash(),
"InitHookPath": ScriptPath(d.ProjectDir(), HooksFilename),
"Body": body,
"SkipInitHookHash": d.SkipInitHookEnvName(),
"InitHookPath": ScriptPath(d.ProjectDir(), HooksFilename),
})
if err != nil {
return "", errors.WithStack(err)
Expand Down
9 changes: 0 additions & 9 deletions internal/shellgen/tmpl/init-hook-wrapper.tmpl

This file was deleted.

3 changes: 1 addition & 2 deletions internal/shellgen/tmpl/script-wrapper.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
scripts. (though users can run a fish script within their script)
*/ -}}

if [ -z "${{ .InitHookHash }}" ]; then
{{/* init hooks will export InitHookHash ensuring no recursive sourcing*/ -}}
if [ -z "${{ .SkipInitHookHash }}" ]; then
. {{ .InitHookPath }}
fi

Expand Down