sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Unclear error message when using Dart web core libraries in Dart native

Open mit-mit opened this issue 4 years ago • 7 comments

If one imports a Dart web dart: library and tried to run it in a Dart native runtime, an error like this is shown during compilation:

$ dart run
../../.pub-cache/hosted/pub.dartlang.org/js-0.6.3/lib/js.dart:8:1: Error: Not found: 'dart:js'
export 'dart:js' show allowInterop, allowInteropCaptureThis;
^

Could we say something more specific like this?

../../.pub-cache/hosted/pub.dartlang.org/js-0.6.3/lib/js.dart:8:1: Error: The 'dart:js' library is not supported in Dart native.
export 'dart:js' show allowInterop, allowInteropCaptureThis;

Here's the list of core libraries I think this makes sense for (the last table on the page): https://dart.dev/guides/libraries#web-platform-libraries

mit-mit avatar Sep 21 '21 12:09 mit-mit

A better error message could be

The web platform library 'dart:js' is not available when compiling to native code.

or, for the other direction:

The native platform library dart:isolate is not available when compiling to web code.

It requires the front-end (I guess) to be aware of all dart: libraries and the platforms they are available for. (If we get more target platforms and libraries available to more than one, but not all, platforms, then we'll need a different wording - possibly just dropping the initial "web"/"native" before "platform library".)

lrhn avatar Sep 21 '21 12:09 lrhn

@johnniwinther wdyt?

mit-mit avatar Jan 04 '22 13:01 mit-mit

We should make this change but it might take while due to technical difficulties.

johnniwinther avatar Jan 04 '22 15:01 johnniwinther

My team is getting same issue today, when trying to build for iOS & Android.

It got fixed when we removed this line from js.dart which is there in pub-cache

export 'dart:js' show allowInterop, allowInteropCaptureThis;

@johnniwinther any help please

krishnatejakanchi avatar Sep 15 '22 12:09 krishnatejakanchi

@krishnatejakanchi you are importing some package that is only going to work on the web (i.e. some of your code is importing package:js/js.dart directly or indirectly). Editing .pub_cache is wrong and you should never do that. The real solution is to identify incorrect dependency.

mraleph avatar Sep 15 '22 12:09 mraleph

I tried running flutter pub deps and got some packages that are dependent on js

build_runner
firebase_core
flutter_native_splash
image_picker
chewie
gleap_sdk

@mraleph Can you please suggest a way, how can i find that incorrect dependency.

krishnatejakanchi avatar Sep 15 '22 13:09 krishnatejakanchi

@krishnatejakanchi unfortunately there is no easy way to find this dependency AFAIK. The simplest approach you could try is just rename js.dart in your .pub-cache to js.dart.bak (yeah, I know I did not recommend doing that before) and then run build targeting Android/iOS. This should error on the file which tries to import js.dart.

mraleph avatar Sep 15 '22 14:09 mraleph

@johnniwinther I would like to suggest a bunch of improvements for the CFE error messaging (if feasible):

  • When importing a platform specific library that does not exist on target platform issue a human readable error message (e.g. dart:js is only available when targeting the web, but this build is targeting native platform. Native platform does not contain a builtin JS runtime and as such can't interoperate with JS libraries.)
  • Visually split errors that originate from .pub-cache dependencies and errors that originate from entry point package itself, e.g. have a header: The following errors occurred when compiling dependencies of your project.
  • Provide import path chain that lead to compilation of a specific dependency: The following errors occured when compiling library 'package:xxx/xxx.dart'. This library is imported through the following sequence of imports: ...

As more and more people starting to use Dart in multiplatform environment it would be great to have better error messaging in place.

mraleph avatar Sep 26 '22 07:09 mraleph

WIP: https://dart-review.googlesource.com/c/sdk/+/261821 that shows the import path (in case of missing dart: libraries for now only)

johnniwinther avatar Oct 27 '22 13:10 johnniwinther