dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

Analyzer crashing when trying to lint #40299

Closed harryterkelsen closed 4 years ago

harryterkelsen commented 4 years ago

Using analyzer 0.39.4 and linter 0.1.109 I am getting this stacktrace when trying to run pub global run linter .:

~/Projects/dart/sdk/pkg/analyzer remotes/origin/HEAD
λ pub global run linter .
Unhandled exception:
type '_CompactIterable<Element>' is not a subtype of type 'Iterable<LibraryElement>'
#0      _ApiModel._calculate (package:analyzer/src/lint/project.dart:147:46)
<asynchronous suspension>
#1      new _ApiModel (package:analyzer/src/lint/project.dart:112:5)
#2      new DartProject._ (package:analyzer/src/lint/project.dart:54:17)
#3      DartProject.create (package:analyzer/src/lint/project.dart:93:39)
<asynchronous suspension>
#4      LintDriver.analyze (package:analyzer/src/lint/analysis.dart:203:45)
#5      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:47:6)
#6      LintDriver.analyze (package:analyzer/src/lint/analysis.dart:167:42)
#7      DartLinter.lintFiles (package:analyzer/src/lint/linter.dart:91:36)
#8      _RootZone.runUnary (dart:async/zone.dart:1381:54)
#9      _FutureListener.handleValue (dart:async/future_impl.dart:140:18)
#10     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#11     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#12     Future._addListener.<anonymous closure> (dart:async/future_impl.dart:391:9)
#13     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#14     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#15     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:175:5)

It seems like these lines are the culprit: https://github.com/dart-lang/sdk/blob/master/pkg/analyzer/lib/src/lint/project.dart#L147

It might be fixed by casting them, i.e.:

        elements.addAll(exports.definedNames.values.cast<LibraryElement>());
        elements.addAll(public.definedNames.values.cast<LibraryElement>());
bwilkerson commented 4 years ago

... when trying to run pub global run linter .

The exception should be fixed by https://dart-review.googlesource.com/c/sdk/+/133154, but I'll point out that the linter is not intended to be used as a stand-alone tool. It only has a main for testing purposes. We don't guarantee that we will continue to support using it this way, nor that the results you get will always match what you'd see when running it via dartanalyzer, as intended.