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

breaking(language.rust): Switch Rust builds to wasm32-wasip1 instead of wasm32-wasi #1382

Merged
merged 1 commit into from
Feb 12, 2025
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
4 changes: 2 additions & 2 deletions .fastly/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ toolchain_constraint = ">= 1.21" # Go toolchain constraint for use wit
toolchain_constraint_tinygo = ">= 1.18" # Go toolchain constraint for use with TinyGo.

[language.rust]
toolchain_constraint = ">= 1.56.1, <1.84.0"
wasm_wasi_target = "wasm32-wasi"
toolchain_constraint = ">= 1.78.0"
wasm_wasi_target = "wasm32-wasip1"

[wasm-tools]
ttl = "24h"
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/pr_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install Rust"
uses: dtolnay/rust-toolchain@1.83.0 # to install tq via `make config`
uses: dtolnay/rust-toolchain@stable
- name: "Generate static app config"
run: make config
- name: "Config Artifact"
Expand All @@ -25,7 +25,7 @@ jobs:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install Rust"
uses: dtolnay/rust-toolchain@1.83.0 # to install tq via `make config`
uses: dtolnay/rust-toolchain@stable
- name: Install Go
uses: actions/setup-go@v5
with:
Expand Down Expand Up @@ -124,11 +124,11 @@ jobs:
path: ${{ steps.go-cache-paths.outputs.gomod }}
key: ${{ runner.os }}-test-go-mod-${{ hashFiles('**/go.sum') }}
- name: "Install Rust"
uses: dtolnay/rust-toolchain@1.83.0
- name: "Add wasm32-wasi Rust target"
run: rustup target add wasm32-wasi --toolchain 1.83.0
uses: dtolnay/rust-toolchain@stable
- name: "Add wasm32-wasip1 Rust target"
run: rustup target add wasm32-wasip1 --toolchain stable
- name: "Validate Rust toolchain"
run: rustup show && rustup target list --installed --toolchain 1.83.0
run: rustup show && rustup target list --installed --toolchain stable
shell: bash
- name: "Install Node"
uses: actions/setup-node@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag_to_draft_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: "Set GOHOSTOS and GOHOSTARCH"
run: echo "GOHOSTOS=$(go env GOHOSTOS)" >> $GITHUB_ENV && echo "GOHOSTARCH=$(go env GOHOSTARCH)" >> $GITHUB_ENV
- name: "Install Rust"
uses: dtolnay/rust-toolchain@1.83.0
uses: dtolnay/rust-toolchain@stable
- name: "Generate static app config"
run: make config
# Passing the raw SSH private key causes an error:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# CHANGELOG

## Unreleased

**Enhancements:**

