Skip to content

Implement automated WIT interface compliance validation #78

@avrabe

Description

@avrabe

Problem

Currently, the validate_wit = True option in Rust and C++ component rules only provides manual validation by logging both the component's actual interface and the expected WIT specification side-by-side. Developers must manually compare the outputs to identify mismatches.

Current Behavior

When validate_wit = True is enabled:

  • ✅ Extracts component's actual WIT interface using wasm-tools component wit
  • ✅ Displays expected WIT specification from source file
  • ✅ Creates validation log with side-by-side comparison
  • Does NOT automatically fail build on interface mismatches
  • Does NOT provide specific error messages for mismatches

Requested Feature

Implement automated interface compliance checking that:

  1. Parses WIT interfaces: Parse both component exports and WIT specification
  2. Compares interfaces programmatically:
    • Function signatures (names, parameters, return types)
    • Exported interfaces and worlds
    • Type definitions and compatibility
  3. Fails build on mismatch: Exit with clear error code and specific mismatch details
  4. Provides actionable error messages:
    • "Function 'hello' expects string parameter, got u32"
    • "Missing export: interface 'calculator' not found in component"
    • "Type mismatch: expected f64, found f32"

Implementation Approach

Option 1: WIT Parser Integration

  • Use existing WIT parsing libraries (wit-parser, wit-bindgen internals)
  • Implement comparison logic in Rust/Go
  • Integrate with existing validation toolchain

Option 2: wasm-tools Enhancement

  • Extend wasm-tools with interface comparison command
  • Add wasm-tools component validate --wit <spec.wit> component.wasm
  • Leverage existing WIT tooling infrastructure

Option 3: Custom Validation Tool

  • Create dedicated validation binary in //tools/wit_validator
  • Implement WIT parsing and comparison logic
  • Integrate with Bazel action system

Example Usage

rust_wasm_component_bindgen(
    name = "my_component",
    srcs = ["src/lib.rs"],
    wit = ":my_interfaces",
    validate_wit = True,  # Now fails build on interface mismatch
)

Build failure example:

ERROR: WIT validation failed for //examples/basic:hello_component
Interface mismatch in hello:interfaces/[email protected]:
  - Function 'hello' parameter mismatch:
    Expected: hello(name: string) -> string  
    Found:    hello(name: u32) -> string

Benefits

  • Catch interface bugs at build time instead of runtime
  • Prevent deployment of incompatible components
  • Improve developer experience with clear error messages
  • Enable reliable component composition with verified interfaces
  • Support CI/CD validation of WebAssembly component contracts

Related Work

  • Current validation implementation in rust/rust_wasm_component.bzl:83-134
  • C++ validation in cpp/defs.bzl:360-387
  • Documentation in docs-site/src/content/docs/languages/cpp.mdx#component-validation

Priority

Medium - Enhances developer experience but current manual validation provides basic functionality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions