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

Misleading error message when import is missing and part directive is used #1123

Closed
AlexanderJohr opened this issue Mar 8, 2018 · 2 comments
Assignees

Comments

@AlexanderJohr
Copy link
Contributor

AlexanderJohr commented Mar 8, 2018

Dart SDK Version [ 2.0.0-dev.32.0 ]
Package [ build_modules 0.2.0+1 ]
System [ windows_x64 ]

I use json_serializable to generate a part file with toJson and fromJson methods. To reproduce the error I inserted one missing import:

library project.serializables.site_page;

import 'package:project/src/serializables/has_parent_page.dart'; // missing
import 'package:json_annotation/json_annotation.dart';

part 'site_page.g.dart';

@JsonSerializable(includeIfNull: false, nullable: false)
class SitePage extends NavigationEntity with _$SitePageSerializerMixin {

When running pub run build_runner (serve | build) I got an error, but not which import is wrong. Instead:

[SEVERE] Build: Failed after 1m 50s
NoSuchMethodError: The getter 'stringValue' was called on null.
Receiver: null
Tried calling: stringValue
#0      Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
#1      _missingImportMessage.<anonymous closure> (package:build_modules/src/errors.dart:94:35)
#2      Iterable.firstWhere (dart:core/iterable.dart:584)
#3      _missingImportMessage (package:build_modules/src/errors.dart:93:56)
<asynchronous suspension>
#4      MissingModulesException.create (package:build_modules/src/errors.dart:76:21)
<asynchronous suspension>
#5      Module.computeTransitiveDependencies (package:build_modules/src/modules.dart:141:43)

I debugged to the line package:build_modules/src/errors.dart:94 and found out the creation of the correct Exception was interrupted. uri of PartOfDirectiveImpl is null and stringValue can't be invoked.

Future<String> _missingImportMessage(
    AssetId sourceId, AssetId missingId, AssetReader reader) async {
  var contents = await reader.readAsString(sourceId);
  var parsed = parseDirectives(contents, suppressErrors: true);
  var import =
      parsed.directives.whereType<UriBasedDirective>().firstWhere((directive) {
                    // directive = PartOfDirectiveImpl "part of project.serializables.site_page;"
                             // uri = null
    var uriString = directive.uri.stringValue; 
    if (uriString.startsWith('dart:')) return false;
    var id = new AssetId.resolve(uriString, from: sourceId);
    return id == missingId;
  }, orElse: () => null);
  var lineInfo = parsed.lineInfo.getLocation(import.offset);
  return '`$import` from $sourceId at $lineInfo';
}
```dart
@jakemac53
Copy link
Contributor

Thanks for the detailed bug report! Will work on fixing this asap.

@jakemac53
Copy link
Contributor

Ugh, this is caused by dart-lang/sdk#32423. Tldr; don't use whereType yet!

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

2 participants