- breaking(language.rust): Switch Rust builds to wasm32-wasip1 instead of wasm32-wasi [#1382](https://github.com/fastly/cli/pull/1382)

**Bug fixes:**

**Dependencies:**

## [v10.18.0](https://github.com/fastly/cli/releases/tag/v10.18.0) (2025-01-29)

**Enhancements:**
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile-rust
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM rust:latest
LABEL maintainer="Fastly OSS <oss@fastly.com>"

ENV RUST_TOOLCHAIN=1.83.0
ENV RUST_TOOLCHAIN=stable
RUN rustup toolchain install ${RUST_TOOLCHAIN} \
&& rustup target add wasm32-wasi --toolchain ${RUST_TOOLCHAIN} \
&& rustup target add wasm32-wasip1 --toolchain ${RUST_TOOLCHAIN} \
&& apt-get update && apt-get install -y curl jq && apt-get -y clean && rm -rf /var/lib/apt/lists/* \
&& export FASTLY_CLI_VERSION=$(curl -s https://api.github.com/repos/fastly/cli/releases/latest | jq -r .tag_name | cut -d 'v' -f 2) \
GOARCH=$(dpkg --print-architecture) \
Expand Down
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_COMPUTE_BUILD_RUST=1 make test TEST_ARGS="-run TestBuildRust/fastly_crate_p

When running the tests locally, if you don't have the relevant language ecosystems set-up properly then the tests will fail to run and you'll need to review the code to see what the remediation steps are, as that output doesn't get shown when running the test suite.

> **NOTE**: you might notice a discrepancy between CI and your local environment which is caused by the difference in Rust toolchain versions as defined in .github/workflows/pr_test.yml which specifies the version required to be tested for in CI. Running `rustup toolchain install <version>` and `rustup target add wasm32-wasi --toolchain <version>` will resolve any failing integration tests you may be running locally.
> **NOTE**: you might notice a discrepancy between CI and your local environment which is caused by the difference in Rust toolchain versions as defined in .github/workflows/pr_test.yml which specifies the version required to be tested for in CI. Running `rustup toolchain install <version>` and `rustup target add wasm32-wasip1 --toolchain <version>` will resolve any failing integration tests you may be running locally.

To the run the full test suite:

Expand Down
42 changes: 35 additions & 7 deletions pkg/commands/compute/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestBuildRust(t *testing.T) {
Profiles: testutil.TokenProfile(),
Language: config.Language{
Rust: config.Rust{
ToolchainConstraint: ">= 1.54.0",
WasmWasiTarget: "wasm32-wasi",
ToolchainConstraint: ">= 1.78.0",
WasmWasiTarget: "wasm32-wasip1",
},
},
},
Expand Down Expand Up @@ -102,8 +102,8 @@ func TestBuildRust(t *testing.T) {
Profiles: testutil.TokenProfile(),
Language: config.Language{
Rust: config.Rust{
ToolchainConstraint: ">= 1.54.0",
WasmWasiTarget: "wasm32-wasi",
ToolchainConstraint: ">= 1.78.0",
WasmWasiTarget: "wasm32-wasip1",
},
},
},
Expand All @@ -123,6 +123,34 @@ func TestBuildRust(t *testing.T) {
build = "echo no compilation happening"`,
wantRemediationError: compute.DefaultBuildErrorRemediation,
},
{
name: "wasmwasi target error",
args: args("compute build --verbose"),
applicationConfig: &config.File{
Profiles: testutil.TokenProfile(),
Language: config.Language{
Rust: config.Rust{
ToolchainConstraint: ">= 1.78.0",
WasmWasiTarget: "wasm32-wasi",
},
},
},
cargoManifest: `
[package]
name = "fastly-compute-project"
version = "0.1.0"

[dependencies]
fastly = "=0.6.0"`,
fastlyManifest: fmt.Sprintf(`
manifest_version = 2
name = "test"
language = "rust"

[scripts]
build = "%s"`, fmt.Sprintf(compute.RustDefaultBuildCommand, compute.RustDefaultPackageName, compute.RustDefaultWasmWasiTarget)),
wantError: "the default build in .fastly/config.toml should produce a wasm32-wasip1 binary, but was instead set to produce a wasm32-wasi binary",
},
// NOTE: This test passes --verbose so we can validate specific outputs.
{
name: "successful build",
Expand All @@ -131,8 +159,8 @@ func TestBuildRust(t *testing.T) {
Profiles: testutil.TokenProfile(),
Language: config.Language{
Rust: config.Rust{
ToolchainConstraint: ">= 1.54.0",
WasmWasiTarget: "wasm32-wasi",
ToolchainConstraint: ">= 1.78.0",
WasmWasiTarget: "wasm32-wasip1",
},
},
},
Expand All @@ -149,7 +177,7 @@ func TestBuildRust(t *testing.T) {
language = "rust"

[scripts]
build = "%s"`, fmt.Sprintf(compute.RustDefaultBuildCommand, compute.RustDefaultPackageName)),
build = "%s"`, fmt.Sprintf(compute.RustDefaultBuildCommand, compute.RustDefaultPackageName, compute.RustDefaultWasmWasiTarget)),
wantOutput: []string{
"Creating ./bin directory (for Wasm binary)",
"Built package",
Expand Down
42 changes: 26 additions & 16 deletions pkg/commands/compute/language_rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import (
// NOTE: In the 5.x CLI releases we persisted the default to the fastly.toml
// We no longer do that. In 6.x we use the default and just inform the user.
// This makes the experience less confusing as users didn't expect file changes.
const RustDefaultBuildCommand = "cargo build --bin %s --release --target wasm32-wasi --color always"
const RustDefaultBuildCommand = "cargo build --bin %s --release --target %s --color always"

// RustDefaultWasmWasiTarget is the expected Rust WasmWasi build target
const RustDefaultWasmWasiTarget = "wasm32-wasip1"

// RustManifest is the manifest file for defining project configuration.
const RustManifest = "Cargo.toml"
Expand Down Expand Up @@ -145,7 +148,7 @@ func (r *Rust) Dependencies() map[string]string {
// Build compiles the user's source code into a Wasm binary.
func (r *Rust) Build() error {
if r.build == "" {
r.build = fmt.Sprintf(RustDefaultBuildCommand, RustDefaultPackageName)
r.build = fmt.Sprintf(RustDefaultBuildCommand, RustDefaultPackageName, RustDefaultWasmWasiTarget)
r.defaultBuild = true
}

Expand All @@ -170,6 +173,11 @@ func (r *Rust) Build() error {
}
}

var wasmWasiTarget = r.config.WasmWasiTarget
if wasmWasiTarget != RustDefaultWasmWasiTarget {
return fmt.Errorf("the default build in .fastly/config.toml should produce a %s binary, but was instead set to produce a %s binary", RustDefaultWasmWasiTarget, wasmWasiTarget)
}

bt := BuildToolchain{
autoYes: r.autoYes,
buildFn: r.Shell.Build,
Expand Down Expand Up @@ -204,7 +212,7 @@ type RustToolchain struct {
// modifyCargoPackageName validates whether the --bin flag matches the
// Cargo.toml package name. If it doesn't match, update the default build script
// to match.
func (r *Rust) modifyCargoPackageName(noBuildScript bool) error {
func (r *Rust) modifyCargoPackageName(defaultBuild bool) error {
s := "cargo locate-project --quiet"
args := strings.Split(s, " ")

Expand Down Expand Up @@ -244,18 +252,16 @@ func (r *Rust) modifyCargoPackageName(noBuildScript bool) error {
return fmt.Errorf("error reading %s manifest: %w", RustManifest, err)
}

hasCustomBuildScript := !noBuildScript

switch {
case m.Package.Name != "":
// If using standard project structure.
// Cargo.toml won't be a Workspace, so it will contain a package name.
r.packageName = m.Package.Name
case len(m.Workspace.Members) > 0 && noBuildScript:
case len(m.Workspace.Members) > 0 && defaultBuild:
// If user has a Cargo Workspace AND no custom script.
// We need to identify which Workspace package is their application.
// Then extract the package name from its Cargo.toml manifest.
// We do this by checking for a rust-toolchain.toml containing a wasm32-wasi target.
// We do this by checking for a rust-toolchain.toml containing the proper target.
//
// NOTE: This logic will need to change in the future.
// Specifically, when we support linking multiple Wasm binaries.
Expand All @@ -270,16 +276,20 @@ func (r *Rust) modifyCargoPackageName(noBuildScript bool) error {
if err != nil {
return fmt.Errorf("failed to unmarshal '%s' data: %w", rustToolchainFile, err)
}
if len(rtm.Toolchain.Targets) > 0 && rtm.Toolchain.Targets[0] == "wasm32-wasi" {
var cm CargoManifest
err := cm.Read(filepath.Join(m, "Cargo.toml"))
if err != nil {
return err
if len(rtm.Toolchain.Targets) > 0 {
if rtm.Toolchain.Targets[0] == RustDefaultWasmWasiTarget {
var cm CargoManifest
err := cm.Read(filepath.Join(m, "Cargo.toml"))
if err != nil {
return err
}
r.packageName = cm.Package.Name
} else {
return fmt.Errorf("please consult https://www.fastly.com/documentation/guides/compute/#install-language-tooling to configure your toolchain correctly")
}
r.packageName = cm.Package.Name
}
}
case len(m.Workspace.Members) > 0 && hasCustomBuildScript:
case len(m.Workspace.Members) > 0 && !defaultBuild:
// If user has a Cargo Workspace AND a custom script.
// Trust their custom script aligns with the relevant Workspace package name.
// i.e. we parse the package name specified in their custom script.
Expand All @@ -293,8 +303,8 @@ func (r *Rust) modifyCargoPackageName(noBuildScript bool) error {
}

// Ensure the default build script matches the Cargo.toml package name.
if noBuildScript && r.packageName != "" && r.packageName != RustDefaultPackageName {
r.build = fmt.Sprintf(RustDefaultBuildCommand, r.packageName)
if defaultBuild && r.packageName != "" && r.packageName != RustDefaultPackageName {
r.build = fmt.Sprintf(RustDefaultBuildCommand, r.packageName, RustDefaultWasmWasiTarget)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/testdata/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ version = "0.0.1"

[language]
[language.rust]
toolchain_constraint = ">= 1.49.0 < 2.0.0"
wasm_wasi_target = "wasm32-wasi"
toolchain_constraint = ">= 1.78.0"
wasm_wasi_target = "wasm32-wasip1"

[starter-kits]
[[starter-kits.assemblyscript]]
Expand Down