After starting a Dart command line app using the --observe flag, it would be useful to be able to connect the VS Code debugger to the observatory port.
(For example, this could be used to debug the analysis server itself after it's been started from within an IDE.)
Made a start on this, though there's currently a lot of stuff on the debugger that assumes it has info that attaching doesn't provide (like the path of the executing program, which it uses to build a map of package uris and it also assumes access to stdout/stderr that we won't have) so I need to do a bit more investigation (to see which we really need and whether we can get some directly from Observatory).
Do you know happen to know if any of the other OSS IDEs provide this functionality that I might be able to use as a reference?
Hi, last week I refactored some code in the IDEA Flutter plugin that does source file mapping [1].
It's not quite the same since it's for mobile apps, not command-line apps, and we're not doing remote debugging for Flutter apps. But I don't think the algorithm changed much from the Dart plugin (not intentionally, anyway).
It's rather ad-hoc; the way the Observatory exposes pathnames for source files seems like a very leaky abstraction.
For the analysis server in particular, the Dart VM is running from a snapshot file and I assume that has source paths embedded in it somehow, but I don't know the details of how that works. (Presumably it's up to the user to check out the SDK source code for the same version, and then we have to match up the paths somehow.)
I'd need this for debugging inside docker.
Anything I can do to help ?
If you look in dart_debug_impl you'll find code for spawning the process, extracting observatory port and then connecting to it. What you really want to do is change this to not spawn a process, but to connect to the observatory url provided to it (which would be passed in via the launch args).
Initially, you could make it so you just hard-code this in the launch.json, but it'd be good to figure out how the attach functionality works in Code, so ideally the user could be prompted to paste in an observatory uri at the time of trying to attach.
There's some very old code where I started this here that might help illustrate this better:
https://github.com/Dart-Code/Dart-Code/commit/65410a86ff4063c2c1ba163f3cef86df2ba63e7d
The complicated bit might be knowing where the .packages file (etc.) are, so you can do path mapping (though possibly you can make it work without that, it just won't handle breakpoints in packages very well yet).
I have an initial implementation here: https://github.com/Dart-Code/Dart-Code/pull/867
I'm sure it'll need iteration, but I am able to successfully attach to and debug a Dart process.
This has been merged into master (thanks @stuartmorgan!), and will ship in v2.13. It doesn't fully support Flutter (eg. hot reload, etc.) but I've opened #905 about that.
If you have any issues with it, please open issues as usual :-)