dart-lang / dart-lang/source_gen

throwing an InvalidGenerationSource on an annotation "element"

Open
#470 0 comments 0 reactions 0 assignees View on GitHub
type-enhancement
Dominant language
Dart
Stars
494
Forks
115
Avg merge
12h 13m
Merged PRs (30d)
3

Description

I have seen various different issues(#131, https://github.com/dart-lang/sdk/issues/34062, https://github.com/dart-lang/sdk/issues/32454) about this but never found a proper solution that wouldn't need me to create a utility function on my side. Of course, I could have missed something, if I did, please tell me what to do :)

We're currently working on a code generation library (https://pub.dev/packages/floor) which relies heavily on annotations, some of them expecting a string with e.g. an SQLite query. We're now adding a validator for these queries, which will throw errors if the query inside the annotation has some faults. Until now, we were throwing `InvalidSourceGenerationError`s, which reference the method on which the annotation happened:

```dart
InvalidGenerationSourceError fromAnalysisError(AnalysisError error) {
return InvalidGenerationSourceError(
'The query contained errors: ${error.message}',
element: _methodElement,
);
}
```

Now, the output looks something like:
```
The query contained errors: Unknown column. These columns are available: id in Person, name in Person
package:_resolve_source/_resolve_source.dart:8:22

8 │ Future findPersonByIdAndName(int id);
│ ^^^^^^^^^^^^^^^^^^^^^

```
which does not show the annotation at all. But as the annotation is not `Element`, but `ElementAnnotation` I can't pass it as the source `element`. It would also be nice to be able to reference specific parts of the annotation.

Another issue is that the functions for getting the annotation content (`firstAnnotationOfExact`, `annotationsOf`, etc) only return `DartObject`s, which are impossible to use for proper errors referencing the context. I think that a more general API which returns matching `ElementAnnotation`s could be more useful, especially if the associated `DartObject` is just a getter/`computeConstantValue()` away.

Contributor guide

Open the contributing guide

Research direction

Start with fromAnalysisError and the InvalidGenerationSourceError element parameter, then inspect how firstAnnotationOfExact and annotationsOf expose DartObject values. Determine how annotation locations or matching ElementAnnotations should be represented so query errors point to the annotation, including a clear way to verify the resulting diagnostic output.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.