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

fix(rust): fix build for wasm #9502

Merged
merged 4 commits into from
Aug 16, 2023
Merged

fix(rust): fix build for wasm #9502

merged 4 commits into from
Aug 16, 2023

Conversation

lorepozo
Copy link
Contributor

Fixes issue #8783, where v0.29 broke the build for wasm. In addition to fixing the build, this adds a CI test to prevent regressions like this in the future. I've gone ahead and added every feature that works with wasm (not all features work out of the box and adding support for those can be done in the future as needed).

Relatedly there's an issue for the home crate rust-lang/cargo#12297 to make it compile for wasm.

@github-actions github-actions bot added fix Bug fix rust Related to Rust Polars labels Jun 22, 2023
@lorepozo
Copy link
Contributor Author

@ritchie46 would you review this change? Build failures are unrelated (probably just need to rebase)

Comment on lines 15 to 23
pub fn resolve_homedir(path: &Path) -> PathBuf {
// replace "~" with home directory
if path.starts_with("~") {
if let Some(homedir) = home_dir() {
// home crate does not compile on wasm https://github.com/rust-lang/cargo/issues/12297
#[cfg(not(target_arch = "wasm32"))]
if let Some(homedir) = home::home_dir() {
return homedir.join(path.strip_prefix("~").unwrap());
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably fine if we are just concerned with compiling, but since WASM doesn't have a filesystem, i'm wondering if resolve_homedir should be feature flagged instead? Likely anything that calls resolve_homedir will panic on wasm32 anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With WASI it could make sense for wasm to have a file system, and even a home dir. And generally this function can be used with paths that don't need homedir at all.

Seeing as it's likely the upstream will make this compile and return None on wasm rust-lang/cargo#12297, my proposed change will match that behavior and be safe to remove without consequences on behavior. This is also a reason not to add a feature flag. Ultimately that means we will fix the regression without changing behavior.

@lorepozo
Copy link
Contributor Author

@ritchie46 — would you please review this? I'm stuck on 0.28 because the build has been broken for wasm.

- name: Wasm
target: wasm32-unknown-unknown
features: >
-F abs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that familiar with this. What does -F mean in this context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is used further down on line 226 — it specifies a feature:

cargo build --target ${{ matrix.target }} -p polars --no-default-features ${{ matrix.features }}

@@ -5,6 +5,14 @@ impl Pool {
rayon::current_num_threads()
}

pub fn current_thread_index(&self) -> Option<usize> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added?

Copy link
Contributor Author

@lorepozo lorepozo Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this was necessary to build on wasm.

For context, crate::POOL is configured to a custom struct when building on wasm rather than the corresponding lazily-built rayon::ThreadPool.

In particular, this method is used in polars-core/src/frame/groupby/hashing.rs and polars-core/src/series/implementations/struct_.rs.

@lorepozo
Copy link
Contributor Author

Just rebased, no manual changes were necessary. @ritchie46 please take another look! Would like to fix the regression from 0.28.

@lorepozo lorepozo requested a review from orlp as a code owner August 8, 2023 11:42
@lorepozo
Copy link
Contributor Author

lorepozo commented Aug 8, 2023

rebased to deal with conflict from #10279 in crates/polars-io/Cargo.toml

Fixes issue #8783, where v0.29 broke the build for wasm.
In addition to fixing the build, this adds a CI test to prevent
regressions like this in the future.

Relatedly there's an issue for the `home` crate rust-lang/cargo#12297 to
make it compile for wasm. The previously used crate there, `dirs`,
_does_ support wasm (just returning None for the home dir).
@lorepozo
Copy link
Contributor Author

Rebased again — @orlp perhaps you can review?

Copy link
Collaborator

@universalmind303 universalmind303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable to me.

only suggestion I have is maybe moving the ci stuff to the Makefile so it is easier to build outside of CI as well.

@lorepozo
Copy link
Contributor Author

Thanks! It appears the Makefile is only used for working in py-polars, but the relevant libraries are part of the rust crates. I'm happy to put the ci stuff in a more appropriate place — I think the next best place to list out the features would be in Cargo.toml as a new feature like wasm-all, but that would add to the public API of the crate for the purposes of testing, which is usually a pattern to avoid. So I'm leaning towards keeping it in CI. I'm open to other ideas if there's another good place! (Or if you think a feature like wasm-all would be preferable to keeping the list in CI, I can move it there)

@orlp
Copy link
Collaborator

orlp commented Aug 14, 2023

@lorepozo

Thanks! It appears the Makefile is only used for working in py-polars, but the relevant libraries are part of the rust crates.

There is another Makefile in the crates folder, it can go there.

On another note, is there no other solution than to manually specify an entire list of feature flags? That's rather brittle and needs maintaining forever.

@lorepozo
Copy link
Contributor Author

There are a few other ways that come to mind:

  1. support all features on wasm
  2. have a smaller explicit list of supported features on wasm.
  3. use a regression testing framework that only fails version N+1 if version N worked under the same conditions (target + features).

I haven't looked into the difficulty of the first choice. The second choice risks regressions when other features lose support, and is hence tied to a judgement call I'd rather not make. I don't know of solutions for the third choice, though it seems technically solvable with the limited scope of cargo features across targets.

And just pushed the change for the Makefile.

@lorepozo
Copy link
Contributor Author

Actually, another option is to have a list of excludes — features that are not supported on wasm. That list would be smaller and more precisely capture where the problems exist. I'll try making that change now.

@lorepozo
Copy link
Contributor Author

Nice, it looks much better.

In case you're curious, most of the build failures are coming from mio.

@lorepozo
Copy link
Contributor Author

I can submit a small PR after this which will fix many of the mio issues so that dtype-full works on wasm. The problem stems from polars-pipe unconditionally depends on ipc/async of polars-io.

@lorepozo
Copy link
Contributor Author

@ritchie46 looks like this is ready to go — could you merge?

@orlp
Copy link
Collaborator

orlp commented Aug 16, 2023

That's a lot of excludes, but the solution to that isn't part of this pull request. Would love pull requests to reduce the amount of WASM-incompatible features.

@orlp orlp changed the title fix(rust) fix build for wasm fix(rust): fix build for wasm Aug 16, 2023
@orlp orlp merged commit be9ebb8 into pola-rs:main Aug 16, 2023
18 checks passed
orlp added a commit that referenced this pull request Aug 16, 2023
@orlp
Copy link
Collaborator

orlp commented Aug 16, 2023

@lorepozo I am reverting this because it broke our CI.

You can view the error log here: https://github.com/pola-rs/polars/actions/runs/5880942481/job/15948348046?pr=10388.

Could you make a new pull request, and test it first in CI on a fork?

@lorepozo
Copy link
Contributor Author

It was tested correctly in CI via this PR, looks like a merge race because of #10492 breaking the decompress feature on wasm. I'll create a new PR with that feature excluded as well.

@lorepozo lorepozo deleted the wasm branch August 16, 2023 16:28
@lorepozo lorepozo restored the wasm branch August 16, 2023 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants