Skip to content

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

@sroddy

Description

@sroddy

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

Activity

dcharkes

dcharkes commented on Jun 10, 2024

@dcharkes
Contributor
mkustermann

mkustermann commented on Jun 10, 2024

@mkustermann
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

mkustermann commented on Jun 10, 2024

@mkustermann
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.

self-assigned this
on Jun 11, 2024
mkustermann

mkustermann commented on Jun 11, 2024

@mkustermann
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

mkustermann

mkustermann commented on Jun 13, 2024

@mkustermann
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

haorendashu commented on Jun 17, 2024

@haorendashu

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

mkustermann

mkustermann commented on Jun 17, 2024

@mkustermann
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

fvisticot commented on Jun 17, 2024

@fvisticot

@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

nietsmmar commented on Jun 17, 2024

@nietsmmar

@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

mkustermann commented on Jun 17, 2024

@mkustermann
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

mkustermann commented on Jun 17, 2024

@mkustermann
Member

/cc @kevmoo (see above)

kevmoo

kevmoo commented on Jun 17, 2024

@kevmoo
Member
chandrabezzo

chandrabezzo commented on Jun 26, 2024

@chandrabezzo

@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

chandrabezzo commented on Jun 26, 2024

@chandrabezzo

But this issue fixed in 3.22.1

mkustermann

mkustermann commented on Jun 26, 2024

@mkustermann
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
locked as resolved and limited conversation to collaborators on Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-dart2wasmIssues for the dart2wasm compiler.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @kevmoo@fvisticot@osa1@sroddy@haorendashu

      Issue actions

        Dart2Wasm compiler fails building when dart:ffi library is conditionally imported · Issue #55948 · dart-lang/sdk