drift icon indicating copy to clipboard operation
drift copied to clipboard

Migrate use of analyzer's "element model" to Element2.

Open srawlins opened this issue 8 months ago • 3 comments

The analyzer package's "element model," (everything declared in package:analyzer/dart/element/element.dart) is migrating to a new API, found at package:analyzer/dart/element/element2.dart. The "element 1" APIs are presently being deprecated.

Consumers of the old element model need to migrate to the new element model. A few notes:

  • In order to migrate, drift_dev needs to use at least analyzer version 7.3.0.
  • In order to migrate, drift_dev also needs to use a new version of source_gen, not yet released, which also makes its APIs available with Element2 types. Follow https://github.com/dart-lang/source_gen/issues/743.
  • There is a lint rule available to help migrate, analyzer_use_new_elements. Turning it on in drift_dev reveals > 700 references to classes in the "element 1" model.

srawlins avatar Mar 06 '25 16:03 srawlins

This looks like a fun one :D I'll try to prepare a branch using the new element model and a dependency override for source_gen (which should be good enough to roll into your clone).

simolus3 avatar Mar 06 '25 16:03 simolus3

@srawlins I gave this a go on the element2 branch, but I think it's too early for client packages to make the migration. I got the >700 usages down to around 50, but the remaining ones are tricky. In addition to source_gen, I also need versions of build that support the element 2 API to complete the migration.

I also need an element2-variant of TypeSystem.instantiateInterfaceToBounds that the analyzer doesn't seem to provide (or I couldn't find it).

Is drift_dev blocking anything internally for you here? Let me know if there's more I can do.

simolus3 avatar Mar 06 '25 21:03 simolus3

Sweeet, thanks @simolus3!! If you're interested, you could try the source_gen and build branches that support Element2:

  • source_gen: https://github.com/dart-lang/source_gen/tree/analyzer-element2
  • build: ... actually I can't find that one.
  • TypeSystem.instantiateInterfaceToBounds2 is not yet released. Should be released in analyzer 7.4.0.

CC @scheglov

srawlins avatar Mar 07 '25 22:03 srawlins

The element2 release of build is now published as 3.0.0-dev, I figured I'd use a prereleases so builder authors have a chance to try it properly and give feedback before the real release.

So far we have json_serializable, mockito and build_value ready to go with the element2 build.

For drift, we have the element2 branch, and the main branch ... not sure what should happen next, do you think it could be possible to merge the element2 changes back into main, or maybe by this point it's easier to redo them on top of main?

Either way I'm happy to take a look / help. Thanks!

davidmorgan avatar Jul 11 '25 10:07 davidmorgan

Thanks for the heads-up! At the very least I want to rebase the element2 branch ontop of develop. I'll also try to get an element2 release to pub.dev soon. I don't think I need help, but if I run into any issues I'll let you know - thanks!

simolus3 avatar Jul 11 '25 11:07 simolus3

I've published a pre-release update of drift_dev with support for element2 APIs and the package:build pre-releases.

@srawlins I'm not sure who I should ping for this, but I assume you're still pulling from element2 for the internal clone? Now that that has been merged, you can move back to the latest-release branch. I'll probably delete that branch soon since it has been released.

simolus3 avatar Jul 13 '25 21:07 simolus3

Great, thanks!

I'll be handling the fallout in google3 from all this, so I'll take a look at syncing drift_dev as part of that.

davidmorgan avatar Jul 15 '25 07:07 davidmorgan