emacs-lsp/lsp-dart

Flutter 3 debug support issues on some cases on MacOS

elken opened this issue ยท 27 comments

elken commented

Describe the bug
Trying to dap-debug in flutter_sample causes a JSON parsing error to show up.

Making a minor change in lsp-dart-flutter-daemon--raw->response to print the incoming response, it's truncated by something and returned in a batch.

To Reproduce

Expected behavior

Being able to debug a flutter project

Screenshots

image

Version

[Flutter SDK] /opt/homebrew/Caskroom/flutter/3.0.0/flutter/
[Flutter project] false
[Project entrypoint] Not found
[LSP Dart] 1.21.0 at 2022.05.19 @ Emacs 28.1
[Dart SDK] Dart SDK version: 2.17.0 (stable) (Mon May 9 10:36:47 2022 +0200) on "macos_arm64"

[Flutter SDK] /opt/homebrew/Caskroom/flutter/3.0.0/flutter/
[Flutter project] true
[Project entrypoint] /Users/elken/build/test/lsp-dart/flutter_sample/lib/main.dart

I can't repro that, so I think it's something specific to the json, could you paste the json that show on the lsp dart flutter daemon buffer?

elken commented

I can't repro that, so I think it's something specific to the json, could you paste the json that show on the lsp dart flutter daemon buffer?

It's likely due to the sheer amount of devices I have. Try adding a bunch of emulators, but JSON here. The only change I've made is flutter 2 -> flutter 3.

Though I do see a new macos device, I wonder if that's causing it?

the json parse is weird, I don't think it's related to a new device

elken commented

OK so this gets weirder, there's something funky with the response.

If I open the daemon buffer, go to one of the "faulty" lines and do beginning-of-line it takes me halfway through to where it would normally split...

elken commented
Screen.Recording.2022-05-20.at.08.03.06.mov

All I'm doing is C-a and C-e (turned evil off to see if that was confusing it)

elken commented

I also get the same behaviour on eglot, so it's not even related to lsp-mode :/

Is it worth @'ing DanTup to assist here?

Upon running dap-debug I get:

Debug session process exited with status: killed

After the UI briefly flashes with some cool colours and UI elements. This occurs regardless of if I'm running the app or not.

elken commented

Repro case:

  1. Clone flutter_sample
  2. Change branch to flutter-3.0
  3. emacs -Q -l dap.el with the below
