simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.65k stars 370 forks source link

CopyCompiledJs Example not working with Flutter 3.7.0 Trival fix? #2285

Closed rokk4 closed 1 year ago

rokk4 commented 1 year ago

Hi simolus, first of all thanks you so much for this packages. I think its a huge building block in the ecosystem.

  drift: ^2.4.2
  sqlcipher_flutter_libs: ^0.5.1
  drift_dev: ^2.4.1

Since I have updated Flutter from. 3.3.X to 3.7, the CopyCompiledJs example stopped working with the following error:

r0@Mac-mini therapy_lift % flutter pub run build_runner build --release --delete-conflicting-outputs
[INFO] Generating build script...
[INFO] Generating build script completed, took 201ms

[INFO] Precompiling build script......
[WARNING] .dart_tool/build/entrypoint/build.dart:195:11: Error: A value of type 'CopyCompiledJs Function()' can't be assigned to a variable of type 'Builder Function(BuilderOptions)'.
 - 'CopyCompiledJs' is from 'tool/builder.dart'.
 - 'Builder' is from 'package:build/src/builder/builder.dart' ('../../.pub-cache/hosted/pub.dev/build-2.3.1/lib/src/builder/builder.dart').
 - 'BuilderOptions' is from 'package:build/src/builder/builder.dart' ('../../.pub-cache/hosted/pub.dev/build-2.3.1/lib/src/builder/builder.dart').
    [_i13.CopyCompiledJs.new],
          ^
[INFO] Precompiling build script... completed, took 725ms

[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.

pub finished with exit code 78

I added the

BuilderOptions options

to the constructor and than the build ran just fine.

Should I add a pull request for this?

class CopyCompiledJs extends Builder {
  CopyCompiledJs(BuilderOptions options);
  @override
  Future<void> build(BuildStep options) async {
    final inputId = AssetId(buildStep.inputId.package, 'web/worker.dart.js');
    final input = await buildStep.readAsBytes(inputId);
    await buildStep.writeAsBytes(buildStep.allowedOutputs.single, input);
  }

  @override
  Map<String, List<String>> get buildExtensions => {
        r'$package$': ['web/shared_worker.dart.js']
      };
}

Also does my build.yaml look okay / up to date?

cheers :)

targets:
  $default:
    # Reducing sources makes the build slightly faster (some of these are required
    # to exist in the default target).
    sources:
      - lib/**
      - web/**
      - "tool/**"
      - pubspec.yaml
      - lib/$lib$
      - $package$
    builders:
      drift_dev:
        # These options change how drift generates code
        options:
          # Drift analyzes SQL queries at compile-time. For this purpose, it needs to know which sqlite3
          # features will be available. We depend on `sqlite3_flutter_libs`, which lets us use the latest
          # version with fts5 enabled.
          sql:
            dialect: sqlite
            options:
              version: "3.38"
              modules:
                - json1
                - fts5
          # This allows us to share a drift database across isolates (or different tabs on the web)
          generate_connect_constructor: true
          # These options are generally recommended: https://drift.simonbinder.eu/docs/advanced-features/builder_options/#recommended-options
          apply_converters_on_variables: true
          generate_values_in_copy_with: true
          scoped_dart_components: true
          write_from_json_string_constructor: true

      # Configuring this builder isn't required for most apps. In our case, we
      # want to compile the web worker in `web/worker.dart` to JS and we use the
      # build system for that.
      build_web_compilers|entrypoint:
        generate_for:
          - web/worker.dart
        options:
          compiler: dart2js
        dev_options:
          dart2js_args:
            - --no-minify
        release_options:
          dart2js_args:
            - -O1  ## >O2 will cause issues with encryption on web
      "|copy_compiled_worker_js":
        enabled: true

      injectable_generator:injectable_builder:
        options:
          runs_before:
            - auto_route_generator
      json_serializable:
        options:
          field_rename: snake
          explicit_to_json: true
          any_map: true
      freezed:
        options:
          union_key: runtimeType
          union_value_case: none
          make_collections_unmodifiable: false
 #     build_modules:module_library:
 #       generate_for:
 #         - web/**.dart
builders:
  copy_compiled_worker_js:
    import: 'tool/builder.dart'
    builder_factories: [ "CopyCompiledJs.new" ]
    build_to: source
    build_extensions: { 'web/worker.dart.js': [ 'web/shared_worker.dart.js' ] }
    required_inputs: [ ".dart.js" ]
 flutter doctor -v 
[✓] Flutter (Channel stable, 3.7.0, on macOS 13.1 22C65 darwin-arm64, locale en-DE)
    • Flutter version 3.7.0 on channel stable at /Users/r0/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b06b8b2710 (3 days ago), 2023-01-23 16:55:55 -0800
    • Engine revision b24591ed32
    • Dart version 2.19.0
    • DevTools version 2.20.1

[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/r0/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/r0/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/r0/Library/Android/sdk
    • Java binary at: /Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (Temurin)(build 1.8.0_302-b08)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] VS Code (version 1.74.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.58.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.1 22C65 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 109.0.5414.119

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
simolus3 commented 1 year ago

I added the

BuilderOptions options

to the constructor and than the build ran just fine.

This is also what we're doing here:

https://github.com/simolus3/drift/blob/fe8611c5f49029650922832b5e50bbaed14eb962/examples/app/tool/builder.dart#L6-L7

So it seems correct :) If there's another place where I've missed this, a PR to fix this would be great.

Also does my build.yaml look okay / up to date?

It looks good. The apply_converters_on_variables, generate_values_in_copy_with and scoped_dart_components options are now enabled by default, so you can remove them from your build configuration. I'll also make that change in the repository.