dart-lang / build

A build system for Dart written in Dart
https://pub.dev/packages/build
BSD 3-Clause "New" or "Revised" License
791 stars 211 forks source link

[build_web_compilers] Crashes due to wrongly analyzed libraries. #3666

Closed schultek closed 8 months ago

schultek commented 8 months ago

I came across a super weird bug that causes this error:

[WARNING] build_web_compilers:entrypoint on web/main.dart:
Skipping compiling myapp|web/main.dart with ddc because some of its
transitive libraries have sdk dependencies that not supported on this platform:

myapp|lib/main.dart

https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-skipped-compiling-warnings

Which is wrong since the file in question definitely does not import any unsupported libraries. It is caused by some other file in the package that imports some unsupported libs, but that is not anywhere in the transitive imports of the entrypoint file.


> dart info

If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

#### General info

- Dart 3.3.1 (stable) (Wed Mar 6 13:09:19 2024 +0000) on "macos_arm64"
- on macos / Version 14.1 (Build 23B74)
- locale is de-DE

#### Project info

- sdk constraint: '^3.3.1'
- dependencies: web
- dev_dependencies: build_runner, build_web_compilers, lints

#### Process info

|  Memory |  CPU | Elapsed time | Command line                                                                               |
| ------: | ---: | -----------: | ------------------------------------------------------------------------------------------ |
| 3674 MB | 0.6% |     16:24:04 | dart language-server --client-id=IntelliJ-IDEA --client-version=IU-232.10203.10 --protocol=analyzer |
|   88 MB | 0.0% |     04:06:19 | dart language-server --client-id=IntelliJ-IDEA --client-version=IU-232.10203.10 --protocol=analyzer |
|   32 MB | 0.0% |     16:24:08 | flutter_tools.snapshot daemon                                                              |
|   41 MB | 0.0% |     04:06:20 | flutter_tools.snapshot daemon                                                              |

I found the following reproducible setup:

  1. dart create -t web myapp
  2. Create file lib/src/web.dart with the content
      String webFunction() => "web";
  3. Create file lib/server.dart with the content

    import 'dart:io';
    import 'src/web.dart';
    
    void main() {
      print(Platform.localHostname);
      print(webFunction());
    }
  4. Change web/main.dart contents to

    import 'package:myapp/src/web.dart';
    import 'package:web/helpers.dart';
    
    void main() {
      final element = document.querySelector('#output') as HTMLDivElement;
      element.text = webFunction();
    }
    1. Run dart run build_runner serve web:8080

You should see the skipped compilation warning for web/main.dart although it has no transitive dependency on dart:io.


The bug seems to be somehow related to the src directory:

jakemac53 commented 8 months ago

Yes, this happens because of the import to src. We group files together into "modules" to optimize compilation, and that grouping is based on the public imports of the package, as well as things like any file containing a main function gets its own module.

This means any time you have a src/ import that you might end up actually pulling in a module which contains additional libraries which were not depended on by the actual thing you imported.

You can switch to using the "fine" module strategy, where we don't do this grouping, but it will result in slower builds and a LOT more JavaScript files.

In your build.yaml file, something like this (not tested):

targets:
  $default:
    builders:
      build_web_compilers:ddc_modules:
        options:
          strategy: fine

The better option though, would be to make a public entrypoint (lib/web.dart) etc, as you have discovered.

schultek commented 8 months ago

Interesting. Thanks for your detailed response.

Can you point me to somewhere where this is documented?

I discovered this while building a jaspr integration, so I cannot fix this problem myself but would need to communicate this to the developers using the framework.

jakemac53 commented 8 months ago

We should probably add a section to https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-skipped-compiling-warnings - I am surprised it doesn't already talk about this

schultek commented 8 months ago

That would be great 😄