Skip to content

Unable to evaluate expressions with top level symbols like print or class names #18160

Closed

Description

@DanTup

I recently implemented something called "log points" in Dart Code where we let the user provide an expression and we wrap it in print() and send it to the debugger via evaluateInFrame. At some point in the past, I'm certain this worked fine:

logpoint

However today, this seems completely broken. I've tried various versions of Flutter and Dart Code and can't make the example in the screenshot work at all (I even went back several beta versions). However, the same thing seems to work fine in Dart VM scripts.

Here's what I get back from the VM in Flutter when trying to use print:

{
	"id": "400",
	"method": "evaluateInFrame",
	"params": {
		"expression": "print(\"\"\"_counter\"\"\")",
		"frameIndex": 0,
		"isolateId": "isolates/909101337"
	}
},
{
	"jsonrpc": "2.0",
	"result": {
		"type": "@Error",
		"_vmType": "UnhandledException",
		"kind": "UnhandledException",
		"id": "objects\/1",
		"message":"Unhandled exception:\nNoSuchMethodError: Class '_MyHomePageState' has no instance method 'print'.\nReceiver: Instance of '_MyHomePageState'\nTried calling: print(\"_counter\")\n..."
	}
}

And here's what happens if I try to use a class name like DateTime:

Unhandled exception:\n'file:\/\/\/Users\/dantup\/Dev\/Dart-Code\/test\/test_projects\/flutter_hello_world\/lib\/main.dart':
malformed type: line 1 pos 9: cannot resolve class 'DateTime' from 'MyHomePage'

As well as affecting log points, this happens from the Debug Console, so if the user types (new DateTime.now()).year into the Debug Console for a Dart CLI script it works fine, but in Flutter it fails. Using the Flutter version of the SDK for both tests.

Activity

  1. devoncarew commented on Jun 4, 2018

    @devoncarew
    Contributor

    I think this is a dart2 issue - when the CFE is enabled by default. /cc @aam, who's been working expression evaluation in dart2. Alex, is there any new guidance in using expr. eval w/ the CFE enabled?

  2. aam commented on Jun 4, 2018

    @aam
    Member

    Currently, in Flutter engine expressions are evaluated in dart1 mode by dart1-compilant parser that is built-in into engine's dart vm. With https://dart-review.googlesource.com/c/sdk/+/49061 and #16161 it will get switched to using flutter tools dart2 frontend for expression compilation.

    Having said that, I am not sure what is the cause for errors above. Are you able to reproduce those failures if you use expression evaluation in observatory for Flutter app?

  3. DanTup commented on Jun 4, 2018

    @DanTup
    ContributorAuthor

    @aam Yep, same thing in Observatory web app:

    $ p print(1)
    Unhandled exception:
    NoSuchMethodError: No top-level method 'print' declared.
    Receiver: top-level
    Tried calling: print(1)
    #0      NoSuchMethodError._throwNew (dart:core/runtime/liberrors_patch.dart:203:5)
    #1      Eval (evaluate:source:1:7)
    #2      main (file:///Users/dantup/Dev/Dart-Code/test/test_projects/flutter_hello_world/lib/main.dart:3:10)
    #3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
    #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
    
    $ p new DateTime.now()
    Unhandled exception:
    'file:///Users/dantup/Dev/Dart-Code/test/test_projects/flutter_hello_world/lib/main.dart': malformed type: line 1 pos 5: cannot resolve class 'DateTime' from '::'
    import 'package:flutter/material.dart';
        ^
    
    
    #0      _TypeError._throwNew (dart:core/runtime/liberrors_patch.dart:84:75)
    #1      Eval (evaluate:source:1:11)
    #2      main (file:///Users/dantup/Dev/Dart-Code/test/test_projects/flutter_hello_world/lib/main.dart:3:10)
    #3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
    #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
    

    (FWIW, I did try passing --no-preview-dart-2 to flutter run and indeed it had no effect.)

  4. DanTup commented on Jun 4, 2018

    @DanTup
    ContributorAuthor

    Since I have a screenshot showing this working from only a couple of weeks ago, if it's useful I could bisect this and find out which changesets work/fail if it's useful (it'll be a little bit manual, so I'm only gonna do it if you think it'd be useful).

  5. aam commented on Jun 4, 2018

    @aam
    Member

    Yeah @DanTup , if you could let me know what revision of Flutter p print(1) worked in observatory, that would help. Thanks!

  6. aam commented on Jun 4, 2018

    @aam
    Member

    Ah, I realized just now I was not going far enough back to find working version of Flutter. Turns out it got broken back in March with culprit being 71b2cfb

  7. aam commented on Jun 4, 2018

    @aam
    Member

    This should get fixed with the switch to dart2 frontend for expression compilation mentioned above in #18160 (comment)

  8. DanTup commented on Jun 5, 2018

    @DanTup
    ContributorAuthor

    Turns out it got broken back in March

    Weird, I must've somehow been on a really old version of Flutter when I coded this (which is possible, I do have many versions checked out from testing; but I hadn't deliberately been using them recently!).

    Is switching the expression compilation to Dart 2 far off?

  9. aam commented on Jun 5, 2018

    @aam
    Member

    Hopefully it happens today

  10. aam commented on Jun 5, 2018

    @aam
    Member

    This should be fixed in f11c8d9.

  11. DanTup commented on Jun 6, 2018

    @DanTup
    ContributorAuthor

    LGTM, thanks! :)

    screen shot 2018-06-06 at 7 15 31 am

  12. DanTup commented on Jun 18, 2018

    @DanTup
    ContributorAuthor

    @aam there's a report in flutter-dev of evaluation issues recently - I'm not sure if they may have been introduced with this change:

    https://groups.google.com/forum/#!topic/flutter-dev/O-mTAk00hos

    I can't reproduce any issues myself so have asked for more info, but thought I'd give you a heads up. I'll open a new issue and ping you on it if I can get a repro/log file.

  13. aam commented on Jun 18, 2018

    @aam
    Member

    thanks for the heads up @DanTup

  14. github-actions commented on Sep 3, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  15. locked as resolved and limited conversation to collaborators on Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions