sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Macro with explicit "dart:core" import doesn't work

Open Sadhorsephile opened this issue 6 months ago • 2 comments

Problem

Cannot launch the project with the following macro:

Macro

import 'dart:async';
import 'package:macros/macros.dart';

macro class ExampleMacro implements MethodDeclarationsMacro, MethodDefinitionMacro {
  const ExampleMacro();
  @override
  FutureOr<void> buildDeclarationsForMethod(MethodDeclaration method, MemberDeclarationBuilder builder) async {
    builder.declareInLibrary(DeclarationCode.fromString('import \'dart:core\';'));
  }

  @override
  FutureOr<void> buildDefinitionForMethod(MethodDeclaration method, FunctionDefinitionBuilder builder) async {
    builder.augment(FunctionBodyCode.fromString('{ final String foo = "foo";}'));
  }
}
Example
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:

org-dartlang-augmentation:/_/test_macros/lib/reproduce/example.dart-1:6:56: Error: 'String' isn't a type.
  augment void testMethod(prefix0.String id, ) { final String foo = "foo";}

As I see it, the problem is the use of twoString types - prefix0.String from 'dart:core' as prefix0 and String from dart:core.

Additional info

dart info output:

#### General info

- Dart 3.5.0-180.3.beta (beta) (Wed Jun 5 15:06:15 2024 +0000) on "macos_arm64"
- on macos / Version 14.4.1 (Build 23E224)
- locale is en-RU

#### Project info

- sdk constraint: '^3.5.0-180'
- dependencies: collection, dio, flutter, json, macros, provider
- dev_dependencies: flutter_lints, flutter_test

Sadhorsephile avatar Aug 15 '24 14:08 Sadhorsephile