From 1b20ee88c9f73c9c7c9988d543604b4e73408f43 Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Wed, 7 Aug 2024 19:59:03 +0000 Subject: [PATCH] Add better error from nvm --- internal/manager/serverless/native.go | 21 ++++++++++++++++++--- internal/manager/serverless/serverless.go | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/manager/serverless/native.go b/internal/manager/serverless/native.go index 79890d6e..da001a86 100644 --- a/internal/manager/serverless/native.go +++ b/internal/manager/serverless/native.go @@ -1,6 +1,7 @@ package serverless import ( + "bytes" "fmt" "io" "os" @@ -49,15 +50,28 @@ func (sls *Manager) nvm(w io.Writer, command string) error { return err } + logrus.Debugf("Running: bash -c source %s/nvm.sh && nvm install %s && %s", nvmDir, sls.App.NodeVersion, command) cmd := exec.Command("bash", "-c", fmt.Sprintf("source %s/nvm.sh && nvm install %s && %s", nvmDir, sls.App.NodeVersion, command), ) - return term.New( + // Capture stderr in a buffer + var stderr bytes.Buffer + cmd.Stderr = &stderr + + t := term.New( term.WithDir(sls.App.Path), term.WithStdout(w), - term.WithStderr(w), - ).InteractiveRun(cmd) + term.WithStderr(&stderr), + ) + + err = t.InteractiveRun(cmd) + if err != nil { + // Return the error along with stderr output + return fmt.Errorf("command failed with error: %w, stderr: %s", err, stderr.String()) + } + + return nil } func (sls *Manager) readNvmrc() error { @@ -70,6 +84,7 @@ func (sls *Manager) readNvmrc() error { } sls.App.NodeVersion = strings.TrimSpace(string(file)) } + return nil } diff --git a/internal/manager/serverless/serverless.go b/internal/manager/serverless/serverless.go index ce786539..d98c2dda 100644 --- a/internal/manager/serverless/serverless.go +++ b/internal/manager/serverless/serverless.go @@ -29,7 +29,7 @@ func (sls *Manager) Nvm(ui terminal.UI, command []string) error { err := sls.nvm(s.TermOutput(), strings.Join(command, " ")) if err != nil { - return fmt.Errorf("can't run nvm: %w", err) + return fmt.Errorf("can't run nvm: %s ", err) } s.Done()