drift icon indicating copy to clipboard operation
drift copied to clipboard

Could not format generated source. The generated code is probably invalid, and this is most likely a bug in drift_dev.

Open AliEasy opened this issue 2 years ago • 12 comments

Hi

database.dart

// @dart=2.12

import 'package:drift/drift.dart';

import 'connection/connection.dart' as impl;

part 'database.g.dart';

@DriftDatabase(include: {'tables.drift'})
class AppDatabase extends _$AppDatabase {
  AppDatabase() : super(impl.connect());

  @override
  int get schemaVersion => 1;
}

table.drift

import 'package:app/model/register.dart';

CREATE TABLE Register (
        Id INTEGER PRIMARY KEY, Download TEXT, ExtraCode TEXT, WcfURL TEXT, CompanyName TEXT, CompanyId INTEGER,
        CompanyPersonId INTEGER, ImeiCode TEXT, RegisterCode TEXT
) WITH RegisterModel;

register.dart

class RegisterModel {
  String download;
  String extraCode;
  String wcfUrl;
  String companyName;
  int companyId;
  int companyPersonId;
  String imeiCode;
  String registerCode;

  RegisterModel(this.download, this.extraCode, this.wcfUrl, this.companyName,
      this.companyId, this.companyPersonId);

}

After running flutter pub run build_runner build --delete-conflicting-outputs the below Warning shows up in terminal:

[WARNING] drift_dev on lib/app/databse/tables.drift:
line 11, column 53 of package:app/model/register.dart: Unexpected parameter wcfUrl which has no matching column.
   ╷
11 │   RegisterModel(this.download, this.extraCode, this.wcfUrl, this.companyName,
   │                                                     ^^^^^^
   ╵

[WARNING] drift_dev on lib/app/databse/database.dart:
Could not format generated source. The generated code is probably invalid, and this is most likely a bug in drift_dev.

Its is a warning but the generated database.g.dart file is broken:

errr

AliEasy avatar May 03 '23 11:05 AliEasy

You forgot to add the Id column in your model.

FaFre avatar May 03 '23 11:05 FaFre

After adding Id

register.dart

class RegisterModel {
  int id;
  String download;
  String extraCode;
  String wcfUrl;
  String companyName;
  int companyId;
  int companyPersonId;
  String imeiCode;
  String registerCode;

  RegisterModel(
      this.id,
      this.download,
      this.extraCode,
      this.wcfUrl,
      this.companyName,
      this.companyId,
      this.companyPersonId,
      this.imeiCode,
      this.registerCode);
}

terminal log

line 16, column 12 of package:app/model/register.dart: Unexpected parameter wcfUrl which has no matching column.
   ╷
16 │       this.wcfUrl,
   │            ^^^^^^
   ╵
[WARNING] drift_dev on lib/app/databse/database.dart:
Could not format generated source. The generated code is probably invalid, and this is most likely a bug in drift_dev.

database.g.dart

errr

AliEasy avatar May 03 '23 11:05 AliEasy

I even tried a simpler table:

tables.drift:

import 'package:app/model/combo.dart';

CREATE TABLE Combo (
        ComboId INTEGER PRIMARY KEY, Value TEXT NOT NULL
) WITH ComboModel;

combo.dart:

class ComboModel {
  int comboId;
  String value;

  ComboModel(this.comboId, this.value);
}

terminal log: (Only one warning this time)

[WARNING] drift_dev on lib/app/databse/database.dart:
Could not format generated source. The generated code is probably invalid, and this is most likely a bug in drift_dev.

database.g.dart:

errr

AliEasy avatar May 03 '23 11:05 AliEasy

Looking at the generated code, it seems like intermediate typedefs generated by drift_dev during an internal analysis phase aren't working properly.

You can most likely fix this by raising the SDK constraint in your pubspec.yaml to >=2.13.0, because that's the version of the Dart SDK that enables general typedefs. I'll take a look at how this can efficiently be fixed in drift too.

simolus3 avatar May 03 '23 14:05 simolus3

I moved to a newer test project and its still happening

environment:
  sdk: ">=2.16.2 <3.0.0"

AliEasy avatar May 03 '23 14:05 AliEasy

Did you also remove // @dart=2.12 in your database.dart file? Also try to run flutter clean, in case the result stays the same.

FaFre avatar May 03 '23 14:05 FaFre

Ye I did remove it. It was actually the goal of creating a new project to test without // @dart=2.12 Also tried flutter clean and did not work

AliEasy avatar May 03 '23 14:05 AliEasy

If you look at .dart_tool/build/generated/<your package name>/<path to drift file>.drift.types.temp.dart, does that file contain dynamic Function() anywhere?

I still think it's likely about that hidden source which drift uses to share Dart types across different build steps. But flutter clean removes the .dart_tool folder which should delete the cache, so if you have sdk: ">=2.16.2 <3.0.0" in your pubspec there shouldn't be a problem.

If the problem persists, you could try a development drift version. I tried to fix this problem in b2b37666bd5810dc0a79eaf3fd04512aae4eb5f0.

simolus3 avatar May 03 '23 16:05 simolus3

This is the content of the file you asked for:

import 'package:crm_ideal_app/model/register.dart' as i0;
typedef T0 = i0.RegisterModel;

AliEasy avatar May 04 '23 05:05 AliEasy

Yeah, so the problem might be that this file gets interpreted as an older Dart version which doesn't have generalized typedefs, in which case the analyzer thinks this is a mistyped function type.

Does your package have the correct language version in .dart_tool/package_config.json?

simolus3 avatar May 04 '23 12:05 simolus3

here is the related content:

  {
     "name": "drift",
     "rootUri": "file:///C:/Users/Dev/AppData/Local/Pub/Cache/hosted/pub.dev/drift-2.7.0",
     "packageUri": "lib/",
     "languageVersion": "2.19"
   },
   {
     "name": "drift_dev",
     "rootUri": "file:///C:/Users/Dev/AppData/Local/Pub/Cache/hosted/pub.dev/drift_dev-2.7.0",
     "packageUri": "lib/",
     "languageVersion": "2.17"
   },

AliEasy avatar May 09 '23 13:05 AliEasy

I need the language version for your package, since that is the context in which drift-generated code gets analyzed.

Either way, could you check whether upgrading to drift_dev: ^2.8.0 fixes this problem?

simolus3 avatar May 10 '23 10:05 simolus3