Skip to content

Commit 07256da

Browse files
committed
wit-component, wit-parser: preserve doc comments through embed path
1 parent de09ad2 commit 07256da

14 files changed

Lines changed: 86 additions & 2 deletions

crates/wit-component/src/metadata.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ use wasm_encoder::{
5050
};
5151
use wasm_metadata::Producers;
5252
use wasmparser::{BinaryReader, Encoding, Parser, Payload};
53-
use wit_parser::{CloneMaps, Package, PackageName, Resolve, World, WorldId, WorldItem, WorldKey};
53+
use wit_parser::{
54+
CloneMaps, Package, PackageMetadata, PackageName, Resolve, World, WorldId, WorldItem, WorldKey,
55+
};
5456

5557
const CURRENT_VERSION: u8 = 0x04;
5658
const CUSTOM_SECTION_NAME: &str = "wit-component-encoding";
@@ -292,6 +294,12 @@ pub fn encode(
292294
data: Cow::Borrowed(&[CURRENT_VERSION, string_encoding]),
293295
});
294296

297+
let package_docs = PackageMetadata::extract(resolve, world.package.unwrap());
298+
builder.custom_section(&CustomSection {
299+
name: PackageMetadata::SECTION_NAME.into(),
300+
data: package_docs.encode()?.into(),
301+
});
302+
295303
let ty = builder.type_component(None, &outer_ty);
296304
builder.export(&world.name, ComponentExportKind::Type, ty, None);
297305

crates/wit-parser/src/decoding.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ pub fn decode_world(wasm: &[u8]) -> Result<(Resolve, WorldId)> {
429429
let mut exports = Vec::new();
430430
let mut depth = 1;
431431
let mut types = None;
432+
#[cfg(feature = "serde")]
433+
let mut package_metadata: Option<PackageMetadata> = None;
432434

433435
for payload in Parser::new(0).parse_all(wasm) {
434436
let payload = payload?;
@@ -451,6 +453,15 @@ pub fn decode_world(wasm: &[u8]) -> Result<(Resolve, WorldId)> {
451453
exports.push(export?);
452454
}
453455
}
456+
#[cfg(feature = "serde")]
457+
Payload::CustomSection(s)
458+
if depth == 1 && s.name() == PackageMetadata::SECTION_NAME =>
459+
{
460+
if package_metadata.is_some() {
461+
bail!("multiple {:?} sections", PackageMetadata::SECTION_NAME);
462+
}
463+
package_metadata = Some(PackageMetadata::decode(s.data())?);
464+
}
454465
_ => {}
455466
}
456467
}
@@ -489,12 +500,16 @@ pub fn decode_world(wasm: &[u8]) -> Result<(Resolve, WorldId)> {
489500
worlds: &mut worlds,
490501
},
491502
)?;
492-
let (resolve, pkg) = decoder.finish(Package {
503+
let (mut resolve, pkg) = decoder.finish(Package {
493504
name,
494505
interfaces,
495506
worlds,
496507
docs: Default::default(),
497508
});
509+
#[cfg(feature = "serde")]
510+
if let Some(metadata) = package_metadata {
511+
metadata.inject(&mut resolve, pkg)?;
512+
}
498513
// The package decoded here should only have a single world so extract that
499514
// here to return.
500515
let world = *resolve.packages[pkg].worlds.iter().next().unwrap().1;

tests/cli/component-embed-only-custom.wit.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(component
22
(@custom "wit-component-encoding" "/04/00")
3+
(@custom "package-docs" "/01{/22docs/22:/22RUN: component embed --only-custom -w foo % | print/22}")
34
(type (;0;)
45
(component
56
(type (;0;)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: component embed --dummy % | component wit
2+
3+
package foo:docs-test;
4+
5+
/// The interface.
6+
interface my-interface {
7+
/// The function.
8+
my-func: func(
9+
/// The function's argument.
10+
my-arg: string,
11+
) -> string;
12+
}
13+
14+
/// A world with documentation.
15+
world my-world {
16+
export my-interface;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package root:root;
2+
3+
world root {
4+
export foo:docs-test/my-interface;
5+
}
6+
/// RUN: component embed --dummy % | component wit
7+
package foo:docs-test {
8+
/// The interface.
9+
interface my-interface {
10+
/// The function.
11+
my-func: func(my-arg: string) -> string;
12+
}
13+
/// A world with documentation.
14+
world my-world {
15+
export my-interface;
16+
}
17+
}

tests/cli/component-wit-of-core-module.wit.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ world root {
55

66
export y: func();
77
}
8+
/// RUN: component embed --dummy % | component wit
89
package a:b {
910
world foo {
1011
import x: func();

tests/cli/importize.wit.simple-component.stdout

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ package root:root;
33
world root-importized {
44
import importize:importize/t;
55
}
6+
/// RUN[simple]: component wit --importize-world simple %
7+
/// RUN[simple-rename]: component wit --importize-world simple-rename --importize-out-world-name test-rename %
8+
/// RUN[simple-component]: component embed --dummy --world simple % | /
9+
/// component wit --importize
10+
/// RUN[with-deps]: component wit --importize-world with-deps %
11+
/// RUN[simple-toplevel]: component wit --importize-world simple-toplevel %
12+
/// RUN[toplevel-deps]: component wit --importize-world toplevel-deps %
13+
/// FAIL[fail1]: component wit --importize-world fail1 %
14+
/// RUN[trim-imports]: component wit --importize-world trim-imports %
15+
/// RUN[tricky-import]: component wit --importize-world tricky-import %
616
package importize:importize {
717
interface t {
818
resource r;

tests/cli/multiple-packages-one-world.wit.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package test:foo2 {
1919
}
2020

2121

22+
/// RUN: component embed --dummy --wat % | component wit
2223
package foo:root {
2324
world hello {
2425
import test:foo1/bar;

tests/cli/print-core-wasm-wit.wit.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package root:root;
33
world root {
44
import foo:foo/my-interface;
55
}
6+
/// RUN: component embed --dummy % | component wit
67
package foo:foo {
78
interface my-interface {
89
foo: func();

tests/cli/reparent-anonymous-interfaces-on-merge.wit.stdout

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ world root {
1111
export b: interface {
1212
}
1313
}
14+
/// RUN: component embed --dummy --world w1 % | /
15+
/// component embed --world w2 % | /
16+
/// component embed --world w3 % | /
17+
/// component embed --world w4 % | /
18+
/// component wit
1419
package a:b {
1520
world w1 {
1621
}

0 commit comments

Comments
 (0)