simolus3 / drift

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

Missing library exceptions during generation with drift_dev 2.11.2 #2688

Closed kuhnroyal closed 12 months ago

kuhnroyal commented 1 year ago

I am trying to migrate from drift 2.11.1 + analyzer:5.12.0 to drift 2.12.1 + analyzer:6.1.0/6.2.0. With drift 2.11.1 everything works perfectly but after upgrades I get errors when trying to generate.

[SEVERE] drift_dev:not_shared on lib/db/file_upload_table.expr.temp.dart:

Invalid argument(s): Missing library: package:foo_app/db/converter/local_date_converter.dart
Libraries: [dart:async, dart:collection, dart:convert, dart:core, dart:developer, dart:ffi, dart:html, ... A very long list

A stripped down version of the file upload table file:

import 'package:foo_app/db/foo/foo.drift';
import 'package:foo_app/db/image/image.drift';
import 'package:foo_app/db/document/document.drift';
import 'package:foo_app/db/uploads/file_upload_state.dart';
import 'package:foo_app/db/uploads/file_upload_type.dart';
import 'package:foo_app/db/converter/built_value_enum_converter.dart';

CREATE TABLE file_uploads(
    id              TEXT NOT NULL,
    task_id         TEXT,
    foo_id       INTEGER NOT NULL,
    image_id        TEXT,
    document_id     TEXT,
    state           INTEGER NOT NULL MAPPED BY `const BuiltValueEnumConverter<FileUploadStateEnum>()`,
    type            INTEGER NOT NULL MAPPED BY `const BuiltValueEnumConverter<FileUploadTypeEnum>()`,
    PRIMARY KEY (id),
    FOREIGN KEY(foo_id) REFERENCES foo(id) ON DELETE CASCADE,
    FOREIGN KEY(image_id) REFERENCES images(id) ON DELETE CASCADE,
    FOREIGN KEY(document_id) REFERENCES documents(id) ON DELETE CASCADE
) STRICT AS FileUploadEntity;

The content of the file_upload_table.expr.temp.dart:

import 'package:foo_app/db/converter/built_value_enum_converter.dart';
import 'package:foo_app/db/estate/uploads/file_upload_type.dart';
import 'package:foo_app/db/estate/uploads/file_upload_state.dart';
import 'package:foo_app/db/catalog/catalog_entity.dart';
import 'package:foo_app/db/converter/local_date_converter.dart';
var expr_0 = const BuiltValueEnumConverter<FileUploadStateEnum>();
var expr_1 = const BuiltValueEnumConverter<FileUploadTypeEnum>();

Here is what I think is happening, but I might be way off:

The file uploads table does NOT use the local_date_converter.dart and does not import it, same goes for the catalog_entity.dart file. Still the local_date_converter.dart and catalog_entity.dart end up as imports in the file_upload_table.expr.temp.dart file and then for whatever reason can not be found. Either the imports are added incorrectly in 2.12 or something might have changed in the analyzer that now "optimizes" the libraries away.

In any case the FK linked foo table has these imports and that is likely why they are transitively (mistakenly?) get added.

simolus3 commented 1 year ago

Does running the build with --verbose print a stack trace?

The file uploads table does NOT use the local_date_converter.dart and does not import it, same goes for the catalog_entity.dart file. In any case the FK linked foo table has these imports and that is likely why they are transitively (mistakenly?) get added.

We also add imports for Dart files transitively imported through other .drift files. This is since drift files don't have a real module system - tables defined in a transitively imported file are reachable and for consistency, we try to emulate this for Dart imports in drift files as well. I think this hasn't changed recently.

Did the drift file actually importing the Dart file also see this error?

kuhnroyal commented 1 year ago

I got 2 stacks with analyzer 6.2.0:

#0      LinkedElementFactory.removeLibraries (package:analyzer/src/summary2/linked_element_factory.dart:243:67)
#1      LibraryContext.remove (package:analyzer/src/dart/analysis/library_context.dart:270:20)
#2      AnalysisDriver._removePotentiallyAffectedLibraries (package:analyzer/src/dart/analysis/driver.dart:1841:22)
#3      AnalysisDriver._applyPendingFileChanges (package:analyzer/src/dart/analysis/driver.dart:1266:7)
#4      AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2217:18)
<asynchronous suspension>
package:analyzer/src/summary2/linked_element_factory.dart 130:7   LinkedElementFactory.createLibraryElementForReading
package:analyzer/src/summary2/linked_element_factory.dart 194:14  LinkedElementFactory.elementOfReference
package:analyzer/src/summary2/linked_element_factory.dart 222:12  LinkedElementFactory.libraryOfUri
package:analyzer/src/summary2/linked_element_factory.dart 226:19  LinkedElementFactory.libraryOfUri2
package:analyzer/src/summary2/bundle_reader.dart 1783:28          ResolutionReader.libraryOfUri
package:analyzer/src/summary2/bundle_reader.dart 483:30           LibraryElementLinkedData._readLibraryOrAugmentation
package:analyzer/src/summary2/bundle_reader.dart 457:5            LibraryElementLinkedData._read
package:analyzer/src/summary2/bundle_reader.dart 220:5            ElementLinkedData.read
package:analyzer/src/summary2/bundle_reader.dart 446:13           LibraryElementLinkedData.read
package:analyzer/src/dart/element/element.dart 4476:17            LibraryElementImpl._readLinkedData
package:analyzer/src/dart/element/element.dart 4223:5             LibraryElementImpl.libraryExports
package:analyzer/src/dart/element/element.dart 4661:12            LibraryOrAugmentationElementImpl.children
package:analyzer/src/dart/element/element.dart 4084:18            LibraryElementImpl.children
package:analyzer/src/dart/element/element.dart 2433:27            ElementImpl.visitChildren
package:analyzer/dart/element/visitor.dart 309:13                 RecursiveElementVisitor.visitLibraryElement
package:drift_dev/src/analysis/resolver/discover.dart 185:5       _FindDartElements.find
package:drift_dev/src/analysis/resolver/discover.dart 78:22       DiscoverStep.discover
package:drift_dev/src/analysis/driver/driver.dart 129:7           DriftAnalysisDriver.discoverIfNecessary
package:drift_dev/src/analysis/driver/driver.dart 166:7           DriftAnalysisDriver.findLocalElements
package:drift_dev/src/backends/build/drift_builder.dart 226:21    _DriftBuildRun._checkForElementsToBuild
package:drift_dev/src/backends/build/drift_builder.dart 149:10    _DriftBuildRun.run
package:drift_dev/src/backends/build/drift_builder.dart 106:5     DriftBuilder.build
kuhnroyal commented 1 year ago

Did the drift file actually importing the Dart file also see this error?

Hard to say, there is always only one or 2 errors in the output but it is not deterministic for which tables.

It seems the analyzer is calling removeLibraries with the converters as arguments (among a long list of other libraries).

[removeLibraries][uriSet: {package:foo_app/db/converter/built_value_enum_converter.dart, package:foo_app/db/converter/local_date_converter.dart, ... 

This time it also failed for the built_value_enum_converter.dart which is directly used...

@simolus3 I sent you a mail with the full log.

kuhnroyal commented 1 year ago

If I run the generation often enough without cleaning, at some point it seems to succeed...

kuhnroyal commented 1 year ago

Ok, so I just saw that there was a change in drift_dev: 2.11.2 to follow exports. This seems to be the culprit. I reverted to my 2.11.1 state and just updated to drift_dev:2.11.2 and the errors start happening.

simolus3 commented 1 year ago

Did this happen with analyzer: 6.3.0 as well or was there a version conflict preventing that? Just wondering because some code in summary2/linked_element_factory.dart seems to have changed recently judging on the line numbers in the stack trace. Drift might have uncovered this, but it still looks like a problem with the analyzer or build_resolvers package.

kuhnroyal commented 1 year ago

Did this happen with analyzer: 6.3.0 as well or was there a version conflict preventing that? Just wondering because some code in summary2/linked_element_factory.dart seems to have changed recently judging on the line numbers in the stack trace. Drift might have uncovered this, but it still looks like a problem with the analyzer or build_resolvers package.

This is probably not related to analyzer 6 - see my previous comment. The error starts to happen with drft_dev: 2.11.2 independent of the analyzer.