dart-lang / source_gen

Automatic source code generation for Dart
https://pub.dev/packages/source_gen
BSD 3-Clause "New" or "Revised" License
482 stars 104 forks source link

Having a property called `override` causes `json_serializable` to fail to obtain the annotation #688

Closed SamJakob closed 9 months ago

SamJakob commented 9 months ago

I know the title mentions json_serializable, but the errors printed (below) mention to file an issue here and I haven't had the time to test whether it's this package or another down the line causing the issue. I also can't inspect the code generated by json_serializable as it fails to produce any output but I can copy this issue to json_serializable if it turns out the problem lies there.

Anyway -- the issue appears whenever there is a constructor parameter with the name override (the field mentioned in the "trying to get location information on" and "Could not resolve annotation for" messages is the first field in the constructor, however after experimentation it is clearly the override field that is the culprit).

@immutable
@freezed
class TestClass with _$TestClass {
  const TestClass._();

  const factory TestClass({
    required final int? id,           // <-- errors will mention this line...
    required final bool? override,    // <-- but removing this causes the code to be generated successfully.
  }) = _TestClass;

  factory TestClass.fromJson(final Map<String, dynamic> json) =>
      _$TestClassFromJson(json);
}
Stacktrace/Error Log ``` [INFO] ------------------------------------------------------------------------ [INFO] Starting Build [INFO] Updating asset graph completed, took 0ms [WARNING] json_serializable on [[FILEPATH REDACTED]]: An unexpected error was thrown trying to get location information on `bool? override` (ConstFieldElementImpl). Please file an issue at https://github.com/dart-lang/source_gen/issues/new Include the contents of this warning and the stack trace along with the version of `package:source_gen`, `package:analyzer` from `pubspec.lock`. RangeError: Index: 0, Size: 0 [SEVERE] json_serializable on [[FILEPATH REDACTED]]: Could not resolve annotation for `bool? override`. [INFO] Running build completed, took 147ms [INFO] Caching finalized dependency graph completed, took 44ms [SEVERE] Failed after 193ms [INFO] ------------------------------------------------------------------------ ```
Relevant Excerpt of pubspec.lock ```yaml source_gen: dependency: transitive description: name: source_gen sha256: fc0da689e5302edb6177fdd964efcb7f58912f43c28c2047a808f5bfff643d16 url: "https://pub.dev" source: hosted version: "1.4.0" analyzer: dependency: transitive description: name: analyzer sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893" url: "https://pub.dev" source: hosted version: "6.2.0" # ...also potentially relevant json_serializable: dependency: "direct dev" description: name: json_serializable sha256: aa1f5a8912615733e0fdc7a02af03308933c93235bdc8d50d0b0c8a8ccb0b969 url: "https://pub.dev" source: hosted version: "6.7.1" freezed: dependency: "direct dev" description: name: freezed sha256: be7826ed5d87e98c924a839542674fc14edbcb3e4fc0adbc058d680f2b241837 url: "https://pub.dev" source: hosted version: "2.4.3" freezed_annotation: dependency: "direct main" description: name: freezed_annotation sha256: c3fd9336eb55a38cc1bbd79ab17573113a8deccd0ecbbf926cca3c62803b5c2d url: "https://pub.dev" source: hosted version: "2.4.1" ```
jakemac53 commented 9 months ago

This looks like it is a limitation of freezed - if you look at your .freezed.dart generated code you will see a class like this:

@JsonSerializable()
class _$TestClassImpl extends _TestClass {
  @override
  final int? id;

  @override
  final bool? override;
}

The @override in each of these is actually pointing to the local variable and not the constant from dart:core. This is why you see the error on the int? id also - it is the first place the @override annotation appears.

To fix this freezed should import dart:core with a prefix, and use that prefix in the @override annotations. Or it should just give an error saying that you can name a field @override.

I am closing the issue here since I can't transfer it to another repo, but please re-open the issue on the freezed repo.

jakemac53 commented 9 months ago

Oh actually I just realized since this is a part file, freezed can't generate its own imports, so likely the issue would be closed as not feasible to fix. But it is worth still filing an issue over there to start a discussion.

I think freezed should give an explicit error here.