Open Sadhorsephile opened 4 weeks ago
Summary: The macro fails to correctly import dart:core
when using DeclarationCode.fromString('import \'dart:core\';')
. This results in a runtime error because the generated code references String
from both dart:core
and the imported dart:core
as a prefix, leading to a type conflict.
This does sound like a bug, relative to the current specification of part files with imports. It may be correct relative to the prior specification of augmentation libraries that this specification replaces.
The implicit import of dart:core
of a library file only happens if you do not explicitly import dart:core
in the library file, with or without a prefix.
There is no implicit import of dart:core
in part files, they inherit the imports of the parent file, and an explicit import of dart:core
in a part file should not affect the import of dart:core
in the library file. Imports in part files can always shadow imports inherited from the parent file, so there is no need to prevent the inheritance.
What looks to be happening here is that the import of dart:core
with a prefix makes the implicit import of dart:core
go away. (Which makes sense for the prior augmentation-library specification, where each augmentation library was treated as having its own implicit import of dart:core
.)
It shouldn't work like that, but "parts with imports" is likely not implemented yet.
It's not that there are two string types, rather there is no String
name imported into the top-level scope at all. That's why the error is "String is not a type".
Problem
Cannot launch the project with the following macro:
Macro
```dart import 'dart:async'; import 'package:macros/macros.dart'; macro class ExampleMacro implements MethodDeclarationsMacro, MethodDefinitionMacro { const ExampleMacro(); @override FutureOrExample
```dart import 'package:test_macros/reproduce/macro.dart'; class SomeClass { @ExampleMacro() external void testMethod(String id); } void main() { SomeClass().testMethod(''); } ```I have no analyzer errors, but I get errors when I run it:
As I see it, the problem is the use of two
String
types -prefix0.String
from'dart:core' as prefix0
andString
fromdart:core
.Additional info
dart info
output: