Winit example window not opening on WSL2

550 views Asked by At

My ultimate goal is to use Pixels in WSL to create a data visualization in Rust.

However, I couldn't get the Pixels examples to run and so I first want to make sure Winit can run properly. Downloading the Winit repository from GitHub and running the window example works correctly.

'window' example running in the Winit source repository

However, if I create a new project and copy-paste the example code to it then running the code does not open a window anymore.

[![Same example code running in my own project](https://i.stack.imgur.com/slLOh.png)](https://i.stack.imgur.com/slLOh.png)

From running gdb, the code appears to get stuck in window.request_redraw() but I couldn't find much else out.

I am not super knowledgeable in windowing systems, but from running echo $DISPLAY and echo $WAYLAND_DISPLAY I get :0 and wayland-0, respectively, which I believe indicates that WSL has both X11 and Wayland functionality installed.

I am using Ubuntu 22.04.3 LTS. Running cat /proc/version prints Linux version 5.15.90.1-microsoft-standard-WSL2 (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220) #1 SMP Fri Jan 27 02:56:13 UTC 2023.

I seem to have some misunderstanding of how Cargo manages dependencies, since I don't know why the same code would run inside the Winit source project but not in my own project when I instruct Cargo to download the same version of the Winit source. I have tried Winit v0.29, v0.28, and v0.27 and the same problem persists.

Steps to reproduce:

git clone https://github.com/rust-windowing/winit.git
cd winit
cargo run --example window

window opens fine...

cd ..
cargo new window
cd window
cargo add winit
cargo add simple_logger
cp ../winit/examples/window.rs src/main.rs
mkdir src/util
cp ../winit/examples/util/fill.rs src/util
cargo run

window does not open...

1

There are 1 answers

0
Cosimos Cendo On

Solved!

TL;DR the example code had a feature flag that I didn't enable in my own project.


After fruitlessly debugging the main example code in window.rs I tackled the helper code in fill.rs. There is one function fill_window() with two signatures:

#[cfg(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios"))))]
pub(super) fn fill_window(window: &Window) {
    ...
}
    
#[cfg(not(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios")))))]
pub(super) fn fill_window(_window: &Window) {
    // No-op on mobile platforms.
}

The first one should run when the rwh_05 feature is enabled and target_os is not android or ios. Otherwise, the second one should run.

I put a println!() in each and discovered that my window project was compiling to the second (no-op) function while the source winit project was compiling to the first function.

So either target_os == android, target_os == ios, or rwh_05 is not enabled.

I ran some tests by adding code to fill.rs

#[cfg(target_os = "android")]
compile_error!("target_os = android");

#[cfg(target_os = "ios")]
compile_error!("target_os = android");

#[cfg(target_os = "linux")]
compile_error!("target_os = linux");

The output shows that target_os is linux.

error: target_os = linux
  --> src/util/fill.rs:16:1
   |
16 | compile_error!("target_os = linux");
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `window` (bin "window") due to previous error

Next,

#[cfg(feature = "rwh_05")]
compile_error!("rwh_05 feature enabled");

#[cfg(not(feature = "rwh_05"))]
compile_error!("rwh_05 feature is not enabled");

The output shows that rwh_05 is not enabledâ €

error: rwh_05 feature is not enabled
  --> src/util/fill.rs:16:1
   |
16 | compile_error!("rwh_05 feature is not enabled");
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `window` (bin "window") due to previous error

And this is where I had my revelation.

I had mistakenly put the following in Cargo.toml thinking that it would enable rwh_05 globally, when in reality it only enabled that feature for the winit crate and not my own local project.

[dependencies]
winit = { path = "./winit", features = ["rwh_05"] }

Adding

[features]
rwh_05 = []

to Cargo.toml and running with cargo run --features rwh_05 runs the window example as expected:

enter image description here