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

Wrong file path passed to RuboCop and Reek #719

Closed
FooBarWidget opened this issue Mar 25, 2021 · 12 comments
Closed

Wrong file path passed to RuboCop and Reek #719

FooBarWidget opened this issue Mar 25, 2021 · 12 comments

Comments

@FooBarWidget
Copy link
Contributor

FooBarWidget commented Mar 25, 2021

Your environment

  • vscode-ruby version: 0.28.1, and also commit 7b5b602
  • Ruby version: 2.7.2
  • Ruby version manager (if any): RVM
  • VS Code version: 1.54.3
  • Operating System: macOS Catalina
  • Using language server? (eg useLanguageServer is true in your configuration?) yes

Expected behavior

RuboCop is run with these arguments:

[
  "bundle",
  "exec",
  "rubocop",
  "-s",
  "/path-to-file.rb",
  "-f",
  "json"
]

Reek is run with these arguments:

[
  "bundle",
  "exec",
  "reek",
  "-f",
  "json",
  "--stdin-filename",
  "/path-to-file.rb"
]

Actual behavior

RuboCop is run with these arguments:

[
  "bundle",
  "exec",
  "rubocop",
  "-s",
  "'/path-to-file.rb'",  // <--- note the extraneous single quotes!
  "-f",
  "json"
]

Reek is run with these arguments:

[
  "bundle",
  "exec",
  "reek",
  "-f",
  "json",
  "--stdin-filename",
  "'/path-to-file.rb'"    // <--- note the extraneous single quotes!
]

To reproduce:

  1. Create a shell script ~/fake-rubocop that prints the exact arguments passed:

    #!/bin/sh
    set -x
    exec bundle exec rubocop "$@"

    Make sure to chmod +x it.

  2. Create a shell script ~/fake-reek that prints the exact arguments passed:

    #!/bin/sh
    set -x
    exec bundle exec reek "$@"

    Make sure to chmod +x it.

  3. Use this configuration:

    "ruby.useLanguageServer": true,
    "ruby.lint": {
        "rubocop": {
            "command": "/path-to-home-directory/fake-rubocop"
        },
        "reek": {
            "command": "/path-to-home-directory/fake-reek"
        }
    },
  4. Open a Ruby project directory. Inside that project directory, open a random Ruby file.

  5. In the Ruby Language Server output, observe that fake-rubocop logs this:

    + exec bundle exec rubocop -s ''\''/path-to-file.rb'\''' -f json
    

    We expect no extraneous quotes:

    + exec bundle exec rubocop -s /path-to-file -f json
    
  6. In the Ruby Language Server output, observe that fake-reek logs this:

    + exec bundle exec reek -f json --stdin-filename ''\''/path-to-file.rb'\'''
    

    We expect no extraneous quotes:

    + exec bundle exec reek -f json --stdin-filename /path-to-file.rb
    

Comments

Because of the extraneous quotes, we pass invalid filenames to RuboCop and Reek.

I don't know whether this causes problems for Reek, but it does cause problems for RuboCop: see #227. Normally, RuboCop checks each level of parent directory in the input file's path, in search for a .rubocop.yml. But because we pass an invalid filename, RuboCop can't find a .rubocop.yml, and ends up ignoring the .rubocop.yml that the project has.

The extraneous quotes are deliberately inserted by the code:

  • In packages/language-server-ruby/src/linters/RuboCop.ts:

    let args = ['-s', `'${documentPath.fsPath}'`, '-f', 'json'];
  • In packages/language-server-ruby/src/linters/Reek.ts:

    return ['-f', 'json', '--stdin-filename', `'${documentPath.fsPath}'`];

I suspect that the author meant to prevent any issues with spaces in filenames. However, Linters are executed through cross-spawn, which doesn't execute commands through a shell. There should be no problems with spaces in filenames: each element in the argument array represents exactly one argument, with no shell trying to split up filenames by space.

@wingrunr21
Copy link
Collaborator

This change was introduced in #647 by @coneybeare.

@coneybeare would you be able to provide your insight here?

@wingrunr21
Copy link
Collaborator

ok. @coneybeare if you don't have any comments here it sounds like #647 caused a regression that I'd like to correct. Absent input to the contrary I'll likely merge #720

Giving this through the end of this week then I'll be merging.

@coneybeare
Copy link
Contributor

oh hm, I actually did comment, not sure where it went.

Here was the original issue that prompted my PR. Despite OPs assertion that There should be no problems with spaces in filenames, I saw that they were necessary, at least at the time of the PR.

As long as the proposed fix doesn't introduce a regression for what I fixed, I am onboard.

@AndreiEres
Copy link

Any updates?

@FooBarWidget
Copy link
Contributor Author

I plan on looking into the issue #641 that @coneybeare referenced. I just haven't had time so far.

@FooBarWidget
Copy link
Contributor Author

FooBarWidget commented Apr 25, 2021

@coneybeare I've tested #641. Just like you, I'm on macOS. I've confirmed that my PR #720 has no problems with spaces in filenames.

Test setup

  • Rubocop 1.12.0, Ruby 2.7.2, macOS Catalina.

  • Create a project directory ~/ruby project with spaces

  • Create a file ~/ruby project with spaces/sub dir/rubyfile_bad.rb with:

    def bad
      "rubocop will complain about double quotes"
    end
  • Create a file ~/ruby project with spaces/sub dir/rubyfile_good.rb with:

    def good
      "rubocop will not complain about double quotes"
    end
  • Create a file ~/ruby project with spaces/.rubocop.yml with:

    Style/FrozenStringLiteralComment:
      Enabled: false
    Style/StringLiterals:
      Exclude:
        - sub dir/rubyfile_good.rb
  • Create a file ~/ruby project with spaces/.vscode/settings.json with:

    {
        "ruby.useLanguageServer": true,
        "ruby.lint": {
            "rubocop": true
        }
    }

Test steps

Expected behavior

  • VSCode runs Rubocop.
  • Rubocop complains only about ruby_bad.rb, but not about ruby_good.rb.

Actual behavior

Matches expected behavior.

@github-actions
Copy link

This issue has not had activity for 30 days. It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale label May 26, 2021
@FooBarWidget
Copy link
Contributor Author

Please don't auto-close this PR. It's awaiting review.

@github-actions github-actions bot removed the stale label May 27, 2021
@github-actions
Copy link

This issue has not had activity for 30 days. It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale label Jul 27, 2021
@FooBarWidget
Copy link
Contributor Author

Please don't auto-close this issue. It's awaiting review.

@github-actions github-actions bot removed the stale label Jul 28, 2021
@github-actions
Copy link

This issue has not had activity for 30 days. It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale label Sep 26, 2021
@ghost
Copy link

ghost commented Sep 26, 2021 via email

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

No branches or pull requests

4 participants