Skip to content

[bin wrappers] Fixes: don't export PATH, and eval the symlink of the binary #1330

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 6 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 29 additions & 13 deletions internal/wrapnix/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
if err != nil {
return err
}
devboxSymlinkDir := ""
if devboxSymlinkPath != "" { // may be empty if not possible to create symlink
devboxSymlinkDir = filepath.Dir(devboxSymlinkPath)
}

for _, bin := range bins {
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: bin,
ShellEnvHash: shellEnvHash,
DevboxSymlinkDir: filepath.Dir(devboxSymlinkPath),
destPath: filepath.Join(destPath, filepath.Base(bin)),
devboxer: devbox,
BashPath: bashPath,
Command: bin,
ShellEnvHash: shellEnvHash,
DevboxSymlinkDirIfExists: devboxSymlinkDir,
destPath: filepath.Join(destPath, filepath.Base(bin)),
}); err != nil {
return errors.WithStack(err)
}
Expand All @@ -72,7 +76,8 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
return createSymlinksForSupportDirs(devbox.ProjectDir())
}

// CreateDevboxSymlink creates a symlink to the devbox binary
// CreateDevboxSymlink creates a symlink to the devbox binary. It may return an
// empty string if it could not create the symlink.
//
// Needed because:
//
Expand All @@ -89,22 +94,33 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
// So, the bin-wrappers need to use a symlink to the latest devbox binary. This
// symlink is updated when devbox is updated.
func CreateDevboxSymlink() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

change definition of this function to return sym link dir.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd prefer not doing that, since I'd expect a function named CreateDevboxSymlink to return the path to the symlink, rather than its directory. Changing the name to CreateDevboxSymlinkAndGetDir feels verbose. I think its okay for the handling logic to do filepath.Dir

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

but yeah, let me know if you feel strongly, and I can make the change.

// Get the symlink path; create the symlink directory if it doesn't exist.
curDir := xdg.CacheSubpath(filepath.Join("devbox", "bin", "current"))
if err := fileutil.EnsureDirExists(curDir, 0755, false /*chmod*/); err != nil {
return "", err
}
currentDevboxSymlinkPath := filepath.Join(curDir, "devbox")

devboxBinaryPath, err := os.Executable()
// Get the path to the devbox binary.
execPath, err := os.Executable()
if err != nil {
return "", errors.WithStack(err)
}
devboxBinaryPath, evalSymlinkErr := filepath.EvalSymlinks(execPath)
// we check the error below, because we always want to remove the symlink

// We will always re-create this symlink to ensure correctness.
if err := os.Remove(currentDevboxSymlinkPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return "", errors.WithStack(err)
}

if evalSymlinkErr != nil {
// This may return an error due to symlink loops. In that case, we
// return an empty string, and the bin-wrapper should handle it accordingly.
// nolint:nilerr
return "", nil
Copy link
Contributor

Choose a reason for hiding this comment

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

return symlink dir

}

// Don't return error if error is os.ErrExist to protect against race conditions.
if err := os.Symlink(devboxBinaryPath, currentDevboxSymlinkPath); err != nil && !errors.Is(err, os.ErrExist) {
return "", errors.WithStack(err)
Expand All @@ -115,11 +131,11 @@ func CreateDevboxSymlink() (string, error) {

type createWrapperArgs struct {
devboxer
BashPath string
Command string
ShellEnvHash string
DevboxSymlinkDir string
destPath string
BashPath string
Command string
ShellEnvHash string
DevboxSymlinkDirIfExists string
destPath string
}

func createWrapper(args *createWrapperArgs) error {
Expand Down
4 changes: 3 additions & 1 deletion internal/wrapnix/wrapper.sh.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!{{ .BashPath }}

export PATH="{{ .DevboxSymlinkDir }}:$PATH"
{{ if ne .DevboxSymlinkDirIfExists "" }}
PATH="{{ .DevboxSymlinkDirIfExists }}:$PATH"
{{ end }}

{{/*
# If env variable has never been set by devbox we set it, but also
Expand Down