Skip to content

[wrappers] Improve wrappers in multi-project env #1067

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 2 commits into from
Jun 1, 2023
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
13 changes: 9 additions & 4 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ func (d *Devbox) ShellEnvHash(ctx context.Context) (string, error) {
return "", err
}

return envs[devboxShellEnvHashVarName], nil
return envs[d.ShellEnvHashKey()], nil
}

func (d *Devbox) ShellEnvHashKey() string {
// Don't make this a const so we don't use it by itself accidentally
return "__DEVBOX_SHELLENV_HASH_" + d.projectDirHash()
}

func (d *Devbox) Info(pkg string, markdown bool) error {
Expand Down Expand Up @@ -827,7 +832,7 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
os.Getenv("XDG_DATA_DIRS"),
)

return env, addHashToEnv(env)
return env, d.addHashToEnv(env)
}

var nixEnvCache map[string]string
Expand Down Expand Up @@ -1113,10 +1118,10 @@ func (d *Devbox) projectDirHash() string {
return hash
}

func addHashToEnv(env map[string]string) error {
func (d *Devbox) addHashToEnv(env map[string]string) error {
hash, err := cuecfg.Hash(env)
if err == nil {
env[devboxShellEnvHashVarName] = hash
env[d.ShellEnvHashKey()] = hash
}
return err
}
1 change: 0 additions & 1 deletion internal/impl/envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

const devboxSetPrefix = "__DEVBOX_SET_"
const devboxShellEnvHashVarName = "__DEVBOX_SHELLENV_HASH"

func mapToPairs(m map[string]string) []string {
pairs := []string{}
Expand Down
9 changes: 5 additions & 4 deletions internal/wrapnix/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type devboxer interface {
NixBins(ctx context.Context) ([]string, error)
ShellEnvHash(ctx context.Context) (string, error)
ShellEnvHashKey() string
ProjectDir() string
Services() (services.Services, error)
}
Expand Down Expand Up @@ -53,20 +54,20 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
bashPath := cmdutil.GetPathOrDefault("bash", "/bin/bash")
for _, service := range services {
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: service.Start,
Env: service.Env,
ProjectDir: devbox.ProjectDir(),
ShellEnvHash: shellEnvHash,
destPath: filepath.Join(destPath, service.StartName()),
}); err != nil {
return err
}
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: service.Stop,
Env: service.Env,
ProjectDir: devbox.ProjectDir(),
ShellEnvHash: shellEnvHash,
destPath: filepath.Join(destPath, service.StopName()),
}); err != nil {
Expand All @@ -81,9 +82,9 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {

for _, bin := range bins {
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: bin,
ProjectDir: devbox.ProjectDir(),
ShellEnvHash: shellEnvHash,
destPath: filepath.Join(destPath, filepath.Base(bin)),
}); err != nil {
Expand All @@ -95,10 +96,10 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
}

type createWrapperArgs struct {
devboxer
Copy link
Collaborator

Choose a reason for hiding this comment

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

where is this used?

Copy link
Collaborator

Choose a reason for hiding this comment

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

as in, I see you removed ProjectDir field and added devboxer field
I also see that you set these fields above.

But I don't see any callsites being updated that used to use createWrapperArgs.ProjectDir but now use createWrapperArgs.ProjectDir()

Copy link
Collaborator

Choose a reason for hiding this comment

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

ahh, its cause it is passed into go-templates and in go-templates the .ProjectDir can be used for fields and methods?

I hate go-templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially wanted to use it for ShellEnvHash function as well but that requires a context :(

BashPath string
Command string
Env map[string]string
ProjectDir string
ShellEnvHash string

destPath string
Expand Down
4 changes: 2 additions & 2 deletions internal/wrapnix/wrapper.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ fi
{{- end }}

{{/*
We use __DEVBOX_SHELLENV_HASH to avoid re-sourcing shellenv. Since wrappers
We use ShellEnvHashKey to avoid re-sourcing shellenv. Since wrappers
call other wrappers and potentially modify the environment, we don't want the
environment to get re-written.

DO_NOT_TRACK=1 can be removed once we optimize segment to queue events.
*/ -}}

if [[ "$__DEVBOX_SHELLENV_HASH" != "{{ .ShellEnvHash }}" ]]; then
if [[ "${{ .ShellEnvHashKey }}" != "{{ .ShellEnvHash }}" ]]; then
eval "$(DO_NOT_TRACK=1 devbox shellenv -c {{ .ProjectDir }})"
fi

Expand Down