dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
628 stars 172 forks source link

dart:html disallowed in Flutter web package #2651

Open tomgilder opened 3 years ago

tomgilder commented 3 years ago

Describe the issue My package is getting marked down due to avoid_web_libraries_in_flutter, and I don't think it should: https://pub.dev/packages/routemaster/score.

avoid_web_libraries_in_flutter disallows import of dart:html unless the pubspec.yaml specifies it's a web plugin.

My package does support web, and needs to import dart:html to access the browser DOM. I don't believe I can specify it's a web plugin in pubspec.yaml as it doesn't have any platform-specific code for the other platforms.

I have another production Flutter web app that needs to import dart:html, I don't believe this is an edge case.

To Reproduce

  1. Create a non-federated package that supports Flutter web
  2. Import dart:html (via conditional exports - e.g. export 'blah_vm.dart' if (dart.library.js) 'blah_web.dart')
  3. Run pana

Expected behavior dart:html is allowed

pq commented 3 years ago

/cc @goderbauer

pq commented 3 years ago

/fyi @jonasfj

goderbauer commented 3 years ago

I am not too familiar with that lint.

@yjbanov Can you say whether that lint is maybe too strict for the web?

tomgilder commented 3 years ago

I've added a hacky workaround to remove this warning, but you can still see it on previous versions: https://pub.dev/packages/routemaster/versions/0.8.0/score

goderbauer commented 3 years ago

@pq Do you know why pub.dev warns about avoid_web_libraries_in_flutter in the first place? I thought scoring is done with score.yaml, which doesn't include this lint?

pq commented 3 years ago

Honestly I'm confused by that too.

@jonasfj ?

jonasfj commented 2 years ago

pana used to score using lints from flutter -> https://github.com/flutter/packages/blob/master/packages/flutter_lints/lib/flutter.yaml#L9

I think we should ask flutter if this lint still makes sense.


I think pana now uses https://github.com/dart-lang/lints/blob/main/lib/recommended.yaml

(pana does scoring on pub.dev)

igoriuz commented 2 years ago

I'm having a similar problem now. Everything worked with routemaster before i added drift package for persistence. If i follow drift docs, i need to run build_web_compilers. But it throws a similar error described in this issue.

[INFO] Running build...
[SEVERE] build_modules:module_library on package:routemaster/src/system_nav_web.dart:

Invalid argument(s): Unsupported conditional import of `dart:html` found in routemaster|lib/src/system_nav_web.dart:

import 'fake_html.dart' if (dart.library.js) 'dart:html';

This environment does not support direct conditional imports of `dart:` libraries. Instead you must create a separate library which unconditionally imports (or exports) the `dart:` library that you want to use, and conditionally import (or export) that library.
[WARNING] build_web_compilers:dart2js_modules on lib/.dart2js.meta_module.raw:
Unable to read module information for package:*, make sure you have a dependency on it in your pubspec.
[INFO] Running build completed, took 967ms

Edit: I realized this is the linter repository. I should probably open an issue in dart-lang/build?

Gieted commented 1 year ago

The same problem. Flutter should support both web and native in the same project, so this warning doesn't make sense

obraz

alefwd commented 2 months ago

Same problem here, I'm adding a wrapper around some calls to see if our app can run on web, and then I ended up into this error. From my point of view, it doesn't make much sense, as the call is conditionally handled under kIsWeb

Screenshot 2024-04-23 at 15 20 21
aike1202 commented 2 months ago

Same problem here, I'm adding a wrapper around some calls to see if our app can run on web, and then I ended up into this error. From my point of view, it doesn't make much sense, as the call is conditionally handled under kIsWeb

Screenshot 2024-04-23 at 15 20 21

same problem ,du you have any soulution yet

Gieted commented 2 months ago

@aike1202 As a workaround, you can just disable this check in linter settings (inside analysis_options.yaml):

linter:
  rules:
    avoid_web_libraries_in_flutter: false