Skip to content
Open
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
20 changes: 19 additions & 1 deletion internal/devbox/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"os"
"strconv"
"text/tabwriter"

Expand Down Expand Up @@ -271,7 +272,24 @@ func (d *Devbox) StartProcessManager(
// defaults for the `devbox services` scenario.
func (d *Devbox) runDevboxServicesScript(ctx context.Context, cmdArgs []string) error {
cmdArgs = append([]string{"services"}, cmdArgs...)
return d.RunScript(ctx, devopt.EnvOptions{}, "devbox", cmdArgs)
return d.RunScript(ctx, devopt.EnvOptions{}, devboxBinaryForSelfInvocation(), cmdArgs)
}

// devboxBinaryForSelfInvocation returns a shell-quoted reference to the
// currently running devbox binary. `devbox services ...` re-invokes devbox
// inside the computed environment, and using the actual executable path (rather
// than a hardcoded "devbox") ensures this works even when the binary has been
// installed or renamed to something other than "devbox". If the executable path
// can't be determined, it falls back to "devbox", relying on a PATH lookup as
// before. See https://github.com/jetify-com/devbox/issues/1321.
func devboxBinaryForSelfInvocation() string {
exe, err := os.Executable()
if err != nil {
return "devbox"
}
// Quote the path so it survives being eval'd by the generated run script
// (e.g. when the path contains spaces).
return strconv.Quote(exe)
}

func (d *Devbox) ShowProcessComposePort(ctx context.Context, writer io.Writer) error {
Expand Down
32 changes: 32 additions & 0 deletions internal/devbox/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

package devbox

import (
"os"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestDevboxBinaryForSelfInvocation verifies that `devbox services ...`
// re-invokes devbox using the path to the currently running binary rather than
// a hardcoded "devbox" that must be resolvable on PATH. This is a regression
// guard for https://github.com/jetify-com/devbox/issues/1321, where services
// commands broke whenever the binary was installed or renamed to something
// other than "devbox".
func TestDevboxBinaryForSelfInvocation(t *testing.T) {
exe, err := os.Executable()
require.NoError(t, err)

got := devboxBinaryForSelfInvocation()

// It should reference the actual running binary (shell-quoted), not the
// literal command name "devbox".
assert.Equal(t, strconv.Quote(exe), got)
assert.NotEqual(t, "devbox", got)
assert.NotEqual(t, strconv.Quote("devbox"), got)
}
Loading