Sdk: Support incremental migration to null safety

Created on 9 Nov 2020  路  5Comments  路  Source: dart-lang/sdk

Having just migrated my app (~35K LOC) to null safety, I think support for incremental migration in the tool is essential for the tool to be useful in real-world app scenarios. I found the following to be true:

  1. I wanted to check things off my list -- once I migrated a file, I wanted to know that it was migrated and didn't need further attention.
  2. The migration of each file required careful scrutiny and the addition of several hints in order for the tool to produce the correct results. In some cases, the tool would produce code that didn't pass analysis (e.g. https://github.com/dart-lang/sdk/issues/44069, https://github.com/dart-lang/sdk/issues/44012), but in most cases, it was simply that I wanted something to be non-null (sometimes requiring the late keyword), and the tool would make it nullable. In those cases, the tool was doing its job correctly, but the migration of the file required careful scrutiny on my part nonetheless.
  3. Said scrutiny was slow going and draining.

The combination of these observations meant that I couldn't just add hints, then migrate the whole project in one go. Attempting to do so would violate #1 in that I wouldn't have an easy way to see which files I had already completed the "add hints" pass to, and because of the analysis errors produced, it would mean that the project would be littered with analysis errors once I migrated everything.

Thus, I did the following process manually:

  1. First, I ran a sed script over my entire project, adding // @dart=2.9 comments to the header of each file.
  2. Then, I ran the migration tool on the whole project so it would make the necessary updates to my pubspec.yaml file. Once the migration ran, I reverted every file in my project _other_ than the pubspec.yaml file. This left me in a state where null safety was turned on, but every Dart file was opted out.
  3. Then, I discovered that if you run the migration tool from a directory, it only includes files in that directory or its subdirectories... so I started in the leaf directories in my project, and ran the tool on each one. Each time I ran it, I would migrate a file or two, revert the changes to the _other_ files, commit then, and repeat.

My manual incremental migration worked, but it strikes me that many (I daresay most) apps or packages will want to migrate incrementally as well, so our tools should support this use case better.

NNBD P1 area-migration

Most helpful comment

I mean within the app's source code itself.

All 5 comments

/cc @stereotype441 @mit-mit

Do you mean incremental within our own app's source code itself, or incremental in the sense that you wanted to migrate prior to your deps being available in null safety versions?

I mean within the app's source code itself.

Soon (hopefully next week) I'm intending to work on adding a feature to the migration tool that essentially automates @tvolkert's manual process. I'll use this issue to track my progress.

The workflow I currently have in mind is to have a set of checkboxes next to the file view in the migration tool; the user can check or uncheck these boxes to tell the tool whether they wish to migrate the given file during this run. For files/directories that are already migrated, instead of a checkbox we can have some clear indication that the file is already migrated (a green checkmark, perhaps). The first time the migration tool is run, if any files are not being migrated this time, // @dart=2.9 will be automatically added to those files. So the workflow would be:

  1. Run the migration tool for the first time, and use the checkboxes to select which files/directories to migrate first (you can still see how the tool would have migrated the other files in the preview). When you click "apply migration", it adjusts your pubspec, migrates the files you want to migrate, and adds // @dart=2.9 to the files you don't want to migrate.
  2. Later, run the migration tool again and use the checkboxes to select which files/directories to migrate next (it will be obvious which files have already been migrated). When you click "apply migration", it migrates the files you want to migrate (removing their // @dart=2.9 annotatoins) and leaves the others alone.
  3. Repeat step 2 until fully migrated.

No need for sed scripts or reverts.

@tvolkert (and anyone else who is interested), feel free to comment if you have other workflow ideas.

That sounds great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmesserly picture jmesserly  路  3Comments

DartBot picture DartBot  路  3Comments

nex3 picture nex3  路  3Comments

55555Mohit55555 picture 55555Mohit55555  路  3Comments

matanlurey picture matanlurey  路  3Comments