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

Issue with analyzer 0.40.5, InconsistentAnalysisException #941

Closed
alanknight-wk opened this issue Nov 2, 2020 · 30 comments
Closed

Issue with analyzer 0.40.5, InconsistentAnalysisException #941

alanknight-wk opened this issue Nov 2, 2020 · 30 comments
Assignees
Labels

Comments

@alanknight-wk
Copy link

I haven't dug into this more deeply, but in attempting to run with bleeding edge 2.12 and analyzer 0.40.5 I encountered the following error building one of our packages. It may be something that's going to be an issue in upcoming builds.

[SEVERE] built_value_generator:built_value on lib/src/data/models/attachment.sg.dart:
Error in BuiltValueGenerator for abstract class Attachment implements Built<Attachment*, dynamic>*.
InconsistentAnalysisException: Requested result might be inconsistent with previously returned results

@davidmorgan
Copy link
Collaborator

Thanks Alan. That's a new one to me ... @scheglov could you give any pointers on why we might get InconsistentAnalysisException, please?

@davidmorgan davidmorgan self-assigned this Nov 3, 2020
@kuhnroyal
Copy link
Contributor

kuhnroyal commented Nov 3, 2020

See #923 (comment)

Also I now managed to generate with analyzer: 0.40.5.

The problem was caused by one of build > 1.3.0, build_runner > 1.10.2 or source_gen > 0.9.5 but I can't nail it down exactly, might be a combination of those that fails.

@kuhnroyal
Copy link
Contributor

Yea, as soon as I set build_runner: 1.10.3 it fails, this also pulls in build: 1.5.0.

@scheglov
Copy link
Contributor

scheglov commented Nov 3, 2020

You would get InconsistentAnalysisException if you try to call anything on a AnalysisSession that was asked from AnalysisDriver at time T, if you called AnalysisDriver.changeFile(path) or AnalysisDriver.addFile(path) after T.

@davidmorgan
Copy link
Collaborator

@scheglov I'm stuck--any chance you could take a look please?

Repro here: https://github.com/davidmorgan/built_value.dart/tree/publishable-null-safety
Then in folder end_to_end_test run pub run build_runner build to get

Precompiling executable... (14.2s)
Precompiled build_runner:build_runner.
[INFO] Generating build script completed, took 260ms
[INFO] Reading cached asset graph completed, took 61ms
[INFO] Checking for updates since last build completed, took 437ms
[INFO] Running build completed, took 17ms
[INFO] Caching finalized dependency graph completed, took 43ms
[SEVERE] built_value_generator:built_value on lib/mixins_nnbd.dart (cached):
Unknown error in BuiltValueGenerator for /end_to_end_test/lib/mixins_nnbd.dart. #0      AnalysisSessionImpl._checkConsistency (package:analyzer/src/dart/analysis/session.dart:146:7)
#1      AnalysisSessionImpl.getParsedLibraryByElement (package:analyzer/src/dart/analysis/session.dart:93:5)
#2      SerializerSourceClass.parsedLibrary (package:built_value_generator/src/serializer_source_class.dart:39:31)
#3      _$SerializerSourceClass.parsedLibrary (package:built_value_generator/src/serializer_source_class.g.dart:48:33)
#4      SerializerSourceClass.serializerDeclaration (package:built_value_generator/src/serializer_source_class.dart:108:18)
#5      _$SerializerSourceClass.serializerDeclaration (package:built_value_generator/src/serializer_source_class.g.dart:70:41)
#6      SerializerSourceClass.computeErrors (package:built_value_generator/src/serializer_source_class.dart:255:11)
#7      SerializerSourceLibrary.generateCode.<anonymous closure> (package:built_value_generator/src/serializer_source_library.dart:138:43)
#8      MappedIterator.moveNext (dart:_internal/iterable.dart:392:20)
#9      new List.from (dart:core-patch/array_patch.dart:38:29)
#10     new List.of (dart:core-patch/array_patch.dart:68:17)
#11     Iterable.toList (dart:core/iterable.dart:404:12)
#12     SerializerSourceLibrary.generateCode (package:built_value_generator/src/serializer_source_library.dart:139:10)
#13     BuiltValueGenerator.generate (package:built_value_generator/built_value_generator.dart:30:48)
#14     _generate (package:source_gen/src/builder.dart:323:33)
<asynchronous suspension>
#15     Stream.toList.<anonymous closure> (dart:async/stream.dart)
<asynchronous suspension>

