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

feat(compute/build): support Cargo Workspaces #1023

Merged
merged 2 commits into from
Oct 2, 2023

Conversation

Integralist
Copy link
Collaborator

@Integralist Integralist commented Sep 29, 2023

Fixes #1020

Problem

The logic flow prior to this PR would cause a user's custom build script to be replaced with a default build script if the package name they set in their Cargo.toml didn't match the 'default' package name of fastly-compute-project.

This issue was further exacerbated when a customer used a Cargo Workspace to structure their project. As part of a reduced test case I discovered a bug that caused the user's custom build script to be unexpectedly modified and also changed it in such a way that it would break the build step.

Solution

This PR modifies the logic flow for a Rust project.

The updated flow looks like this...

  • If using a standard project structure, then the root Cargo.toml won't be a Cargo Workspace, and so it will contain a package name. The CLI will assign that name to r.packageName and later on it will use that name to look up the compiled binary and move it to the required bin/ directory.

  • If the user has set up a Cargo Workspace but there is no custom build script, then the CLI needs to identify which Workspace package is their application. It does this by checking if the package has a rust-toolchain.toml containing the target wasm32-wasi and if so we extract the package name from that package's relevant Cargo.toml manifest. The CLI will assign that name to r.packageName and later on it will use that name to look up the compiled binary and move it to the required bin/ directory.

    NOTE: This is sufficient for now, but I can see us needing to update this logic in the future when we start supporting the linking of multiple Wasm binaries on the Fastly platform (aka Wasm Components). Because at that point, a Cargo Workspace could well contain multiple 'binary' projects, each producing a Wasm binary that are linked up in some fashion.

  • If the user has set up a Cargo Workspace and they define a custom build script, then the CLI will trust their custom script aligns with the relevant Workspace package name (i.e. their custom script is set up to reference the right binary). The CLI will parse the package name specified in their custom script by looking at the value assigned to the --bin flag. The CLI will assign that name to r.packageName and later on it will use that name to look up the compiled binary and move it to the required bin/ directory.

    NOTE: The implementation is very basic and presumes the --bin value is inserted directly and isn't required to be resolved (e.g. $some_environment_variable).

  • If there is no custom build script (i.e. the CLI will use a pre-defined default build script), then the CLI will check to see if the package name (assigned to r.packageName) matches the default package name fastly-compute-project. If not, then it will update the default build script to use whatever package name is assigned to r.packageName.

NOTES

I've tested this PR with different project set ups to validate the new logic flows.

Below is an example Cargo Workspace project I tested this with...

.
├── Cargo.lock
├── Cargo.toml
├── bin
│   └── main.wasm
├── compute
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── README.md
│   ├── rust-toolchain.toml
│   └── src
│       ├── main.rs
│       └── welcome-to-compute@edge.html
├── fastly.toml
└── pkg
    └── compute.tar.gz

5 directories, 11 files

Below is the root workspace Cargo.toml...

[workspace]

members = ["compute"] # << matches compute/ directory name

Below is the compute/Cargo.toml...

[package]
name = "my-compute-app"
version = "0.1.0"
authors = []
edition = "2018"
# Remove this line if you want to be able to publish this crate as open source on crates.io.
# Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source.
publish = false

[profile.release]
debug = 1

[dependencies]
fastly = "0.9.4"

Below is the fastly.toml file I used with the Cargo Workspace (notice it was moved to the root directory, i.e. I moved it out from the compute/ directory which contains my Compute application)...

authors = ["integralist@fastly.com"]
description = ""
language = "rust"
manifest_version = 3
name = "compute"
service_id = ""

[scripts]
  build = "cargo build --bin my-compute-app --release --target wasm32-wasi --color always -vv"

pkg/commands/compute/language_rust.go Outdated Show resolved Hide resolved
Co-authored-by: Dora Militaru <hello@doramilitaru.com>
@Integralist Integralist merged commit 649b55c into main Oct 2, 2023
6 checks passed
@Integralist Integralist deleted the integralist/cargo-workspace branch October 2, 2023 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[fastly compute build] scripts.build ignored with rust lang
2 participants