richterger / Perl-LanguageServer

Language Server for Perl
Other
222 stars 53 forks source link

Format Document in vscode removes file contents #87

Closed abraxxa closed 2 years ago

abraxxa commented 3 years ago

This happens under fresh installed Perl 5.32.1 regardless of using perlbrew or plenv. It did work as expected using Perl 5.26.1 installed by perlbrew.

While looking at the log level 2 output I realized that it uses perltidy under the hood which wasn't installed.

So the bug report is to either add Perl::Tidy as a dependency to Perl::LanguageServer or make it disable the 'Format Document' feature if Perl::Tidy isn't installed.

Thanks!

davebaird commented 3 years ago

I have the same issue on Perl v5.30.0 (Ubuntu OS). Perl::Tidy is installed and perl.perlInc in settings.json includes my local modules installation path. If I select some code and format selection, the selected code disappears. If I format the whole file, everything disappears.

UPDATE: I'll leave the log output below, I think this behaviour is a bug but not sure if it's with perltidy or Perl::LanguageServer. The issue was that I was trying to use a perltidy installed to my ~/perl5 directory. I don't know why this doesn't work, as per the log output below, the extension seems to find the local perltidy executable but it gets no output back from it. On the command-line the local perltidy works fine. Anyway, I installed perltidy systemwide sudo apt install perltidy and that resolved the issue. I previously tried symlinking the local perltidy into /usr/bin but then perltidy couldn't find Perl::Tidy.

Log output with loglevel: 2 (using perltidy installed to ~/perl5) -

##### 58 #####
 running: {} coros: {}
LS: end aio read cnt=286
LS: line=<Content-Length: 263>
LS: line=<>
LS: read data={"jsonrpc":"2.0","id":37,"method":"textDocument/rangeFormatting","params":{"textDocument":{"uri":"file:///home/dave/code/z5.igbot/t/scrap.pl"},"range":{"start":{"line":0,"character":0},"end":{"line":16,"character":0}},"options":{"tabSize":4,"insertSpaces":true}}}
LS: read header={ "Content-Length" => 263 }
LS: ---> Request: {
   "id" : 37,
   "jsonrpc" : "2.0",
   "method" : "textDocument/rangeFormatting",
   "params" : {
      "range" : {
         "end" : {
            "character" : 0,
            "line" : 16
         },
         "start" : {
            "line" : 0,
            "character" : 0
         }
      },
      "options" : {
         "tabSize" : 4,
         "insertSpaces" : true
      },
      "textDocument" : {
         "uri" : "file:///home/dave/code/z5.igbot/t/scrap.pl"
      }
   }
}

LS: handle_req id=37
LS: method=_rpcreq_rangeFormatting
LS: {
  options => {
    insertSpaces => bless(do{\(my $o = 1)}, "JSON::PP::Boolean"),
    tabSize => 4,
  },
  range => {
    end => { character => 0, line => 16 },
    start => { character => 0, line => 0 },
  },
  textDocument => { uri => "file:///home/dave/code/z5.igbot/t/scrap.pl" },
}LS: perltidy text: <#!/usr/bin/env perl
use v5.30;
use warnings;

sub hi
{
    say 'hi'
    ;
    }

sub
ho  {
    say 'ho'      ;
    }

>
LS: start perltidy file:///home/dave/code/z5.igbot/t/scrap.pl from line 0 to 15
LS: start aio read
LS: perltidy rc=126 errout=
LS: <--- Notification: {
   "params" : {
      "uri" : "file:///home/dave/code/z5.igbot/t/scrap.pl",
      "diagnostics" : []
   },
   "method" : "textDocument/publishDiagnostics",
   "jsonrpc" : "2.0"
}

LS: <--- Response: {
   "result" : [
      {
         "newText" : "",
         "range" : {
            "end" : {
               "character" : 0,
               "line" : 16
            },
            "start" : {
               "line" : 0,
               "character" : 0
            }
         }
      }
   ],
   "id" : "37",
   "jsonrpc" : "2.0"
}

LS: done handle_req id=37
LS: end aio read cnt=23
LS: line=<Content-Length: 174>
LS: line=<>
LS: read data={"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{"uri":"file:///home/dave/code/z5.igbot/t/scrap.pl","version":36},"contentChanges":[{"text":""}]}}
LS: read header={ "Content-Length" => 174 }
LS: ---> Request: {
   "params" : {
      "textDocument" : {
         "uri" : "file:///home/dave/code/z5.igbot/t/scrap.pl",
         "version" : 36
      },
      "contentChanges" : [
         {
            "text" : ""
         }
      ]
   },
   "method" : "textDocument/didChange",
   "jsonrpc" : "2.0"
}

LS: handle_req id=
LS: method=_rpcnot_didChange
LS: done handle_req id=1000000035
LS: start aio read
LS: end aio read cnt=166
LS: line=<Content-Length: 143>
LS: line=<>
LS: read data={"jsonrpc":"2.0","id":38,"method":"textDocument/documentSymbol","params":{"textDocument":{"uri":"file:///home/dave/code/z5.igbot/t/scrap.pl"}}}
LS: read header={ "Content-Length" => 143 }
LS: ---> Request: {
   "params" : {
      "textDocument" : {
         "uri" : "file:///home/dave/code/z5.igbot/t/scrap.pl"
      }
   },
   "id" : 38,
   "jsonrpc" : "2.0",
   "method" : "textDocument/documentSymbol"
}

LS: handle_req id=38
LS: method=_rpcreq_documentSymbol
LS: start aio read
LS: <--- Response: {
   "result" : [],
   "jsonrpc" : "2.0",
   "id" : "38"
}

LS: done handle_req id=38
start perl -c
perl -c rc=0 out= errout=- syntax OK

LS: <--- Notification: {
   "jsonrpc" : "2.0",
   "method" : "textDocument/publishDiagnostics",
   "params" : {
      "diagnostics" : [],
      "uri" : "file:///home/dave/code/z5.igbot/t/scrap.pl"
   }
}

##### 59 #####
 running: {} coros: {}
##### 60 #####
 running: {} coros: {}

Note the rc from perltidy is 126 (not executable), but

$ which perltidy
/home/dave/perl5/bin/perltidy
$ ls -l /home/dave/perl5/bin/perltidy
-r-xr-xr-x 1 dave dave 183795 Apr  6 15:11 /home/dave/perl5/bin/perltidy*
Direct-A commented 2 years ago

I've got the same issue, but in vscode ouput I found this:

LS: path_map = undef
LS: perlinc = ["/home/xxx/perl5/lib/perl5/"]
LS: file_filter_regex = "(?:\\.pm|\\.pl)\$"
LS: ignore_dir = { ".git" => 1, ".svn" => 1, ".vscode" => 1 }
LS: background_parser folders = {
  "file:///run/media/Data/OneDrive/On_Linux/bioinfo/learn_prel" => "/run/media/Data/OneDrive/On_Linux/bioinfo/learn_prel",
}
LS: apply_all_roles (Moose::Meta::Class::__ANON__::SERIAL::2=HASH(0x55dd5b264988), Perl::LanguageServer::Methods::textDocument, Perl/LanguageServer/Methods/textDocument.pm)
state_dir = /run/media/Data/OneDrive/On_Linux/bioinfo/learn_prel/.vscode/perl-lang
LS: initial parsing done, loaded 2 files, parsed  files, 2 files
LS: ERROR: Unknow perlmethod _rpcnot_setTraceNotification at /home/xxx/perl5/lib/perl5/Perl/LanguageServer.pm line 222.