@@ -47,6 +47,7 @@ class CommandLineOptions {
4747 static const applyChangesFlag = 'apply-changes' ;
4848 static const helpFlag = 'help' ;
4949 static const ignoreErrorsFlag = 'ignore-errors' ;
50+ static const ignoreExceptionsFlag = 'ignore-exceptions' ;
5051 static const previewPortOption = 'preview-port' ;
5152 static const sdkPathOption = 'sdk-path' ;
5253 static const skipPubOutdatedFlag = 'skip-pub-outdated' ;
@@ -60,6 +61,8 @@ class CommandLineOptions {
6061
6162 final bool ignoreErrors;
6263
64+ final bool ignoreExceptions;
65+
6366 final int previewPort;
6467
6568 final String sdkPath;
@@ -74,6 +77,7 @@ class CommandLineOptions {
7477 {@required this .applyChanges,
7578 @required this .directory,
7679 @required this .ignoreErrors,
80+ @required this .ignoreExceptions,
7781 @required this .previewPort,
7882 @required this .sdkPath,
7983 @required this .skipPubOutdated,
@@ -238,6 +242,10 @@ class MigrationCli {
238242
239243 AnalysisContextCollection _contextCollection;
240244
245+ bool _hasExceptions = false ;
246+
247+ bool _hasAnalysisErrors = false ;
248+
241249 MigrationCli (
242250 {@required this .binaryName,
243251 @visibleForTesting this .loggerFactory = _defaultLoggerFactory,
@@ -275,6 +283,10 @@ class MigrationCli {
275283 return contextCollection.contexts.length > 1 ;
276284 }
277285
286+ @visibleForTesting
287+ bool get isPreviewServerRunning =>
288+ _fixCodeProcessor? .isPreviewServerRunnning ?? false ;
289+
278290 Context get pathContext => resourceProvider.pathContext;
279291
280292 /// Blocks until an interrupt signal (control-C) is received. Tests may
@@ -289,12 +301,10 @@ class MigrationCli {
289301 ResourceProvider resourceProvider, LineInfo getLineInfo (String path),
290302 {List <String > included = const < String > [],
291303 int preferredPort,
292- bool enablePreview = true ,
293304 String summaryPath}) {
294305 return NonNullableFix (listener, resourceProvider, getLineInfo,
295306 included: included,
296307 preferredPort: preferredPort,
297- enablePreview: enablePreview,
298308 summaryPath: summaryPath);
299309 }
300310
@@ -349,6 +359,8 @@ class MigrationCli {
349359 applyChanges: applyChanges,
350360 directory: migratePath,
351361 ignoreErrors: argResults[CommandLineOptions .ignoreErrorsFlag] as bool ,
362+ ignoreExceptions:
363+ argResults[CommandLineOptions .ignoreExceptionsFlag] as bool ,
352364 previewPort: previewPort,
353365 sdkPath: argResults[CommandLineOptions .sdkPathOption] as String ??
354366 defaultSdkPathOverride ??
@@ -408,13 +420,12 @@ class MigrationCli {
408420 await _withProgress (
409421 '${ansi .emphasized ('Generating migration suggestions' )}' , () async {
410422 _fixCodeProcessor = _FixCodeProcessor (context, this );
411- _dartFixListener =
412- DartFixListener ( DriverProviderImpl (resourceProvider, context));
423+ _dartFixListener = DartFixListener (
424+ DriverProviderImpl (resourceProvider, context), _exceptionReported );
413425 nonNullableFix = createNonNullableFix (
414426 _dartFixListener, resourceProvider, _fixCodeProcessor.getLineInfo,
415427 included: [options.directory],
416428 preferredPort: options.previewPort,
417- enablePreview: options.webPreview,
418429 summaryPath: options.summary);
419430 nonNullableFix.rerunFunction = _rerunFunction;
420431 _fixCodeProcessor.registerCodeTask (nonNullableFix);
@@ -547,6 +558,7 @@ Use this interactive web view to review, improve, or apply the results.
547558 logger.stdout (
548559 'Note: analysis errors will result in erroneous migration suggestions.' );
549560
561+ _hasAnalysisErrors = true ;
550562 if (options.ignoreErrors) {
551563 logger.stdout ('Continuing with migration suggestions due to the use of '
552564 '--${CommandLineOptions .ignoreErrorsFlag }.' );
@@ -620,6 +632,43 @@ Use this interactive web view to review, improve, or apply the results.
620632 }
621633 }
622634
635+ void _exceptionReported (String detail) {
636+ if (_hasExceptions) return ;
637+ _hasExceptions = true ;
638+ if (options.ignoreExceptions) {
639+ logger.stdout ('''
640+ Exception(s) occurred during migration. Attempting to perform
641+ migration anyway due to the use of --${CommandLineOptions .ignoreExceptionsFlag }.
642+
643+ To see exception details, re-run without --${CommandLineOptions .ignoreExceptionsFlag }.
644+ ''' );
645+ } else {
646+ exitCode = 1 ;
647+ if (_hasAnalysisErrors) {
648+ logger.stderr ('''
649+ Aborting migration due to an exception. This may be due to a bug in
650+ the migration tool, or it may be due to errors in the source code
651+ being migrated. If possible, try to fix errors in the source code and
652+ re-try migrating. If that doesn't work, consider filing a bug report
653+ at:
654+ ''' );
655+ } else {
656+ logger.stderr ('''
657+ Aborting migration due to an exception. This most likely is due to a
658+ bug in the migration tool. Please consider filing a bug report at:
659+ ''' );
660+ }
661+ logger.stderr ('https://github.com/dart-lang/sdk/issues/new' );
662+ logger.stderr ('''
663+ To attempt to perform migration anyway, you may re-run with
664+ --${CommandLineOptions .ignoreExceptionsFlag }.
665+
666+ Exception details:
667+ ''' );
668+ logger.stderr (detail);
669+ }
670+ }
671+
623672 bool _isUriError (AnalysisError error) =>
624673 error.errorCode == CompileTimeErrorCode .URI_DOES_NOT_EXIST ;
625674
@@ -697,6 +746,12 @@ Use this interactive web view to review, improve, or apply the results.
697746 help: 'Attempt to perform null safety analysis even if there are '
698747 'analysis errors in the project.' ,
699748 );
749+ parser.addFlag (CommandLineOptions .ignoreExceptionsFlag,
750+ defaultsTo: false ,
751+ negatable: false ,
752+ help:
753+ 'Attempt to perform null safety analysis even if exceptions occur.' ,
754+ hide: hide);
700755 parser.addOption (CommandLineOptions .previewPortOption,
701756 help:
702757 'Run the preview server on the specified port. If not specified, '
@@ -780,6 +835,9 @@ class _FixCodeProcessor extends Object {
780835 _FixCodeProcessor (this .context, this ._migrationCli)
781836 : pathsToProcess = _computePathsToProcess (context);
782837
838+ bool get isPreviewServerRunnning =>
839+ nonNullableFixTask? .isPreviewServerRunning ?? false ;
840+
783841 LineInfo getLineInfo (String path) =>
784842 context.currentSession.getFile (path).lineInfo;
785843
@@ -863,7 +921,10 @@ class _FixCodeProcessor extends Object {
863921 await _task.processUnit (phase, result);
864922 });
865923 }
866- await _task.finish ();
924+ var state = await _task.finish ();
925+ if (_migrationCli.exitCode == null && _migrationCli.options.webPreview) {
926+ await _task.startPreviewServer (state);
927+ }
867928 _progressBar.complete ();
868929
869930 return nonNullableFixTask.previewUrls;
0 commit comments