Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixed compiler output capture in JSON mode #2078

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
cmaglie merged 4 commits into arduino:master from cmaglie:fix_compiler_output
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Do not alter ctx.Stderr/Stdout in ExecCommand
Some instances of ctx.Stdout may now contains nil since it's no more
altered during ExecCommand.
  • Loading branch information
cmaglie committed Mar 10, 2023
commit 816a68172dcd0efceea8032ba325d145fa02fb93
2 changes: 1 addition & 1 deletion legacy/builder/builder.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
}

// Output arduino-preprocessed source
ctx.Stdout.Write([]byte(ctx.Source))
ctx.WriteStdout([]byte(ctx.Source))
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/preprocess_sketch.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ func (s *OutputCodeCompletions) Run(ctx *types.Context) error {
// we assume it is a json, let's make it compliant at least
ctx.CodeCompletions = "[]"
}
fmt.Fprintln(ctx.Stdout, ctx.CodeCompletions)
ctx.WriteStdout([]byte(ctx.CodeCompletions))
return nil
}
23 changes: 12 additions & 11 deletions legacy/builder/utils/utils.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -184,29 +184,30 @@ const (
)

func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int) ([]byte, []byte, error) {
Copy link
Contributor

@Bikappa Bikappa Mar 2, 2023

Choose a reason for hiding this comment

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

I got inspired by this refactoring, but do what you want with it. Since it took longer than expected it might not be that good

func getWriter(option int, isVerbose bool, candidate io.Writer) io.Writer {
	getCandidateOrStdout := func() io.Writer {
		if candidate != nil {
			return candidate
		}
		return os.Stdout
	}
	mapOptionToWriterFunc := map[int]func() io.Writer{
		Ignore: func() io.Writer {
			return io.Discard
		},
		Show: getCandidateOrStdout,
		ShowIfVerbose: func() io.Writer {
			if !isVerbose {
				return io.Discard
			}
			return getCandidateOrStdout()
		},
		Capture: func() io.Writer {
			return &bytes.Buffer{}
		},
	}
	return mapOptionToWriterFunc[option]()
}
func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int) ([]byte, []byte, error) {
	stdoutWriter := getWriter(stdout, ctx.Verbose, ctx.Stdout)
	stderrWriter := getWriter(stderr, ctx.Verbose, ctx.Stderr)
	return ExecCommandWithWriters(ctx, command, stdoutWriter, stderrWriter)
}
func ExecCommandWithWriters(ctx *types.Context, command *exec.Cmd, stdout io.Writer, stderr io.Writer) ([]byte, []byte, error) {
	if ctx.Verbose {
		ctx.Info(PrintableCommand(command.Args))
	}
	command.Stdout = stdout
	command.Stderr = stderr
	err := command.Start()
	if err != nil {
		return nil, nil, errors.WithStack(err)
	}
	err = command.Wait()
	var outbytes, errbytes []byte
	if buf, ok := command.Stdout.(*bytes.Buffer); ok {
		outbytes = buf.Bytes()
	}
	if buf, ok := command.Stderr.(*bytes.Buffer); ok {
		errbytes = buf.Bytes()
	}
	return outbytes, errbytes, errors.WithStack(err)
}```

Copy link
Member Author

Choose a reason for hiding this comment

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

This is something to keep in mind when this subroutine will be merged with executils and removed from legacy.

if ctx.Stdout == nil {
ctx.Stdout = os.Stdout
}
if ctx.Stderr == nil {
ctx.Stderr = os.Stderr
}

if ctx.Verbose {
ctx.Info(PrintableCommand(command.Args))
}

if stdout == Capture {
buffer := &bytes.Buffer{}
command.Stdout = buffer
} else if stdout == Show || stdout == ShowIfVerbose && ctx.Verbose {
command.Stdout = ctx.Stdout
} else if stdout == Show || (stdout == ShowIfVerbose && ctx.Verbose) {
if ctx.Stdout != nil {
command.Stdout = ctx.Stdout
} else {
command.Stdout = os.Stdout
}
}

if stderr == Capture {
buffer := &bytes.Buffer{}
command.Stderr = buffer
} else if stderr == Show || stderr == ShowIfVerbose && ctx.Verbose {
command.Stderr = ctx.Stderr
} else if stderr == Show || (stderr == ShowIfVerbose && ctx.Verbose) {
if ctx.Stderr != nil {
command.Stderr = ctx.Stderr
} else {
command.Stderr = os.Stderr
}
}

err := command.Start()
Expand Down

AltStyle によって変換されたページ (->オリジナル) /