8000 Check the rustup version by fgsch · Pull Request #248 · fastly/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Check the rustup version #248

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
merged 3 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions pkg/compute/compute_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ func TestBuildRust(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: "0.0.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -676,6 +677,7 @@ func TestBuildRust(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: "0.0.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -709,6 +711,7 @@ func TestBuildRust(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.4.0 <= 0.9.0", // the fastly-sys version in 0.6.0 is actually ^0.3.6 so a minimum of 0.4.0 causes the constraint to fail
RustupConstraint: ">= 1.23.0",
},
},
},
Expand All @@ -731,6 +734,7 @@ func TestBuildRust(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.3.0 <= 0.6.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -763,6 +767,7 @@ func TestBuildRust(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.3.0 <= 0.6.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -1229,6 +1234,7 @@ func TestPublish(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.3.0 <= 0.6.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -1287,6 +1293,7 @@ func TestPublish(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.3.0 <= 0.6.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down Expand Up @@ -1345,6 +1352,7 @@ func TestPublish(t *testing.T) {
ToolchainVersion: "1.49.0",
WasmWasiTarget: "wasm32-wasi",
FastlySysConstraint: ">= 0.3.0 <= 0.6.0",
RustupConstraint: ">= 1.23.0",
},
},
},
Expand Down
48 changes: 42 additions & 6 deletions pkg/compute/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -123,15 +124,50 @@ func (r Rust) Verify(out io.Writer) error {

fmt.Fprintf(out, "Found rustup at %s\n", p)

// 2) Check that the desired toolchain version is installed
// 2) Check that the desired rustup version is installed
fmt.Fprintf(out, "Checking if rustup %s is installed...\n", r.config.File.Language.Rust.RustupConstraint)

cmd := exec.Command("rustup", "--version")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error executing rustup: %w", err)
}

reader := bufio.NewReader(bytes.NewReader(stdoutStderr))
line, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("error reading rustup output: %w", err)
}
parts := strings.Split(line, " ")
// Either `rustup <version> (<date>)` or `rustup <version> (<sha> <date>)`
if len(parts) != 3 && len(parts) != 4 {
return fmt.Errorf("error reading rustup version")
}
rustupVersion, err := semver.NewVersion(parts[1])
if err != nil {
return fmt.Errorf("error parsing rustup version: %w", err)
}

rustupConstraint, err := semver.NewConstraint(r.config.File.Language.Rust.RustupConstraint)
if err != nil {
return fmt.Errorf("error parsing rustup constraint: %w", err)
}
if !rustupConstraint.Check(rustupVersion) {
return errors.RemediationError{
Inner: fmt.Errorf("rustup constraint not met: %s", r.config.File.Language.Rust.RustupConstraint),
Remediation: fmt.Sprintf("To fix this error, run the following command:\n\n\t$ %s\n", text.Bold("rustup self update")),
}
}

// 3) Check that the desired toolchain version is installed
//
// We use rustup to assert that the toolchain is installed by streaming the output of
// `rustup toolchain list` and looking for a toolchain whose prefix matches our desired
// version.
fmt.Fprintf(out, "Checking if Rust %s is installed...\n", r.config.File.Language.Rust.ToolchainVersion)

cmd := exec.Command("rustup", "toolchain", "list")
stdoutStderr, err := cmd.CombinedOutput()
cmd = exec.Command("rustup", "toolchain", "list")
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error executing rustup: %w", err)
}
Expand All @@ -153,7 +189,7 @@ func (r Rust) Verify(out io.Writer) error {
}
}

// 3) Check `wasm32-wasi` target exists
// 4) Check `wasm32-wasi` target exists
//
// We use rustup to assert that the target is installed for our toolchain by streaming the
// output of `rustup target list` and looking for the the `wasm32-wasi` value. If not found,
Expand Down Expand Up @@ -193,7 +229,7 @@ func (r Rust) Verify(out io.Writer) error {

fmt.Fprintf(out, "Found wasm32-wasi target\n")

// 4) Check Cargo.toml file exists in $PWD
// 5) Check Cargo.toml file exists in $PWD
//
// A valid Cargo.toml file is needed for the `cargo build` compilation
// process. Therefore, we assert whether one exists in the current $PWD.
Expand All @@ -209,7 +245,7 @@ func (r Rust) Verify(out io.Writer) error {

fmt.Fprintf(out, "Found Cargo.toml at %s\n", fpath)

// 5) Verify `fastly` and `fastly-sys` crate version
// 6) Verify `fastly` and `fastly-sys` crate version
//
// A valid and up-to-date version of the fastly-sys crate is required.
if !filesystem.FileExists(fpath) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ type Rust struct {
// FastlySysConstraint is a free-form semver constraint for the internal Rust
// ABI version that should be supported.
FastlySysConstraint string `toml:"fastly_sys_constraint"`

// RustupConstraint is a free-form semver constraint for the rustup version
// that should be installed.
RustupConstraint string `toml:"rustup_constraint"`
}

// StarterKitLanguages represents language specific starter kits.
Expand Down
6 changes: 6 additions & 0 deletions pkg/configure/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestConfigure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down Expand Up @@ -113,6 +114,7 @@ func TestCon 6DAF figure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down Expand Up @@ -154,6 +156,7 @@ func TestConfigure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down Expand Up @@ -198,6 +201,7 @@ func TestConfigure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down Expand Up @@ -240,6 +244,7 @@ func TestConfigure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down Expand Up @@ -285,6 +290,7 @@ func TestConfigure(t *testing.T) {

[language.rust]
fastly_sys_constraint = ""
rustup_constraint = ""
toolchain_version = ""
wasm_wasi_target = ""

Expand Down
0