Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webview_benchmarks] Migrate to null safety #879

Merged
merged 21 commits into from Aug 24, 2022

Conversation

amanv8060
Copy link
Contributor

@amanv8060 amanv8060 commented Feb 17, 2022

Migrated package to null safety

List which issues are fixed by this PR. You must list at least one issue.
Fixes: #98647

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@amanv8060 amanv8060 marked this pull request as ready for review February 17, 2022 18:58
@amanv8060
Copy link
Contributor Author

@yjbanov / @stuartmorgan . this is ready to be reviewed.
Thanks.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left specific notes inline; in general this is using far too much force unwrapping rather than making things actually type-safe.

packages/web_benchmarks/lib/client.dart Show resolved Hide resolved
packages/web_benchmarks/lib/client.dart Show resolved Hide resolved
packages/web_benchmarks/lib/client.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/client.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/client.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/browser.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/browser.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/browser.dart Outdated Show resolved Hide resolved
@@ -156,7 +155,7 @@ class BenchmarkServer {
// Trace data is null when the benchmark is not frame-based, such as RawRecorder.
if (latestPerformanceTrace != null) {
final BlinkTraceSummary traceSummary =
BlinkTraceSummary.fromJson(latestPerformanceTrace);
BlinkTraceSummary.fromJson(latestPerformanceTrace!)!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout this function, look for opportunities to use locals so that the checks don't require force unwrapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlinkTraceSummary.fromJson has not many things that can be replaced with locals, it returns null only in one case.

packages/web_benchmarks/pubspec.yaml Outdated Show resolved Hide resolved
@amanv8060
Copy link
Contributor Author

@stuartmorgan should I also update dependencies in pubspec.yaml? Since the versions used were to support non-null safe code with +2 to allow null safe.

@stuartmorgan
Copy link
Contributor

@stuartmorgan should I also update dependencies in pubspec.yaml? Since the versions used were to support non-null safe code with +2 to allow null safe.

Yes, a null-safe package should require null-safe dependencies.

@stuartmorgan
Copy link
Contributor

@yjbanov should do the primary review on this.

@stuartmorgan
Copy link
Contributor

@yjbanov Ping on this review?

Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few suggestions, but otherwise it looks good!

Thanks for migrating the package!

packages/web_benchmarks/lib/client.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/browser.dart Outdated Show resolved Hide resolved
WipConnection debugConnection;
if (withDebugging) {
WipConnection? debugConnection;
if (options.debugPort != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, use a local variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, made it in sync with this comment as well.

packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
@amanv8060 amanv8060 requested a review from mdebbar May 14, 2022 08:23
@amanv8060
Copy link
Contributor Author

@mdebbar I have done the changes.

packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
packages/web_benchmarks/lib/src/recorder.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -1,3 +1,7 @@
## 0.0.8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there really no breaking changes here? That's very unusual for an NNBD transition, and I see public API that takes non-nullable values which looks breaking to me (since previously, e.g., int was equivalent to int? in NNBD code, not int).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please direct me to it. None of the changes look breaking to me.recorder.dart#L66 is one , but it was expected to be non null previously as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please direct me to it.

https://github.com/flutter/packages/pull/879/files#diff-97332f3a9c3e68b393026c3f408eb96ec266dd389c858623f87ad97096207a63R43-R45 for example, is changing three public parameters to non-nullable types.

None of the changes look breaking to me.recorder.dart#L66 is one , but it was expected to be non null previously as well.

I haven't looked at all of the logic, which is why it's a question. Maybe in practice nothing changes, it's just unusual in an NNBD migration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @stuartmorgan. I think this needs to be version 0.1.0. The dart docs recommend indicating the NNBD migration as breaking change:

Update the version of the package to indicate a breaking change:

If your package is already at 1.0.0 or greater, increase the major version. For example, if the previous version is 2.3.2, the new version is 3.0.0.

If your package hasn’t reached 1.0.0 yet, either increase the minor version or update the version to 1.0.0. For example, if the previous version is 0.3.2, the new version is either 0.4.0 or 1.0.0.

https://dart.dev/null-safety/migration-guide#package-version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there are (unsurprisingly) 0 published packages depending on this, which means the ecosystem cost of making this a breaking version change even if it doesn't turn out to be strictly necessary is essentially zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuartmorgan since there are multiple such cases, i have added a generic line regarding breaking change. Also updated the version constraint

@amanv8060 amanv8060 force-pushed the migrate_webview_benchmarks branch 2 times, most recently from fc65553 to 319ef00 Compare May 26, 2022 09:45
if (value is! List) {
throw FormatException(
'"Tracing.dataCollected" returned malformed data. '
'Expected a List but got: ${value.runtimeType}');
}
_tracingData.addAll(event.params['value'].cast<Map<String, dynamic>>());
_tracingData!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's enough separation here and for _tracingCompleter that this is fragile; it would be safer to make new non-nullable locals for both, and then set the fields to the local variable.

Profile profile;
Completer<void> _runCompleter;
late Profile profile;
Completer<void>? _runCompleter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is force-unwrapped a lot; please add a comment explaining which methods it's guaranteed to be non-null in and why.


/// The number of frames ignored as warm-up frames.
int get warmUpFrameCount =>
int? get warmUpFrameCount =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _warmUpFrameCount is expected to be non-null whenever useCustomWarmUp is true, then shouldn't this return be non-nullable, and _warmUpFrameCount be force-unwrapped in the next line?

if (value < 0.0) {
throw StateError(
'Timeseries $name: negative metric values are not supported. Got: $value',
);
}
_allValues.add(value);
if (useCustomWarmUp && isWarmUpValue) {
_warmUpFrameCount += 1;
_warmUpFrameCount = _warmUpFrameCount! + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case the RHS here can use the getter, and not need to force-unwrap.

_RecordingWidgetsBinding();
}
return _RecordingWidgetsBinding.instance;
}

FrameRecorder _recorder;
FrameRecorder? _recorder;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my earlier comment, this needs a comment explaining when and why it's safe to force-unwrap.

@@ -117,16 +116,16 @@ class BenchmarkServer {
Completer<List<Map<String, dynamic>>>();
final List<Map<String, dynamic>> collectedProfiles =
<Map<String, dynamic>>[];
List<String> benchmarks;
Iterator<String> benchmarkIterator;
List<String>? benchmarks;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this nullable? We strongly prefer non-nullable collections to avoid having two ways of expressing empty, which is very error-prone to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is null, we populate it here.

@amanv8060 amanv8060 marked this pull request as draft June 15, 2022 07:13
Copy link
Contributor

@yjbanov yjbanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if this is still passing tests. Let's rebase and see?

io.Platform.environment['PROGRAMFILES'],
io.Platform.environment['PROGRAMFILES(X86)'],
])
if (item != null) item
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this got off my radar, will update this today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...<String?>[
  io.Platform.environment['LOCALAPPDATA'],
  io.Platform.environment['PROGRAMFILES'],
  io.Platform.environment['PROGRAMFILES(X86)'],
].whereType<String>()

could we do this ?

@amanv8060 amanv8060 marked this pull request as ready for review August 14, 2022 04:20
@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 24, 2022
@auto-submit auto-submit bot merged commit 2e17cff into flutter:main Aug 24, 2022
@amanv8060 amanv8060 deleted the migrate_webview_benchmarks branch August 24, 2022 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: web_benchmarks
Projects
None yet
5 participants