InconsistentAnalysisException: Requested result might be inconsistent with previously returned results

--the generator code doesn't get to decide when changeFile or addFile is called on AnalysisDriver, so I'm stuck :/ is there any way to get more information about where the problem is happening? Per comments above it may be somewhere in build_runner or source_gen. Thanks!

(Sorry for the horrible repro--built_value has been red on travis for a long time because of null safety being behind a flag, I'm currently working on getting it green again but ran into this issue).

@scheglov
Copy link
Contributor

scheglov commented Nov 17, 2020

I changed the analyzer version that is used in end_to_end_test by modifying pubspec.yaml:

dependency_overrides:
  analyzer:
    path: /Users/scheglov/Source/Dart/sdk.git/sdk/pkg/analyzer

So, I can change analyzer and add a few debug print invocations.
Specifically, when a new AnalysisSession is created (because the previous one is discarded).

The result:

[WARNING] built_value_generator:built_value on test/generics_nnbd_test.dart:
[new session][#0      new AnalysisSessionImpl (package:analyzer/src/dart/analysis/session.dart:38:39)
#1      AnalysisDriver._createNewSession (package:analyzer/src/dart/analysis/driver.dart:1563:23)
#2      AnalysisDriver._changeHook (package:analyzer/src/dart/analysis/driver.dart:1215:5)
#3      FileTracker.changeFile (package:analyzer/src/dart/analysis/file_tracker.dart:129:16)
#4      AnalysisDriver._changeFile (package:analyzer/src/dart/analysis/driver.dart:1207:18)
#5      AnalysisDriver.changeFile (package:analyzer/src/dart/analysis/driver.dart:477:5)
#6      BuildAssetUriResolver.performResolve (package:build_resolvers/src/build_asset_uri_resolver.dart:79:16)
<asynchronous suspension>
#7      PerActionResolver._resolveIfNecessary.<anonymous closure> (package:build_resolvers/src/resolver.dart:124:11)
<asynchronous suspension>
#8      Pool.withResource (package:pool/pool.dart:127:14)
<asynchronous suspension>
#9      PerActionResolver.isLibrary (package:build_resolvers/src/resolver.dart:91:5)
<asynchronous suspension>
#10     _Builder.build (package:source_gen/src/builder.dart:73:10)
<asynchronous suspension>
#11     runBuilder.buildForInput (package:build/src/generate/run_builder.dart:55:7)
<asynchronous suspension>
#12     Future.wait.<anonymous closure> (dart:async/future.dart)
<asynchronous suspension>
#13     scopeLogAsync.<anonymous closure> (package:build/src/builder/logging.dart)
<asynchronous suspension>
]
[SEVERE] built_value_generator:built_value on lib/collections_nnbd.dart:
Unknown error in BuiltValueGenerator for /end_to_end_test/lib/collections_nnbd.dart. #0      AnalysisSessionImpl._checkConsistency (package:analyzer/src/dart/analysis/session.dart:148:7)
#1      AnalysisSessionImpl.getParsedLibraryByElement (package:analyzer/src/dart/analysis/session.dart:95:5)
#2      SerializerSourceClass.parsedLibrary (package:built_value_generator/src/serializer_source_class.dart:39:31)
#3      _$SerializerSourceClass.parsedLibrary (package:built_value_generator/src/serializer_source_class.g.dart:48:33)
#4      SerializerSourceClass.serializerDeclaration (package:built_value_generator/src/serializer_source_class.dart:108:18)
#5      _$SerializerSourceClass.serializerDeclaration (package:built_value_generator/src/serializer_source_class.g.dart:70:41)
#6      SerializerSourceClass.computeErrors (package:built_value_generator/src/serializer_source_class.dart:255:11)
#7      SerializerSourceLibrary.generateCode.<anonymous closure> (package:built_value_generator/src/serializer_source_library.dart:138:43)
#8      MappedIterator.moveNext (dart:_internal/iterable.dart:392:20)
#9      new List.from (dart:core-patch/array_patch.dart:38:29)
#10     new List.of (dart:core-patch/array_patch.dart:68:17)
#11     Iterable.toList (dart:core/iterable.dart:404:12)
#12     SerializerSourceLibrary.generateCode (package:built_value_generator/src/serializer_source_library.dart:139:10)
#13     BuiltValueGenerator.generate (package:built_value_generator/built_value_generator.dart:30:48)
#14     _generate (package:source_gen/src/builder.dart:323:33)
<asynchronous suspension>
#15     Stream.toList.<anonymous closure> (dart:async/stream.dart)
<asynchronous suspension>

InconsistentAnalysisException: Requested result might be inconsistent with previously returned results

So, we see that BuildAssetUriResolver.performResolve invoked AnalysisDriver.changeFile and so invalidated the session.

@davidmorgan
Copy link
Collaborator

Thanks Konstantin! Much appreciated.

I am suspicious about this recent change to build_resolver relating to changeFile

dart-lang/build@c1d82b4#diff-329dca0f34609c00795a91dafd45cf73040588d6fdb37d5839231980523b0ab3

--but it's end of day so I will dig further tomorrow.

@davidmorgan
Copy link
Collaborator

After reading dart-lang/sdk#43744 I checked for flakiness; because of cache in .dart_tools you have to delete the cache to see flakiness. So:

rm -rf .dart_tool; pub get; pub run build_runner build --delete-conflicting-outputs

--and per the repro I linked to above, it is flaky, i.e. it sometimes succeeds. I also noticed I can simplify by making the generator immediately call the thing that's failing:

  @override
  Future<String> generate(LibraryReader library, BuildStep buildStep) async {
    var result = StringBuffer();

    library.element.session.getParsedLibraryByElement(library.element);

and now it looks like this always fails rather than being flaky.

@jakemac53 @natebosch any ideas please?

@jakemac53
Copy link
Contributor

Ya this is a known issue that was always potentially present but just happens more often now.

The problem and workaround (which isn't great) are listed here dart-lang/build#2634 (comment).

@davidmorgan
Copy link
Collaborator

Thanks Jake! I tried the workaround--then tried a bit harder ;) once wasn't enough, I ended up with

    var attempts = 0;
    while (true) {
      try {
        libraryElement = await buildStep.resolver.libraryFor(
            await buildStep.resolver.assetIdForElement(library.element));
        libraryElement.session.getParsedLibraryByElement(libraryElement);
        break;
      } catch (_) {
        ++attempts;
        if (attempts == 10) {
          log.severe('Analysis session did not stabilize after ten tries!');
          return null;
        }
      }
    }

at the start of generate.

@jakemac53
Copy link
Contributor

Ya, its sad because we know here that the session won't be invalid in any meaningful way, but have no way of overriding the behavior :(.

@natebosch
Copy link
Member

I wonder if the same information that we used to get from the ParsedLibraryResult is also available on the CompilationUnit. That can be access through Resolver.compilationUnitFor and might not have the same difficulty around invalidation.

@larssn
Copy link

larssn commented Dec 3, 2020

If this fix/workaround gonna get released soon? I have a lot of built_value models, and a lot of them fail to get built due to the above error.

@davidmorgan
Copy link
Collaborator

It should be possible to make it work by using dependency_overrides to pin some combination of analyzer/build.

Unfortunately it's hard to do a release right now because of the migration to null safety: there is a circular dependency between package:build and package:built_value which complicates things. If enough people are blocked on this I can look at backporting the fix. Sorry about that!

@kuhnroyal
Copy link
Contributor

You can try:

dependency_overrides:
  analyzer: 0.40.5
  build_runner: 1.10.2

@larssn
Copy link

larssn commented Dec 3, 2020

I already have a pin on analyzer 0.41.1, but I still get a ton of InconsistentAnalysisExceptions for seemingly random models.

I'll try pinning build runner as well.

@larssn
Copy link

larssn commented Dec 3, 2020

Thank you both for replying so quick! The pin on build_runner did the trick! 🎉

eernstg added a commit to google/reflectable.dart that referenced this issue Dec 3, 2020
Update analyzer dependency to allow all versions until 0.41.1. Issue #227 reports that 'lib/main.dart' is skipped during code generation; this PR fixes that. Issue #198 about InconsistentAnalysisException re-emerged with analyzer 0.40.5, workaround from google/built_value.dart#941 adopted.

This PR also adds // @Dart=2.9 to tests, because they do not get the language version from the pubspec.yaml.

Prepare for publishing as 2.2.9.
@vixez
Copy link

vixez commented Jan 20, 2021

I had this as well, even when pinning build_runner. I deleted the .dart_tool folder from my project and ran the runner again. That seemed to help.

@jakemac53
Copy link
Contributor

This issue should be resolved on the latest build_runner and built_value now, try removing the pinning and ensure you are on the latest? @davidmorgan did the change to use the provided parsed ast libraries get published for sure?

@davidmorgan
Copy link
Collaborator

It was only published in the last few days as built_value_generator version 8.0.0-nullsafety.0.

@larssn
Copy link

larssn commented Jan 20, 2021

But I take it that 8.x is not released yet (at least it appears it isn't), and I assume it's tied to the next big Flutter/Dart release with null safety.

So the pins have to stay until then.

@davidmorgan
Copy link
Collaborator

8.0.0-nullsafety.0 is a prerelease, so you have to ask for it specifically in the yaml file. But it should work today.

8.0.0 will be published once full releases of the deps (built_collection, collection, fixnum) are released.

@larssn
Copy link

larssn commented Jan 20, 2021

@davidmorgan I still don't think that will work if you're the stable branch of Flutter, since that requires Dart 2.10, and 8.0.0-nullsafety.0 requires Dart 2.12.

@davidmorgan
Copy link
Collaborator

Got it--yes, you are correct, it won't work with a Flutter version pinned before 2.12.

@jakemac53
Copy link
Contributor

Note that this will only work on dev Dart sdks as well. It might be worth backporting a fix to the last release that allows 2.10 stable, but that depends on the work involved to do that etc.

@larssn
Copy link

larssn commented Jan 20, 2021

We tend to only run stable, as we have a production app. We'd only consider running dev to fix critical issues.

We haven't run into any issues after pinning the above, so I guess the question is: is it worth the dev hours to fix something that is about to be phased out anyway.

(At least I hope it's about to be phased out, been waiting on null safety for a while 😁)

@mateusfccp
Copy link

mateusfccp commented May 25, 2021

We are having this same problem in our team. We still didn't upgrade our codebase to null-safety and we don't expect to do it too soon, so we are stuck in analyzer: 0.41.1. Using 0.40.5 will make the building process endless (another bug).

It used to work some weeks ago with 0.41.1, but now I get this error 100% of the time and my teammates also...


@davidmorgan said:

If enough people are blocked on this I can look at backporting the fix. Sorry about that!

This would be very welcome.

@jakemac53
Copy link
Contributor

jakemac53 commented May 25, 2021

@mateusfccp you should be able to update your analyzer without migrating to null safety, fwiw. You can use migrated dependencies without migrating yourself.

That is, unless you are stuck on an older SDK which doesn't support null safety for some reason.

@mateusfccp
Copy link

mateusfccp commented May 25, 2021

@jakemac53 I am stuck with old analyzer because I use some generators that I have to keep pre-null-safety or else it's going to generate null-safe code. I actually have to use some dependencies overrides to keep everything working.

If I try to use other analyzer version I will either get dependencies conflicts or, if I override, runtime errors. I only managed to make our app build properly by using a specific combination of analyzer and build_runner.

@davidmorgan
Copy link
Collaborator

I haven't had much time for built_value fixes/improvements lately :/ ... I'll try to take a look at this at some point.

evanweible-wf added a commit to evanweible-wf/built_value.dart that referenced this issue Aug 25, 2021
This commit includes the same workaround for
google#941 that was merged into the v8.0.0 tag, which
can be found here:

google@413c263#diff-09e8eaf836a5c00d73676397af852777169bc8f047e6a0d18c9f8aa050fe5f47
kevmoo pushed a commit that referenced this issue Aug 26, 2021
…ysisException (#1090)

* Back-patch to 7.x to allow analyzer v0.40.x

The built_value_generator package at version 7.1.0 depends on
`analyzer: '>=0.39.3 <0.40.0'`, which prevents consumers from resolving
newer versions of certain build packages. However, built_value_generator
is completely compatible with analyzer v0.40.x as-is. So, to allow
consumption of built_value* 7.x with newer build packages without also
having to upgrade to Dart 2.12+, this commit widens the analyzer range
to include v0.40.x.

* Backpatch the InconsistentAnalysisException workaround.

This commit includes the same workaround for
#941 that was merged into the v8.0.0 tag, which
can be found here:

413c263#diff-09e8eaf836a5c00d73676397af852777169bc8f047e6a0d18c9f8aa050fe5f47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants