vionya / discord-rich-presence

A cross-platform Discord Rich Presence library written in Rust
MIT License
89 stars 16 forks source link

Error when compiling to WASI #26

Closed jesus-ale43 closed 1 year ago

jesus-ale43 commented 1 year ago

I am trying to make a plugin for the Lapce editor, it compiles plugins to WASI (WebAssembly System Interface). Because the editor is relatively new, there is not much information about plugin development.

The problem is, when I use the package, and compile it to WASI, returns 2 errors:

error[E0432]: unresolved import `ipc`                                                                                                                                   
  --> C:\Users\[USERNAME]\.cargo\registry\src\github.com-1ecc6299db9ec823\discord-rich-presence-0.2.3\src\lib.rs:37:9
   |
37 | pub use ipc::DiscordIpcClient;
   |         ^^^ use of undeclared crate or module `ipc`

error[E0433]: failed to resolve: use of undeclared crate or module `ipc`
  --> C:\Users\[USERNAME]\.cargo\registry\src\github.com-1ecc6299db9ec823\discord-rich-presence-0.2.3\src\lib.rs:48:5
   |
48 |     ipc::DiscordIpcClient::new(client_id)
   |     ^^^ use of undeclared crate or module `ipc`

This is the code I am compiling:

//When the user opens Lapce execute this
fn initialize(params: InitializeParams) -> Result<()> {
    let mut client = DiscordIpcClient::new("1000000000000000000")?;

    client.connect()?;
    client.set_activity(activity::Activity::new()
        .state("Testing")
        .details("RPC Rust")
    )?;
    client.close()?;

    Ok(())
}

Or you can also look at the repository.

image image

vionya commented 1 year ago

Thanks for the issue!

I think I can diagnose at least half of the problem here. Because of different OS APIs, the library dynamically imports either the unix or windows implementation of the underlying IPC client. It looks like the wasm matches neither windows nor unix.

That alone could be fixed. However, I did some testing myself, and found that there are several issues that I do not see a solution for:

  1. WASI appears to require explicit permission to access directories in intends to work with. I'm not sure what this entails for your use case specifically, but it would require granting explicit permission to the named pipe directory
  2. Needs to know the named pipe directory. This is problematic because named pipes are in different places in different OS's, and since there's no way to distinguish between WASM on windows vs WASM on unix, it would be difficult to work around this
  3. Lack of proper APIs. Assuming the first 2 points were resolved, there's still the issue of Windows and Unix having entirely different APIs for the Discord IPC pipe (i.e. Unix needs the UnixStream struct and Windows needs extended open options). Cross compiling to WASM makes it impossible to use these, since they are not made available for its target OS. And, since they work so differently across different systems, I don't see a way to make this possible

All that said, I'd love to make it work. If there's something I missed that solves all of the points I listed, I'll make it work. But, as far as I can see, there's no clear way to solve your issue, unfortunately. Sorry for the inconvenience :(