rubycritic icon indicating copy to clipboard operation
rubycritic copied to clipboard

Ignores Ruby files without a .rb extension

Open cvoltz opened this issue 5 years ago • 4 comments

Rubycritic won't analyze files which don't have the .rb suffix even if the file has a shebang line like:

#!/usr/bin/env ruby 

which indicates it it is a Ruby file. This is true even if the file is explicitly specified either as a trailing argument on the CLI:

rubycritic exe/my-bin

or by using the paths argument in the Rake task:

RubyCritic::RakeTask.new do |task|
  task.paths = FileList["exe/my-bin"]
  task.verbose = true
end

The problem seems to be that the code checks to see if the file has the '.rb' extension: https://github.com/whitesmith/rubycritic/blob/a6ddc92235adb2f4353dfec1286850ebbe48a800/lib/rubycritic/source_locator.rb#L42

This is unnecessarily restrictive. When creating a gem which provides a command the user will run from the CLI, the convention is to provide the Ruby script in the exe directory (which the gem will use to put the script into a bin directory in the user's PATH), add a shebang to run the Ruby interpreter, and set the executable permission on the file (i.e., chmod +x exe/my-bin). Rubycritic doesn't provide any means to analyze such a script.

As an ugly workaround, I have to create a symlink (with the .rb suffix) for each Ruby script in the exe directory and make sure the .gemspec file excludes the symlinked files.

A good solution would be to analyze all files specified explicitly (on the CLI or via the paths argument of the Rake task) and to look at the shebang line of the files which were globbed. Rubocop does this already so you can look at the signatures they look for in the shebang to decide if the file is a Ruby file or not.

cvoltz avatar May 03 '19 16:05 cvoltz

@cvoltz Thanks for reporting this! I like your solution, I think we need to improve the way we detect whether it is a file with Ruby code or not.

In terms of implementing a patch, I would recommend we go with a solution like this:

  1. First check for the extension. If the extension is .rb then it is a Ruby file -- This already works
  2. Then check for the first line in the file, if it has the ruby shebang, then it is a Ruby file. -- This needs to be implemented.

If you want to take a stab at it, I'd be happy to review your PR. 👍

etagwerker avatar Dec 08 '20 14:12 etagwerker

@etagwerker If no one else is working on this yet, can I work on it? I plan to work on this issue using rubocop's FileFinder module as a reference.

NerdyBoyCool avatar Mar 14 '21 09:03 NerdyBoyCool

@NerdyBoyCool as far as I know nobody is actively working on this issue so I'd say go for it. 👍

etagwerker avatar Mar 14 '21 11:03 etagwerker

@etagwerker Thank you!!! I'll do it! 😆

NerdyBoyCool avatar Mar 14 '21 14:03 NerdyBoyCool

@etagwerker looks like this was solved here I think issue can be closed then

itsmeurbi avatar Oct 23 '22 21:10 itsmeurbi