winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
525 stars 40 forks source link

Wasm-pack build error when use winnow debug feature #430

Open baoyachi opened 8 months ago

baoyachi commented 8 months ago

Please complete the following tasks

rust version

rustc 1.74.0 (79e9716c9 2023-11-13)

winnow version

0.5.34

Minimal reproducible code

I hope to use debug in wasm to debug and output information. Currently, using the debug feature will result in errors. Can we collect trace information somewhere, not necessarily at the terminal

See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib] crate-type = ["cdylib", "rlib"]

[dependencies] winnow = {version = "0.5.34",features = ["debug"]}


* lib.rs
```rust
fn hello() {}

cargo tree

➜  demo git:(main) ✗ cargo tree
demo v0.1.0 (/Users/baoyachi/demo)
└── winnow v0.5.34
    ├── anstream v0.3.2
    │   ├── anstyle v1.0.4
    │   ├── anstyle-parse v0.2.3
    │   │   └── utf8parse v0.2.1
    │   ├── anstyle-query v1.0.2
    │   ├── colorchoice v1.0.0
+    │   ├── is-terminal v0.4.10
    │   │   └── rustix v0.38.30
    │   │       ├── bitflags v2.4.2
+    │   │       ├── errno v0.3.8
    │   │       │   └── libc v0.2.152
    │   │       └── libc v0.2.152
    │   └── utf8parse v0.2.1
    ├── anstyle v1.0.4
    ├── is-terminal v0.4.10 (*)
    └── terminal_size v0.2.6
        └── rustix v0.37.27
            ├── bitflags v1.3.2
            ├── errno v0.3.8 (*)
            ├── io-lifetimes v1.0.11
            │   └── libc v0.2.152
            └── libc v0.2.152

Steps to reproduce the bug with the above code

wasm-pack build --target bundler

Actual Behaviour

➜  demo git:(main) ✗ wasm-pack build --target bundler 
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
   Compiling errno v0.3.8
error[E0583]: file not found for module `sys`
  --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:26:1
   |
26 | mod sys;
   | ^^^^^^^^
   |
   = help: to create the module `sys`, create file "/Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/sys.rs" or "/Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/sys/mod.rs"

error[E0425]: cannot find function `with_description` in module `sys`
  --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:47:14
   |
47 |         sys::with_description(*self, |desc| {
   |              ^^^^^^^^^^^^^^^^ not found in `sys`

error[E0425]: cannot find function `with_description` in module `sys`
  --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:58:14
   |
58 |         sys::with_description(*self, |desc| match desc {
   |              ^^^^^^^^^^^^^^^^ not found in `sys`

error[E0425]: cannot find value `STRERROR_NAME` in module `sys`
  --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:64:22
   |
64 |                 sys::STRERROR_NAME,
   |                      ^^^^^^^^^^^^^ not found in `sys`

error[E0425]: cannot find function `errno` in module `sys`
  --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:95:10
   |
95 |     sys::errno()
   |          ^^^^^ not found in `sys`

error[E0425]: cannot find function `set_errno` in module `sys`
   --> /Users/baoyachi/.cargo/registry/src/errno-0.3.8/src/lib.rs:100:10
    |
100 |     sys::set_errno(err)
    |          ^^^^^^^^^ not found in `sys`

Some errors have detailed explanations: E0425, E0583.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `errno` (lib) due to 6 previous errors
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/Users/baoyachi/demo" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
➜  demo git:(main) ✗ 

Expected Behaviour

Expected build ok.

Additional Context

No response

baoyachi commented 8 months ago

If possible, I can provide support. @epage

epage commented 8 months ago

If its just is-terminal, an MSRV bump will make that dependency go away.

I haven't settled on an MSRV policy yet for this package but wanted a bit wider of a window since people were used to that with nom. We are now 5 releases past 1.70, so maybe its ok?

I'm also curious why you are wanting to support debug in wasm? debug is not for production code and I would assume development could easily run a parser on the host as well as a wasm target.

baoyachi commented 8 months ago

Currently, I have used winnow to develop a parsing engine for a domain-specific language (DSL). The DSL parsing scripts are authored within a web page, and when I need to understand the entire parsing process to pinpoint and troubleshoot issues, winnow's trace functionality effectively helps me display this information on the webpage itself. I no longer need to repeatedly debug the Rust program; instead, I can directly view verbose tracing output to resolve problems.

So, if winnow's trace capability could be enhanced such that it collects tracing information at specific points without necessarily outputting to the terminal, and instead relays this information back to JavaScript through WebAssembly (WASM), the web page would then be able to display the verbose trace output.

epage commented 8 months ago

Is this a "development" page where the traces being there, but disabled, is fine? Or is this where you compile two different instances of the parser, one with debug and one without, and switch between them?

baoyachi commented 8 months ago

Yes, in the binary file compiled by Rust, the winnow debug feature is disabled for the service running on the real server. However, in the compiled wasm, the winnow debug feature is enabled.

epage commented 8 months ago

I'm hesitant. This was originally designed to just be for quick, one-off debugging.

To expose this for use cases like this would require

Thats a large API surface for what feels like a fairly small use case that we then would be striving to maintain compatibility on.

Maybe an approach to take is to allow you to experiment on such a design behind a unstable-debug-sink feature and then we can evaluate whether its worth stabilizing or removing.

EDIT: I wish we had language-level registration system...

baoyachi commented 6 months ago

EDIT: I wish we had language-level registration system...

Can you provide some examples or ideas for the registration system here. Or provide a reference

epage commented 6 months ago

Can you provide some examples or ideas for the registration system here. Or provide a reference

I don't remember enough of the details but there has been talk along these lines for async runtime registration.

I wonder if distributed_slice (rust-lang/testing-devex-team#3) could be used as it allows a dependent to inject a value into a dependency. You could have a static value that and then inject a reference it into the slice. We could pick the first item found or do a const assert if there are multiple items.