(setq user-emacs-directory "/tmp/emacs")
(setq package-user-dir (expand-file-name "elpa" user-emacs-directory))
(when (boundp 'native-comp-eln-load-path)
  (push (expand-file-name "eln-cache" user-emacs-directory) native-comp-eln-load-path))

(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(package-initialize)

(setq package-selected-packages
  '(dart-mode lsp-mode lsp-dart lsp-treemacs flycheck company
    ;; Optional packages
    lsp-ui company hover))

(when (cl-find-if-not #'package-installed-p package-selected-packages)
  (package-refresh-contents)
  (mapc #'package-install package-selected-packages))

(add-hook 'dart-mode-hook 'lsp)

(setq gc-cons-threshold (* 100 1024 1024)
      read-process-output-max (* 1024 1024))

(find-file "~/build/test/lsp-dart/flutter_sample/lib/main.dart")
  1. dap-debug
  2. Flutter :: Debug
  3. Select any emulator
elken commented

So I "think" I know what's happening.

Something is truncating the buffer, nothing I do seems to resolve it. inhibit-field-text-motion t makes the buffer work correctly, but it doesn't make comint-output-filter take the whole line.

Due to "something" in a recent version of Flutter, the response that comes back when trying to debug becomes truncated. Without any changes, C-e followed by C-a moves to the middle of the line and the character has the following text properties:

There are text properties here:
  field                output
  fontified            t
  front-sticky         (field inhibit-line-move-field-capture)
  inhibit-line-move-field-capture t
  rear-nonsticky       (field inhibit-line-move-field-capture read-only font-lock-face)

I don't know what sets them or why. With the below, I can navigate through the buffer fine but comint-output-filter still recieves split lines. I've tried using comint-preoutput-filter-functions to return the string without text properties but that doesn't seem to help.

  (setq-local comint-max-line-length 10240)
  (setq-local comint-buffer-maximum-size 10240)
  (setq-local inhibit-field-text-motion t)

WDYT @ericdallo ?

Trace output: https://gist.github.com/elken/ab32227d2126023c7d3b2eeeb42e1494

@elken I can't repro the json issue, I'm starting to think it happens only on your emacs :/ I have 3 emulators including linux native device but not getting that issue, could you try using the old debugger to check if it works? you need to disable lsp-dart-dap-use-sdk-debugger and remember to lsp-dart-dap-setup

elken commented

Others have reported before in the discord, so it's either related to macos or emacs-plus or something else then maybe. Not sure.

No change, it fails before it even gets to hit the debugger.

I'm working on refactoring it to include a JSONRPC version to see if that changes it.

@yyoncho any suggestions on how to debug that json parse error? it seems to me something related to a minor mode or something that it's preventing beginning of the buffer works properly

elken commented

Finally got around to testing on Linux, and there is indeed no issue here it seems.

So it seems to somehow related to macOS...

elken commented

https://gist.github.com/elken/852f5656e2416c5b695710db18416df9

Output of (buffer-string) in the daemon buffer after running and trying to trigger the issue. With the latest commits, I can also start the chrome debugger, but it fails with the same error shortly after.

I've tried with the nix version of emacs 28.1 and emacs-mac with the exact same result.

Any progress on this issue? I am having the exact same issue with @elken.

elken commented

#165 was started as a means to address this properly as it's definitely just comint (and provide a faster, stable interface overall) but I have since been moved away from the flutter project that was blocking me.

If eric is able to find time to review what's there I can try and look further :D

@elken I thought it was not ready because of tests failing :)
Could you rebase and fix tests?

elken commented

@elken I thought it was not ready because of tests failing :) Could you rebase and fix tests?

There are a couple of issues I was struggling to resolve, but I will look into fixing them when I can :)

Encountering a similar issue here.

In my particular case, the error is:

error in process filter: could not parse JSON stream: "invalid token near 'fal'", "<string>", 1, 1024, 1024 [4 times]

Now, opening *LSP Dart - Flutter daemon buffer, the interesting bit is that this first displays ---

[{"id":55744,"result":[{"id":"28FE18E8-C80A-4EFF-987D-6CE94AB317E8","name":"iPhone 13","platform":"ios","emulator":true,"category":"mobile","platformType":"ios","ephemeral":true,"emulatorId":"apple_ios_simulator","sdk":"com.apple.CoreSimulator.SimRuntime.iOS-15-0","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":true,"fastStart":false,"flutterExit":true,"hardwareRendering":false,"startPaused":true}},{"id":"macos","name":"macOS","platform":"darwin","emulator":false,"category":"desktop","platformType":"macos","ephemeral":false,"emulatorId":null,"sdk":"macOS 12.4 21F79 darwin-arm","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":false,"fastStart":false,"flutterExit":true,"hardwareRendering":true,"startPaused":true}},{"id":"chrome","name":"Chrome","platform":"web-javascript","emulator":false,"category":"web","platformType":"web","ephemeral":false,"emulatorId":null,"sdk":"Google Chrome 103.0.5060.53","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":false,"fastStart":fal

Note that the message itself was cut short ending at fal. And then after a while, it then completes the line to:

[{"id":55744,"result":[{"id":"28FE18E8-C80A-4EFF-987D-6CE94AB317E8","name":"iPhone 13","platform":"ios","emulator":true,"category":"mobile","platformType":"ios","ephemeral":true,"emulatorId":"apple_ios_simulator","sdk":"com.apple.CoreSimulator.SimRuntime.iOS-15-0","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":true,"fastStart":false,"flutterExit":true,"hardwareRendering":false,"startPaused":true}},{"id":"macos","name":"macOS","platform":"darwin","emulator":false,"category":"desktop","platformType":"macos","ephemeral":false,"emulatorId":null,"sdk":"macOS 12.4 21F79 darwin-arm","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":false,"fastStart":false,"flutterExit":true,"hardwareRendering":true,"startPaused":true}},{"id":"chrome","name":"Chrome","platform":"web-javascript","emulator":false,"category":"web","platformType":"web","ephemeral":false,"emulatorId":null,"sdk":"Google Chrome 103.0.5060.53","capabilities":{"hotReload":true,"hotRestart":true,"screenshot":false,"fastStart":false,"flutterExit":false,"hardwareRendering":true,"startPaused":true}}]}]
elken commented

Looks like Mac users fall into the 1024 max line length: https://github.com/emacs-mirror/emacs/blob/9c9e34e9b683b6b548d92dc7c1ed57f06f62eefb/lisp/comint.el#L492

#163 (comment) please read the whole thread before commenting :)

Apologies for the duplicate. :) Perhaps the add here is that, 1024 limit is probably just on the mac. Linux and Windows users won't run into it.

Not particularly good with elisp, and unsure what this does. But setting this before launching any dart project solves the issue.

(setq process-connection-type nil)
elken commented

Doesn't work here

Needs to get it set before daemon process start. Just to test the workaround, do put it on init.el

elken commented

I did, and it's probably not something that should be set globally anyway.

This should be fixed by #165, LMK otherwise