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

Dart2Wasm compiler fails building when dart:ffi library is conditionally imported #55948

Closed
sroddy opened this issue Jun 6, 2024 · 15 comments
Closed
Assignees
Labels
area-dart2wasm Issues for the dart2wasm compiler.

Comments

@sroddy
Copy link

sroddy commented Jun 6, 2024

When compiling a simple flutter project in wasm using the latest dart stable version, the compiler will refuse building once a library that conditionally imports "dart:ffi" is imported and used.

simple steps to reproduce

main.dart:

import 'e.dart';

void main() {
  print(e());
}

e.dart:

export 'e_stub.dart' if (dart.library.ffi) 'e_ffi.dart';

e_stub.dart:

String e() => 'hello';

e_ffi.dart:

import 'dart:ffi';
String e() => 'hello';

interestingly enough, if the e.dart file is rewritten like this...

e.dart:

export 'e_ffi.dart' if (dart.library.js_interop) 'e_stub.dart';

...the compiler doesn't complain

@dcharkes
Copy link
Contributor

dcharkes commented Jun 10, 2024

@osa1 osa1 added the area-dart2wasm Issues for the dart2wasm compiler. label Jun 10, 2024
@mkustermann
Copy link
Member

This is a known issue and comes due to the fact that the library dart:ffi is actually available but we disallow importing it in newest versions (because we only have sketchy support for a subset of it atm - it's not generally working). It's not a specific issue to dart:ffi but also for dart:js / dart:js_util / ...

We could avoid this issue if we made it an internal library (i.e. dart:_ffi). Though this not so easy to do due to a few reasons (among them rolling such a change into flutter engine - which currently uses dart:ffi imports interop).

The conditional import mechanism works in the earlier stages than the backend mechanism that disallows importing certain (somewhat existing) libraries. We could investigate whether we can do that part earlier in the pipeline (/cc @osa1)

Using dart.library.js_interop is the condition we recommend for packages that work on web/js-environments.

mkustermann added a commit to mkustermann/engine that referenced this issue Jun 10, 2024
…nditional imports

Users of packages that have specialized code for the VM (which supports
FFI) use conditional imports based on `dart.library.ffi`. We don't want
the VM-specific code to be used for web in dart2wasm.

As a result we're going to make `dart.library.ffi` be false in
coditional imports (as well as in
`const bool.fromEnvironment('dart.library.ffi')`.

Issue dart-lang/sdk#55948
Issue flutter/flutter#149984
mkustermann added a commit to flutter/engine that referenced this issue Jun 10, 2024
…nditional imports (#53307)

Users of packages that have specialized code for the VM (which supports
FFI) use conditional imports based on `dart.library.ffi`. We don't want
the VM-specific code to be used for web in dart2wasm.

As a result we're going to make `dart.library.ffi` be false in
conditional imports (as well as in
`const bool.fromEnvironment('dart.library.ffi')`).

Issue dart-lang/sdk#55948
Issue flutter/flutter#149984
@mkustermann
Copy link
Member

I've made a few changes that will make us be able to evaluate the conditional import correctly (and thereby dart.library.ffi being false). Once cl/370580 lands and rolls up to flutter it should address these issues.

copybara-service bot pushed a commit that referenced this issue Jun 11, 2024
…ary.ffi`

This is a follow-up to [0]. That CL changed dart2wasm's modular
transformer to issue an error if `dart:ffi` is imported.

Users of packages that have specialized code for the VM (which supports
FFI) use conditional imports based on `dart.library.ffi`. We don't want
the VM-specific code to be used for web in dart2wasm (as dart2wasm
doesn't support the entirety of `dart:ffi`).

As a result we're going to make `dart.library.ffi` be false in
coditional imports (as well as in
`const bool.fromEnvironment('dart.library.ffi')`).

[0] https://dart-review.googlesource.com/c/sdk/+/368568

Issue #55948
Issue flutter/flutter#149984

Change-Id: I70a775278ab701d1fd2596521e378cb6364edac2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370580
Commit-Queue: Martin Kustermann <[email protected]>
Reviewed-by: Srujan Gaddam <[email protected]>
@mkustermann mkustermann self-assigned this Jun 11, 2024
@mkustermann
Copy link
Member

The CL landed and will make it's way up to flutter main channel in the coming days.

In the meantime I've filed for a cherry-pick request to stable channel in Issue 55979

copybara-service bot pushed a commit that referenced this issue Jun 11, 2024
…dart.library.ffi`

This is a follow-up to [0]. That CL changed dart2wasm's modular
transformer to issue an error if `dart:ffi` is imported.

Users of packages that have specialized code for the VM (which supports
FFI) use conditional imports based on `dart.library.ffi`. We don't want
the VM-specific code to be used for web in dart2wasm (as dart2wasm
doesn't support the entirety of `dart:ffi`).

As a result we're going to make `dart.library.ffi` be false in
coditional imports (as well as in
`const bool.fromEnvironment('dart.library.ffi')`).

[0] https://dart-review.googlesource.com/c/sdk/+/368568

Bug: #55948
Bug: flutter/flutter#149984

Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/370580
Cherry-pick-request: #55979
Change-Id: Ia968bacf92566d421606fc026c7f016fe5abff01
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370880
Reviewed-by: Slava Egorov <[email protected]>
@mkustermann
Copy link
Member

The fix for the conditional imports should be available in latest origin/main of flutter/flutter.
The fix was also cherry-picked onto Dart's stable channel and released under Dart SDK 3.4.4 yesterday.

It seems that flutter may not do hot fix releases on stable channel for 1-2 weeks, so it may take time until it arrives there.

@haorendashu
Copy link

@mkustermann Hello, can we rollback the local flutter version to avoid this issue? And whick version flutter can we rollback to?

@mkustermann
Copy link
Member

@mkustermann Hello, can we rollback the local flutter version to avoid this issue? And whick version flutter can we rollback to?

Due to incomplete and unstable ffi in dart2wasm we made two changes, here's the commits and how they rolled:

So you can use versions of flutter before those if you really want to. But there's a reason we have disallowed it, so I can discourage relying on it until we have a properly working FFI for the web.

@fvisticot
Copy link

@mkustermann Hello, can we rollback the local flutter version to avoid this issue? And whick version flutter can we rollback to?

Due to incomplete and unstable ffi in dart2wasm we made two changes, here's the commits and how they rolled:

So you can use versions of flutter before those if you really want to. But there's a reason we have disallowed it, so I can discourage relying on it until we have a properly working FFI for the web.

I'm a bit lost.
I have updated dart to 3.4.4 and try to update my web project including flutter_svg package (using vector_graphics_compiler)

I get the following error:


../.pub-cache/hosted/pub.dev/vector_graphics_compiler-1.1.11+1/lib/src/svg/_tessellator_ffi.dart:6:1: Error: 'dart:ffi' can't be imported when compiling to Wasm.
import 'dart:ffi' as ffi;
^
../.pub-cache/hosted/pub.dev/vector_graphics_compiler-1.1.11+1/lib/src/svg/_path_ops_ffi.dart:6:1: Error: 'dart:ffi' can't be imported when compiling to Wasm.
import 'dart:ffi' as ffi;
^

What can I do to fix this issue ?

@nietsmmar
Copy link

nietsmmar commented Jun 17, 2024

@fvisticot

What can I do to fix this issue ?

You have to build current flutter from git on your own. Or wait for the fix to be in the next version.

@mkustermann
Copy link
Member

I get the following error:

../.pub-cache/hosted/pub.dev/vector_graphics_compiler-1.1.11+1/lib/src/svg/_tessellator_ffi.dart:6:1: Error:
 'dart:ffi' can't be imported when compiling to Wasm.
import 'dart:ffi' as ffi;

What can I do to fix this issue ?

This particular issue in package:vector_graphics_compiler is due to a conditional import that was incorrectly evaluated in the compiler (see packages:vector_graphics_compiler/src/svg/tessellator.dart). The bug was fixed in 847c356 and should be available on flutter main/master channel. As mentioned above flutter may be slow atm to release new stable channel versions (which is why this fix isn't available there yet)

That being said, package:vector_graphics_compiler has other conditional imports (see e.g. package:vector_graphics_compiler/vector_graphics_compiler.dart) depending on dart.library.html (i.e. dart:html - the legacy API to talk to the DOM). We discourage usage of dart:html and instead want users to migrate to our new static JS interop APIs (and package:web which provides dom APIs using static interop). dart2wasm doesn't support dart:html - so this package would need to be migrated to static JS interop. Unfortunately the owner of this package is unavailable.

@mkustermann
Copy link
Member

/cc @kevmoo (see above)

@kevmoo
Copy link
Member

kevmoo commented Jun 17, 2024 via email

@chandrabezzo
Copy link

@mkustermann I try using 3.23.0-13.0.pre.337, but the issue still happen. I found the issue Error: 'dart:ffi' can't be imported when compiling to Wasm.. We need to improve the code for every package/plugin that we use?

@chandrabezzo
Copy link

But this issue fixed in 3.22.1

@mkustermann
Copy link
Member

But this issue fixed in 3.22.1
...
@mkustermann I try using 3.23.0-13.0.pre.337, but the issue still happen. I found the issue Error: 'dart:ffi' can't be imported when compiling to Wasm.. We need to improve the code for every package/plugin that we use?

Please see my response here: flutter/flutter#149984 (comment)

Locking this issue for more comments due to

  • master/main is working as intended (as far as I'm aware of)
  • the changes will eventually make it to stable channel (either via flutter team cherry-picking new dart stable version or making new stable branch cut)
  • The problem with package:flutter_svg and package:vector_graphics_compiler needs changes in those packages, which is tracked ffi imported when compiling for web wasm dnfield/vector_graphics#244

@dart-lang dart-lang locked as resolved and limited conversation to collaborators Jun 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-dart2wasm Issues for the dart2wasm compiler.
Projects
None yet
Development

No branches or pull requests

9 participants