Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Auto-formatting is deleting all text #443

Closed
compostbrain opened this issue Mar 1, 2019 · 25 comments
Closed

Auto-formatting is deleting all text #443

compostbrain opened this issue Mar 1, 2019 · 25 comments
Assignees
Labels
bug Bug reports

Comments

@compostbrain
Copy link

Your environment

  • vscode-ruby version:
  • Ruby version: 2.5
  • Ruby version manager (if any): asdf
  • VS Code version:1.31.1
  • Operating System:macos
  • Using language server? yes

Expected behavior

autoformat on save

Actual behavior

deletes the contents of the entire file
This and the next section should include screenshots, code samples, console output, etc. The more information we have to reproduce the better!

@wingrunr21
Copy link
Collaborator

You are going to have to provide more information

@adamzapasnik
Copy link

Same here.

vscode-ruby version: 0.22.2
Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Ruby version manager: asdf
VS code version: 1.31.0
Operating system: Ubuntu 18.04.2 LTS
Using language server? yes

What other information do you need?
This is an output from OUTPUT tab.

Lint: executing reek -f json...
Lint: executing rubocop -s somefile.rb -f json...
Lint: attempted to lint with unsupported linter: ruby
Lint: attempted to lint with unsupported linter: fasterer
Lint: attempted to lint with unsupported linter: debride
Lint: attempted to lint with unsupported linter: ruby-lint

@wingrunr21
Copy link
Collaborator

Well just to start: what formatter? What's your configuration? What's the console output? Do you see a Format: call in the console?

Basically the part in the issue template that wasn't filled out: This and the next section should include screenshots, code samples, console output, etc. The more information we have to reproduce the better!

@adamzapasnik
Copy link

Oh, you are right. I posted Lint output, welp. I'm using rubocop.
I do see format call:
Format: executing bundle exec rubocop -s somefile.rb -a...

I noticed that when I launch/reload vscode it throws error rubocop failed with code=1 but I don't see any more details. I can find rubocop in the terminal, though.

which rubocop
/home/username/.asdf/shims/rubocop

configuratioon:

  "ruby.codeCompletion": "rcodetools",
  "ruby.intellisense": "rubyLocate",
  "ruby.useBundler": true,
  "ruby.useLanguageServer": true,
  "ruby.lint": {
    "reek": true,
    "rubocop": true,
    "ruby": true, //Runs ruby -wc
    "fasterer": true,
    "debride": true,
    "ruby-lint": true
  },
  "ruby.format": "rubocop",

@wingrunr21
Copy link
Collaborator

Do you have a lot of lint errors in that file? I wonder if it's related to another issue where the RuboCop output is really long

@adamzapasnik
Copy link

It happens even to a file with only 1 lint error.

@wingrunr21
Copy link
Collaborator

Ok. It is this specific line + this ternary that's doing that. I'll have to take another pass as I introduced those to fix other bugs with selection formatting.

@wingrunr21 wingrunr21 self-assigned this Mar 4, 2019
@wingrunr21 wingrunr21 added the bug Bug reports label Mar 4, 2019
@adamzapasnik
Copy link

:) Glad you found the cause. Do you need any help with that?

@wingrunr21
Copy link
Collaborator

No, I think I'm going to have to combine the fix with #435. I need to introduce a buffer on all stdout and then process that output based on the return code from the linter/formatter.

@adamzapasnik
Copy link

Seems like a good solution 👍 Good luck then. Feel free to ping me to test the fix.

@wingrunr21 wingrunr21 pinned this issue Mar 5, 2019
@wingrunr21
Copy link
Collaborator

v0.22.3 has the bugfix for this

@adamzapasnik
Copy link

Almost...
If you have "ruby.useBundler": true in config and no rubocop in Gemfile it deletes content of the file and writes this instead:

load command: rubocop (/home/username/.asdf/installs/ruby/2.5.1/bin/rubocop)
Gem::Exception: can't find executable rubocop for gem rubocop. rubocop is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  /home/username/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.17.2/lib/bundler/rubygems_integration.rb:462:in `block in replace_bin_path'
  /home/username/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.17.2/lib/bundler/rubygems_integration.rb:482:in `block in replace_bin_path'
  /home/username/.asdf/installs/ruby/2.5.1/bin/rubocop:23:in `<top (required)>'

Or is it correct behaviour?

@wingrunr21
Copy link
Collaborator

Well, replacing the file contents is probably not correct. The problem I'm trying to work around is that bundler doesn't distinguish between stdout and stderr until v2 (see rubygems/bundler#3353) so I can't just assume stdout is good and stderr is bad.

I'll add a test case for it. This is actually a different issue than originally though so I'll probably open a new one and link them.

@adamzapasnik
Copy link

I see, thanks for the information and the fix 👍

@wingrunr21
Copy link
Collaborator

Can you post the full Output you are seeing? Having trouble reproducing

@adamzapasnik
Copy link

Which Output exactly?

@wingrunr21
Copy link
Collaborator

The Ruby Language Server stuff. I want to see what format command it is attempting to run

@adamzapasnik
Copy link

Format: executing bundle exec rubocop -s somefile.rb -a...
This is what I can see printed from Format

@wingrunr21
Copy link
Collaborator

Interesting. When I try and reproduce this setup:

  • ruby.useBundler: true
  • Gemfile in project without rubocop

I get a correctly handled language server error. Are you using a binstub?

@adamzapasnik
Copy link

AFAIK, no, I don't.

@wingrunr21
Copy link
Collaborator

ok but you have rubocop installed globally? /home/username/.asdf/installs/ruby/2.5.1/bin/rubocop seems to suggest yes

@adamzapasnik
Copy link

Yes, I do.

which rubocop
=> /home/username/.asdf/shims/rubocop

@wingrunr21
Copy link
Collaborator

Reproduced it. Thanks

@wingrunr21 wingrunr21 unpinned this issue Mar 6, 2019
@arminanton
Copy link

Was this solved? I'm having the same issue.

@kristof-mattei
Copy link

So this is funny, I just hit this issue. And I switched to .asdf yesterday...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug reports
Projects
None yet
Development

No branches or pull requests

5 participants