Skip to content

Commit

Permalink
Improve compute build rust compilation error messaging.
Browse files Browse the repository at this point in the history
  • Loading branch information
phamann committed May 11, 2020
1 parent 0682968 commit 8bbbe4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// Toolchain abstracts a Compute@Edge source language toolchain.
type Toolchain interface {
Verify(out io.Writer) error
Build(out io.Writer) error
Build(out io.Writer, verbose bool) error
}

// GetToolchain returns a Toolchain for the provided language.
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {

progress.Step(fmt.Sprintf("Building package using %s toolchain...", lang))

if err := toolchain.Build(progress); err != nil {
if err := toolchain.Build(progress, c.Globals.Flag.Verbose); err != nil {
return err
}

Expand Down
37 changes: 29 additions & 8 deletions pkg/compute/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r Rust) Verify(out io.Writer) error {

// Build implments the Toolchain interface and attempts to compile the package
// Rust source to a Wasm binary.
func (r Rust) Build(out io.Writer) error {
func (r Rust) Build(out io.Writer, verbose bool) error {
// Get binary name from Cargo.toml.
var m CargoManifest
if err := m.Read("Cargo.toml"); err != nil {
Expand All @@ -164,12 +164,27 @@ func (r Rust) Build(out io.Writer) error {
// Specify the toolchain using the `cargo +<version>` syntax.
toolchain := fmt.Sprintf("+%s", RustToolchainVersion)

args := []string{
toolchain,
"build",
"--bin",
binName,
"--release",
"--target",
WasmWasiTarget,
"--color",
"always",
}
if verbose {
args = append(args, "--verbose")
}

// Call cargo build with Wasm Wasi target and release flags.
// gosec flagged this:
// G204 (CWE-78): Subprocess launched with variable
// Disabling as the variables come from trusted sources.
/* #nosec */
cmd := exec.Command("cargo", toolchain, "build", "--bin", binName, "--release", "--target", WasmWasiTarget)
cmd := exec.Command("cargo", args...)

// Add debuginfo RUSTFLAGS to command environment to ensure DWARF debug
// infomation (such as, source mappings) are compiled into the binary.
Expand Down Expand Up @@ -201,15 +216,21 @@ func (r Rust) Build(out io.Writer) error {
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()

// Wait for the command to exit.
if err := cmd.Wait(); err != nil {
return fmt.Errorf("error during compilation process: %w", err)
}
if errStdout != nil {
return fmt.Errorf("error during compilation process: %w", errStdout)
return fmt.Errorf("error streaming stdout output from child process: %w", errStdout)
}
if errStderr != nil {
return fmt.Errorf("error during compilation process: %w", errStderr)
return fmt.Errorf("error streaming stderr output from child process: %w", errStderr)
}

// Wait for the command to exit.
if err := cmd.Wait(); err != nil {
// If we're not in verbose mode return the bufferred stderr output
// from cargo as the error.
if !verbose && stderrBuf.Len() > 0 {
return fmt.Errorf("error during compilation process:\n%s", strings.TrimSpace(stderrBuf.String()))
}
return fmt.Errorf("error during compilation process")
}

// Get working directory.
Expand Down

0 comments on commit 8bbbe4f

Please sign in to comment.