Skip to content

Commit 87c646e

Browse files
authored
Rely less on wasm-component-ld for tests (#1434)
* Rely less on `wasm-component-ld` for tests This repository is often used to test experimental/upcoming features for components such as async/etc, but this creates a chicken-and-egg problem where it becomes difficult to compile tests. Many tests use upstream toolchains which use `wasm-component-ld` (e.g. Rust/C tests) but it can take time to get this updated. There's already logic to handle having tests target wasip1 and running the wit-component step manually, so this PR makes that step unconditional. This means that test binaries are always assembled with a crate-dependency of `wit-component` which should enable easier updates since it's "just" a Cargo dependency vs being part of a toolchain in a separate project. * Conditionally embed type information
1 parent f7f3b49 commit 87c646e

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
with:
123123
submodules: true
124124
- name: Install Rust
125-
run: rustup update nightly --no-self-update && rustup default nightly
125+
run: rustup update stable --no-self-update && rustup default stable
126126
- run: rustup target add wasm32-wasip2
127127
- uses: ./.github/actions/install-wasi-sdk
128128
- run: |

crates/test/src/c.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ fn compile(runner: &Runner<'_>, compile: &Compile<'_>, compiler: PathBuf) -> Res
123123

124124
// Now compile the runner's source code to with the above object and the
125125
// component-type object into a final component.
126-
let output = if produces_component(runner) {
127-
compile.output.to_path_buf()
128-
} else {
129-
compile.output.with_extension("core.wasm")
130-
};
126+
let output = compile.output.with_extension("core.wasm");
131127
let mut cmd = Command::new(compiler);
132128
cmd.arg(&compile.component.path)
133129
.arg(&bindings_object)
@@ -154,13 +150,14 @@ fn compile(runner: &Runner<'_>, compile: &Compile<'_>, compiler: PathBuf) -> Res
154150
cmd.arg("-mexec-model=reactor");
155151
}
156152
}
153+
if produces_component(runner) {
154+
cmd.arg("-Wl,--skip-wit-component");
155+
}
157156
runner.run_command(&mut cmd)?;
158157

159-
if !produces_component(runner) {
160-
runner
161-
.convert_p1_to_component(&output, compile)
162-
.with_context(|| format!("failed to convert {output:?}"))?;
163-
}
158+
runner
159+
.convert_p1_to_component(&output, compile)
160+
.with_context(|| format!("failed to convert {output:?}"))?;
164161
Ok(())
165162
}
166163

crates/test/src/lib.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,17 @@ status: {}",
999999
.context("failed to load WIT")?;
10001000
let world = resolve.select_world(&[pkg], Some(&compile.component.bindgen.world))?;
10011001
let mut module = fs::read(&p1).context("failed to read wasm file")?;
1002-
let encoded = wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None)?;
10031002

1004-
let section = wasm_encoder::CustomSection {
1005-
name: Cow::Borrowed("component-type"),
1006-
data: Cow::Borrowed(&encoded),
1007-
};
1008-
module.push(section.id());
1009-
section.encode(&mut module);
1003+
if !has_component_type_sections(&module) {
1004+
let encoded =
1005+
wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None)?;
1006+
let section = wasm_encoder::CustomSection {
1007+
name: Cow::Borrowed("component-type"),
1008+
data: Cow::Borrowed(&encoded),
1009+
};
1010+
module.push(section.id());
1011+
section.encode(&mut module);
1012+
}
10101013

10111014
let wasi_adapter = match compile.component.kind {
10121015
Kind::Runner => {
@@ -1071,6 +1074,18 @@ status: {}",
10711074
}
10721075
}
10731076

1077+
fn has_component_type_sections(wasm: &[u8]) -> bool {
1078+
for payload in wasmparser::Parser::new(0).parse_all(wasm) {
1079+
match payload {
1080+
Ok(wasmparser::Payload::CustomSection(s)) if s.name().starts_with("component-type") => {
1081+
return true
1082+
}
1083+
_ => {}
1084+
}
1085+
}
1086+
false
1087+
}
1088+
10741089
struct StepResult<'a> {
10751090
result: Result<()>,
10761091
should_fail: bool,

crates/test/src/rust.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@ path = 'lib.rs'
166166
// If this rust target doesn't natively produce a component then place
167167
// the compiler output in a temporary location which is componentized
168168
// later on.
169-
let output = if runner.produces_component() {
170-
compile.output.to_path_buf()
171-
} else {
172-
compile.output.with_extension("core.wasm")
173-
};
169+
let output = compile.output.with_extension("core.wasm");
174170

175171
// Compile all extern crates, if any
176172
let mut externs = Vec::new();
@@ -216,13 +212,14 @@ path = 'lib.rs'
216212
cmd.arg("--crate-type=cdylib");
217213
}
218214
}
215+
if runner.produces_component() {
216+
cmd.arg("-Clink-arg=--skip-wit-component");
217+
}
219218
runner.run_command(&mut cmd)?;
220219

221-
if !runner.produces_component() {
222-
runner
223-
.convert_p1_to_component(&output, compile)
224-
.with_context(|| format!("failed to convert {output:?}"))?;
225-
}
220+
runner
221+
.convert_p1_to_component(&output, compile)
222+
.with_context(|| format!("failed to convert {output:?}"))?;
226223

227224
Ok(())
228225
}

0 commit comments

Comments
 (0)