Skip to content

Commit e70e25e

Browse files
authored
devbox: handle paths with spaces (#221)
## Summary The current command for nix-env starts as a string and then gets split by spaces to form the args slice. This doesn't handle paths with spaces, so change it to just create a slice directly instead. Also improve the error message when `nix-env` fails to give more info instead of just the exit code. Finally, unexport `ApplyDevNixDerivation` because isn't used anywhere else. ## How was it tested? Ran `devbox shell` from a directory with spaces in the name.
1 parent 93d61d0 commit e70e25e

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

devbox.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
347347
fmt.Fprintf(d.writer, "%s nix packages. This may take a while...", installingVerb)
348348

349349
// We need to re-install the packages
350-
if err := d.ApplyDevNixDerivation(); err != nil {
350+
if err := d.applyDevNixDerivation(); err != nil {
351351
fmt.Println()
352-
return err
352+
return errors.Wrap(err, "apply Nix derivation")
353353
}
354354
fmt.Println("done.")
355355

@@ -376,26 +376,32 @@ func (d *Devbox) printPackageUpdateMessage(mode installMode, pkgs []string) erro
376376
return nil
377377
}
378378

379-
// ApplyDevNixDerivation ensures the local profile has exactly the packages in the development.nix file
380-
//
381-
// Will move to a store interface/package
382-
func (d *Devbox) ApplyDevNixDerivation() error {
379+
// applyDevNixDerivation installs or uninstalls packages to or from this
380+
// devbox's Nix profile so that it matches what's in development.nix.
381+
func (d *Devbox) applyDevNixDerivation() error {
383382
profileDir, err := d.profileDir()
384383
if err != nil {
385384
return err
386385
}
387386

388-
cmdStr := fmt.Sprintf(
389-
"--profile %s --install -f %s/.devbox/gen/development.nix",
390-
profileDir,
391-
d.srcDir,
387+
cmd := exec.Command("nix-env",
388+
"--profile", profileDir,
389+
"--install",
390+
"-f", filepath.Join(d.srcDir, ".devbox/gen/development.nix"),
392391
)
393-
cmdParts := strings.Split(cmdStr, " ")
394-
execCmd := exec.Command("nix-env", cmdParts...)
395392

396-
debug.Log("running command: %s\n", execCmd.Args)
397-
err = execCmd.Run()
398-
return errors.WithStack(err)
393+
debug.Log("Running command: %s\n", cmd.Args)
394+
_, err = cmd.Output()
395+
396+
var exitErr *exec.ExitError
397+
if errors.As(err, &exitErr) {
398+
return errors.Errorf("running command %s: exit status %d with command output: %s",
399+
cmd, exitErr.ExitCode(), string(exitErr.Stderr))
400+
}
401+
if err != nil {
402+
return errors.Errorf("running command %s: %v", cmd, err)
403+
}
404+
return nil
399405
}
400406

401407
// Move to a utility package?

0 commit comments

Comments
 (0)