Skip to content
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

Sometimes complete terraform output not being shown when there are errors #835

Open
fmartingr opened this issue Oct 21, 2024 · 0 comments
Open

Comments

@fmartingr
Copy link
Contributor

          Hmm .. ok I understand the bug. https://pkg.go.dev/os/exec#Cmd.StdoutPipe

Cmd.Wait will close the pipe after seeing the command exit, so most callers need not close the pipe themselves. It is thus incorrect to call Wait before all reads from the pipe have completed.

So using a scanner to read from the pipe is error-prone and probably why it fails a lot of times. The better way is to directly set the StdErr/Stdout fields with a custom io.Writer.

type cmdLogger struct {
}

func (*cmdLogger) Write(in []byte) (int, error) {
	mlog.Info(string(in))
	return len(in), nil
}

and then

cmd.Stdout = &cmdLogger
cmd.Stderr = cmd.Stdout

if err := cmd.Start(); err != nil {
return err
}

if err := cmd.Wait(); err != nil {
return err
}

Having the same writer for stdout and stderr is handled properly in the stdlib:

// If Stdout and Stderr are the same writer, and have a type that can
// be compared with ==, at most one goroutine at a time will call Write.

@fmartingr - can we try with something like this?

Originally posted by @agnivade in #833 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant