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.27k stars 1.58k forks source link

Poor error message when accessing element that is imported twice #23840

Open sigurdm opened 9 years ago

sigurdm commented 9 years ago

main.dart:

import "lib.dart";
import "lib2.dart";

void main() {
  foo();
}

lib1.dart:

foo() {}

lib2.dart

foo() {}

The vm prints

Unhandled exception:
No top-level method 'foo' declared.

NoSuchMethodError: method not found: 'foo'
Receiver: top-level
Arguments: [...]
#0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:176)
#1      main (file:///usr/local/google/home/sigurdm/test/main.dart:5:3)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)

It would be more helpful with a message mentioning the duplicate import.

srawlins commented 6 years ago

dartanalyzer includes a duplicate import error. But the VM still confusingly says "Method not found: 'foo'."

$ dart --version
Dart VM version: 2.0.0-dev.63.0 (Fri Jun 15 00:42:43 2018 +0200) on "macos_x64"
$ cat 23840.dart 
import "23840-lib.dart";
import "23840-lib2.dart";

void main() {
  foo();
}
$ cat 23840-lib.dart 
foo() {}
$ cat 23840-lib2.dart 
foo() {}
$ dartanalyzer 23840.dart 
Analyzing 23840.dart...
  error • The name 'foo' is defined in the libraries '23840-lib.dart' and '23840-lib2.dart' at 23840.dart:5:3 • ambiguous_import
1 error found.
$ dart --preview-dart-2 23840.dart 
23840.dart:5:3: Error: Method not found: 'foo'.
  foo();
  ^^^

😖