Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASI compilation? #6518

Closed
Timmmm opened this issue Mar 22, 2025 · 6 comments
Closed

WASI compilation? #6518

Timmmm opened this issue Mar 22, 2025 · 6 comments

Comments

@Timmmm
Copy link
Contributor

Timmmm commented Mar 22, 2025

I'm trying to compile this for wasm32-wasip2 and not getting very far. Do you guys know if this is possible? I got as far as:

rustup toolchain install nightly
rustup +nightly target add wasm32-wasip2
rustup component add rust-src rustc-dev llvm-tools-preview

git clone https://github.com/rust-lang/rustfmt.git
cd rustfmt

rustup override set nightly

cargo build --release --target wasm32-wasip2

However the first error is:

   Compiling rustfmt-nightly v1.8.0 (C:\workspace\rustfmt)
error[E0463]: can't find crate for `rustc_ast`
 --> src\lib.rs:8:1
  |
8 | extern crate rustc_ast;
  | ^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
  |
  = help: maybe you need to install the missing components with: `rustup component add rust-src rustc-dev llvm-tools-preview`

(I have already tried the suggestion.)

Seems like @alexcrichton got this to work a long time ago - see #4358.

@Timmmm
Copy link
Contributor Author

Timmmm commented Mar 22, 2025

Ah I think the issue is rustup component add ... rustc-dev adds it for the native architecture. I need rustc-dev for wasm32-wasip2 but it doesn't seem to be available. I guess I'll need to build it from source somehow...

@ytmimi
Copy link
Contributor

ytmimi commented Mar 22, 2025

I'm not sure if this is possible. Also, rustfmt dynamically links against some rustc internals, and I'm not sure if that's going to cause you any issues when you actually go to run rustfmt.

@Timmmm
Copy link
Contributor Author

Timmmm commented Mar 22, 2025

Does it really dynamically link against rustc? Can you point me to the code if so?

I've got pretty far into the compilation (279/290) with this addition to Cargo.toml:

# Rustc dependencies are loaded from the sysroot, Cargo doesn't know about them.

# For WASI however we need to compile them from source since the Rust compiler
# isn't built for WASI.
rustc_ast = { git = "https://github.com/rust-lang/rust.git" }
rustc_ast_pretty = { git = "https://github.com/rust-lang/rust.git" }
rustc_builtin_macros = { git = "https://github.com/rust-lang/rust.git" }
rustc_data_structures = { git = "https://github.com/rust-lang/rust.git" }
rustc_errors = { git = "https://github.com/rust-lang/rust.git" }
rustc_expand = { git = "https://github.com/rust-lang/rust.git" }
rustc_parse = { git = "https://github.com/rust-lang/rust.git" }
rustc_session = { git = "https://github.com/rust-lang/rust.git" }
rustc_span = { git = "https://github.com/rust-lang/rust.git" }
# thin_vec = { git = "https://github.com/rust-lang/rust.git" }

# For WASI we need to enable the nightly feature of `tempfile` (used by rustc)
# so that it can use the wasip2 feature.
tempfile = { version = "*", features = ["nightly"] }

Current error is:

error[E0425]: cannot find function `current_dll_path` in this scope
   --> C:\Users\Tim\.cargo\git\checkouts\rust-a4081372e9049d90\d93f678\compiler\rustc_session\src\filesearch.rs:164:16
    |
164 |     let path = current_dll_path().and_then(|s| try_canonicalize(s).map_err(|e| e.to_string()));
    |                ^^^^^^^^^^^^^^^^ not found in this scope

I guess I will have to do some hackery on rustc unfortunately.

@ytmimi
Copy link
Contributor

ytmimi commented Mar 22, 2025

It's the rustc_driver I believe. I don't remember all the details, but it's the reason that we can't easily distribute a standalone binary for rustfmt outside of rustup since you need to have the correct tool chain installed.

@Timmmm
Copy link
Contributor Author

Timmmm commented Mar 22, 2025

That's unfortunate. Do you know if there are any plans to disentangle rustfmt from rustc?

@ytmimi
Copy link
Contributor

ytmimi commented Mar 23, 2025

Not to my knowledge, no.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants