devtools icon indicating copy to clipboard operation
devtools copied to clipboard

Define standard event for navigating IDEs to view a file + line + column

Open jacob314 opened this issue 3 years ago • 3 comments

Currently the inspector does a lot of extra work to navigate to select a widget. We should instead dispatch an event with a file + line + column indicating IDEs should navigate to that location.

Flutter should dispatch that event directly rather than relying on IDEs listening for the dart:developer inspect event. DevTools should also dispatch the event to trigger code navigation in the IDE from all places where DevTools wants to trigger navigation in the IDE.

jacob314 avatar Aug 01 '22 15:08 jacob314

Fyi @bkonyi as another option would be have a first class dart:developer API for this rather than just a convention Flutter, DevTools, and IDEs follow.

jacob314 avatar Aug 01 '22 15:08 jacob314

That's potentially an option we could investigate. Can you file an issue against the SDK?

bkonyi avatar Aug 02 '22 14:08 bkonyi

Created: https://github.com/dart-lang/sdk/issues/49606

polina-c avatar Aug 06 '22 02:08 polina-c

@CoderDake fixed this. You can now call this tool event to navigate in IDEs https://github.com/flutter/flutter/blob/9c72f5a7e62e63c199739267f3feeee0559caf11/packages/flutter/lib/src/widgets/widget_inspector.dart#L1518

  postEvent(
        'navigate',
        <String, Object>{
          'fileUri': location.file, // URI file path of the location.
          'line': location.line, // 1-based line number.
          'column': location.column, // 1-based column number.
          'source': 'flutter.inspector',
        },
        stream: 'ToolEvent',
      );
      ```

jacob314 avatar May 12 '23 20:05 jacob314

This works great, thanks!

Just one small thing I noticed: If the fileUri is invalid, somehow the IDE seems to disconnect from the attached app

rrousselGit avatar May 13 '23 22:05 rrousselGit

In particular, I sent instead of sending fileUri: 'file///path/to/file', I sent /path/to/file

rrousselGit avatar May 13 '23 22:05 rrousselGit

@DanTup could this be a bug on the Dart-Code side of handling 'ToolEvent' events?

kenzieschmoll avatar May 15 '23 15:05 kenzieschmoll

It sounds like a bug somewhere if an invalid URI is causing a disconnect. I don't know if it's in Dart-Code or the DAP, but I'll try to repro and fix.

FWIW it is required to be a URI to avoid any confusion with the format of this (for example between Windows and Linux/Mac) and to support non-paths (such as google3:// or package:) which will be resolved on the way through the debug adapter, it should always be a URI. The original expectation was that it was always a file URI (which is why it was named fileUri) but it was only afterwards we found that g3 couldn't provide local file paths. Both DAP and Dart-Code support just uri as well as fileUri, but it doesn't look like Flutter was changed. If VS Code is currently the only client of this, I wonder if we should change the Flutter inspector to uri? (@CoderDake do you recall if there was a reason we didn't do that?)

DanTup avatar May 15 '23 15:05 DanTup

@rrousselGit do you have steps to reproduce? I tried adding this to a counter app, but it doesn't fail, it just doesn't navigate (which is what I was expecting):

void _incrementCounter() {
    print('Sending navigation event!');
    developer.postEvent(
      'navigate',
      {
        // Good:
        // 'uri': Platform.script.toString(),
        // Bad:
        'uri': Platform.script.toFilePath(),
        'line': 1,
        'column': 1,
        'source': 'ketchup',
      },
      stream: 'ToolEvent',
    );

    setState(() {
      _counter++;
    });
  }

DanTup avatar May 15 '23 15:05 DanTup

It happened to me when I was sending a non-URI

Like: 'uri': '/path/to/file.dart'

It could be an issue when if the scheme is missing there's an internal exception

rrousselGit avatar May 15 '23 21:05 rrousselGit

@rrousselGit in the example above I sent a non-URI ('uri': Platform.script.toFilePath(),) but I was unable to cause any disconnect. If you're still able to reproduce this and provide a code sample I'd be interested in debugging. Invalid data might prevent navigating, but it shouldn't cause anything else to fail.

DanTup avatar May 15 '23 21:05 DanTup

I'll try and make a reproducible example next time I see this happen

rrousselGit avatar May 15 '23 22:05 rrousselGit