Skip to content

Commit 4dce92b

Browse files
committed
Write correct producers metadata to generated components
These changes updates fastly-compute-py to inject appropriate producer metadata to our generated component at the top-level. Here's what we get with this change: $ wasm-tools metadata show build/bottle-app.composed.wasm --json | jq '.component.metadata.producers[]' [ "language", { "Python": "3.14" } ] [ "processed-by", { "componentize-py": "0.22.1", "fastly-compute-py": "0.1.0" } ] [ "sdk", { "fastly-compute-py": "0.1.0" } ]
1 parent 0449cc8 commit 4dce92b

5 files changed

Lines changed: 268 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 85 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fastly-compute-py/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ wac-graph = "0.8"
2323
wac-parser = "0.8"
2424
wit-parser = "0.244"
2525
wit-component = "0.244"
26+
wasm-metadata = { version = "0.245", default-features = false }
2627
tempfile = "3"
2728
componentize-py = { git = "https://github.com/bytecodealliance/componentize-py" }
2829
futures = { version = "0.3", default-features = false, features = ["executor"] }
@@ -35,5 +36,6 @@ indexmap = "2"
3536

3637
[build-dependencies]
3738
anyhow = "1"
39+
cargo_metadata = "0.23.1"
3840
wit-parser = "0.219"
3941
wit-component = "0.219"

crates/fastly-compute-py/build.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn main() -> Result<()> {
88
println!("cargo:rerun-if-changed=../../wit");
99
println!("cargo:rerun-if-changed=../../crates/wasiless");
1010
println!("cargo:rerun-if-changed=../../wrap_app_in_wasiless.wac");
11+
println!("cargo:rerun-if-changed=../../Cargo.lock");
1112

1213
let root_dir = PathBuf::from("../../");
1314
let wit_dir = root_dir.join("wit");
@@ -25,6 +26,20 @@ fn main() -> Result<()> {
2526
out_dir.join("wrap_app_in_wasiless.wac"),
2627
)?;
2728

29+
// Expose the componentize-py version for embedding in Wasm producers metadata.
30+
let metadata = cargo_metadata::MetadataCommand::new()
31+
.manifest_path(root_dir.join("Cargo.toml"))
32+
.exec()
33+
.context("Failed to run `cargo metadata`")?;
34+
let componentize_py_version = metadata
35+
.packages
36+
.iter()
37+
.find(|p| p.name == "componentize-py")
38+
.with_context(|| "componentize-py not found in cargo metadata")?
39+
.version
40+
.to_string();
41+
println!("cargo:rustc-env=COMPONENTIZE_PY_VERSION={componentize_py_version}");
42+
2843
Ok(())
2944
}
3045

@@ -71,7 +86,7 @@ fn build_wasiless_wasm(root_dir: impl AsRef<Path>, out_dir: impl AsRef<Path>) ->
7186
anyhow::bail!("Failed to build wasiless");
7287
}
7388

74-
// Transform wasiless into a component using wasm-tools compnent new
89+
// Transform wasiless into a component using wasm-tools component new
7590
let input_wasm = target_dir.join("wasm32-unknown-unknown/release/wasiless.wasm");
7691
let output_wasm = out_dir.as_ref().join("wasiless.wasm");
7792

crates/fastly-compute-py/src/lib.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wac_graph::EncodeOptions;
1010
use wac_parser::Document;
1111
use wac_types::BorrowedPackageKey;
1212
use wac_types::Package;
13+
use wasm_metadata::AddMetadata;
1314

1415
pub mod cli;
1516
pub mod config;
@@ -166,7 +167,10 @@ pub fn build(output: PathBuf, entry_name: String, virtualenv: Option<PathBuf>) -
166167
let composed =
167168
compose_with_wasiless(&temp_component_wasm_path, WASILESS_WASM, WRAP_WAC, &output)?;
168169

169-
fs::write(&output, composed)
170+
log::info!(" Injecting Fastly metadata...");
171+
let annotated = inject_fastly_metadata(composed)?;
172+
173+
fs::write(&output, annotated)
170174
.with_context(|| format!("Failed to write output: {}", output.display()))?;
171175

172176
log::debug!("Composed output: {}", output.display());
@@ -229,3 +233,56 @@ fn compose_with_wasiless(
229233

230234
Ok(encoded)
231235
}
236+
237+
/// Inject build tool metadata into the Wasm component's standard `producers`
238+
/// custom section.
239+
///
240+
/// Follows the WebAssembly [Producers Section spec] field conventions:
241+
///
242+
/// - `language: Python <version>` — the source language and the CPython
243+
/// version bundled by componentize-py. Update this when upgrading to a
244+
/// componentize-py release that bundles a different CPython version.
245+
/// - `sdk: fastly-compute-py <version>` — the SDK library the user's code is
246+
/// written against, analogous to `@fastly/js-compute` for the JS SDK.
247+
/// - `processed-by: componentize-py <version>` — the tool that performed the
248+
/// core Wasm transformation. `fastly-compute-py` also adds itself here as
249+
/// the build orchestrator.
250+
///
251+
/// Note: the Fastly-proprietary `fastly.manifest.*` custom sections
252+
/// (language, version, service_id, etc.) are **not** written here. Those are
253+
/// injected during package ingestion, sourced from the `fastly.toml` manifest
254+
/// that the CLI bundles alongside the Wasm in the upload package.
255+
/// Dependency lists, build scripts, and machine info are similarly the CLI's
256+
/// responsibility via its `fastly_data` producers entry.
257+
///
258+
/// [Producers Section spec]: https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md
259+
fn inject_fastly_metadata(wasm: Vec<u8>) -> Result<Vec<u8>> {
260+
let mut add_metadata = AddMetadata::default();
261+
262+
// Source language. The version is the CPython version bundled by
263+
// componentize-py — update this when upgrading to a componentize-py
264+
// release that bundles a different CPython version.
265+
add_metadata
266+
.language
267+
.push(("Python".to_owned(), "3.14".to_owned()));
268+
269+
// The SDK the user's code is written against.
270+
add_metadata.sdk.push((
271+
"fastly-compute-py".to_owned(),
272+
env!("CARGO_PKG_VERSION").to_owned(),
273+
));
274+
275+
// Tools that performed the Wasm transformation.
276+
add_metadata.processed_by.push((
277+
"componentize-py".to_owned(),
278+
env!("COMPONENTIZE_PY_VERSION").to_owned(),
279+
));
280+
add_metadata.processed_by.push((
281+
"fastly-compute-py".to_owned(),
282+
env!("CARGO_PKG_VERSION").to_owned(),
283+
));
284+
285+
add_metadata
286+
.to_wasm(&wasm)
287+
.context("Failed to add producers metadata to Wasm component")
288+
}

0 commit comments

Comments
 (0)