Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testBuilder method does not work with GenerateForAnnotated generators #2292

Closed
elnurvl opened this issue May 30, 2019 · 15 comments
Closed

testBuilder method does not work with GenerateForAnnotated generators #2292

elnurvl opened this issue May 30, 2019 · 15 comments

Comments

@elnurvl
Copy link

elnurvl commented May 30, 2019

I have a custom generator that extends from GenerateForAnnotated class. I want to debug my generator with breakpoints. In order to do that, I am using testBuilder method of build_test package.

However, the debugger does not hit generateForAnnotatedElement method and testBuilder’s result is an empty asset.

When I change my generator to inherit from Generator class, the package works and the debugger hits the generate method.

Why does it happen? Is it a bug? How can I debug the generator?

Test file:

void main() {
  group('generator tests', () {
    test('generator test', () async {
      var builder = PartBuilder([TestGeneratorAnnot()], '.g.dart');
      var assets = {
        'annotations|lib/annotations.dart': annotations,
        'source_gen_test|lib/test.dart': input
      };
      await testBuilder(builder, assets,
       rootPackage: 'source_gen_test');
    });
  });
}

var input = r'''
library source_gen_test;
import 'package:annotations/annotations.dart';

part 'test.g.dart';

@TestAnnot()
class Test {
  
}
''';

var annotations= r'''
library annotations;

class TestAnnot{
  const TestAnnot();
}
''';

TestAnnot file:

class TestAnnot{
  const TestAnnot();
}

Generator file:

class TestGeneratorAnnot extends GeneratorForAnnotation<TestAnnot> {
  const TestGeneratorAnnot();

  @override
  generateForAnnotatedElement(
      Element element, ConstantReader annotation, BuildStep buildStep) {
    var code = '''
    It worked!
    ''';
    return code;
  }
}

pubspec.yaml:

environment:
  sdk: '>=2.2.0 <3.0.0'

dependencies:
  annotations:
    path: ../annotations
  analyzer: ^0.34.0
  build: ^1.0.0
  logging: ^0.11.0
  source_gen: ^0.9.0
  meta: ^1.1.5

dev_dependencies:
  test: ^1.3.0
  build_runner: ^1.1.0
  build_test: ^0.10.3
  collection: ^1.14.10
  path: ^1.6.2
@jakemac53
Copy link
Contributor

I was not able to repro the error here - you will need to ensure that you are correctly emulating the annotations library - is that the exact same uri as the actual annotation as it is imported in the builder file?

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

No. The annotation library is fake. I had maden this up instead of pasting the real annotations for the sake of simplicity. The point is that, the code does not hit generateForAnnotatedElement method.

@jakemac53
Copy link
Contributor

Right, but the likely reason it isn't is because it can't resolve the annotation. You have to provide in assets the library that defines the annotations. It can be a stub and not the real library, but it needs to have the annotations and the same uri as the real annotations library.

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

Well, I created a real annotations library and imported it to the generator project. Same result.

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

I have made some corrections on the first comment. Still does not work. There is one more issue(#1828) representing this problem which was closed without being resolved. I would like to know if it is a bug or our mistake.

@jakemac53
Copy link
Contributor

In both of these cases I can't actually reproduce the problem, (I recreated your example above and it works for me). So I assume there is some mistake somewhere.

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

Did the breakpoint in the generator work for you??? I shared pubspec.yaml in order to make sure there is not a difference between package versions. How else can the same code give different results in Dart environment?

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

Below you can watch the problem if you do not believe me. I reproduced the problem and recorded every step. I want to know where is my mistake in this setup?

https://1drv.ms/u/s!ArT19eL-omOjhY0wxeUJsLVZeTwf4Q

@natebosch
Copy link
Member

@elnurvl - is there a repository we can check out and reproduce what you are seeing?

@jakemac53
Copy link
Contributor

Did the breakpoint in the generator work for you???

Yes, it did.

Below you can watch the problem if you do not believe me. I reproduced the problem and recorded every step. I want to know where is my mistake in this setup?

I think it might have to do with the export - in that case the class is actually defined in the exported file under src but in your test the annotation facade is directly under lib/annotations.dart. Libraries are identified by their URI in dart so I think you would need to replicate that exact exports setup in the test also.

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

I think it might have to do with the export - in that case the class is actually defined in the exported file under src but in your test the annotation facade is directly under lib/annotations.dart. Libraries are identified by their URI in dart so I think you would need to replicate that exact exports setup in the test also.

@jakemac53 , can you please share your working sample, so I can examine the problem?
I do not think it is with the export. Because there is no problem with the test code when I change the generator type to Generator. But if you can share your working code, I can compare it for the exact differences.

@elnurvl
Copy link
Author

elnurvl commented May 31, 2019

@natebosch I will push the code to a repository soon and share it here.

@elnurvl
Copy link
Author

elnurvl commented Jun 1, 2019

@natebosch , you can look at the repository from this link:
https://github.com/elnurvl/source-gen-test-project

If you think there is something wrong in this repository which causes the problem mentioned here, please feel free to send a pull request with the corrected code, or open an issue stating the source of the problem. If it cannot be thought as a mistake, I suggest, either to improve the code in order to achieve easier test setup or to improve the documentation in order to prevent any confusion while working with the package, as it is quite hard to detect a problem especially when almost identical codes give different results.

Thanks.

@jakemac53
Copy link
Contributor

Sent a pr to implement the fix that I outlined above

@jakemac53
Copy link
Contributor

#2297 will also make this a bit easier, so you won't have to provide these types of mock assets (and won't get broken if the real asset moves)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants