Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make RuboCop inspect a file in a folder that starts with dot #1401

Closed
zeljkofilipin opened this issue Oct 22, 2014 · 7 comments
Closed
Assignees

Comments

@zeljkofilipin
Copy link

I want to run RuboCop for VisualEditor repository. At the moment, the only Ruby file I could find in the repository is .docs/CustomTags.rb.

$ find . | grep rb
./.docs/CustomTags.rb

If I run only rubocop, it does not find any files:

$ rubocop
Inspecting 0 files
0 files inspected, no offenses detected

I guess it ignores files in folders that start with dot (.docs).

RuboCop documentation on including files says:

If you'd like it to check other files you'll need to manually pass
them in, or to add entries for them under AllCops/Include.

If I provide path to the file from the command line, RuboCop finds the file:

$ rubocop .docs/CustomTags.rb 
Inspecting 1 file
W
(...)
1 file inspected, 26 offenses detected

Our continuous integration just runs rubocop for the repository, so I can not provide path to the file from the command line. I have to use AllCops/Include, but I can not figure out how to do it.

If I create a .rubocop.yml in the root of the repository:

AllCops:
  Include:
    - '.docs/CustomTags.rb'

and run Rubocop, it does not find the file:

$ rubocop
Inspecting 0 files
0 files inspected, no offenses detected

I have tried several variations of the .rubocop.yml file, including:

AllCops:
  Include:
    - '**/CustomTags.rb'

and

AllCops:
  Include:
    - '.docs/**/*'

But none of them are finding the file.

@zeljkofilipin
Copy link
Author

Also asked the question here: http://stackoverflow.com/q/26508803/17469

@jonas054
Copy link
Collaborator

I think that the patterns '.docs/CustomTags.rb' and '.docs/**/*' ought to find the file. They don't and that's a bug IMO. The pattern '**/CustomTags.rb', on the other hand, will not and that's consistent with how Ruby's Dir[pattern] works.

@zeljkofilipin
Copy link
Author

Is there a reason for RuboCop to ignore files in folders that start with dot, like .docs? I could not find it documented anywhere.

@jonas054
Copy link
Collaborator

You could argue that it's more of an accident (depending, as it does, on what Dir["#{base_dir}/**/*"] returns), but at the same time, I do think that there are cases when staying out of the hidden directories is the right thing to do.

Directories given explicitly either on the command line or in configuration files should be searched. Currently, RuboCop only searches the directories given on the command line. Hidden files and directories should be skipped by default.

So I think this needs fixing, and we should also add something in the README about hidden files.

@hashar
Copy link

hashar commented Nov 1, 2014

From an email I wrote about this issue: https://lists.wikimedia.org/pipermail/qa/2014-October/002030.html

Rubocop traverses the tree hierarchy using:

Dir["somepath/**/*"]

Which is the equivalent of:

Dir.glob( "somepath/**/*", 0 )

0 is the flag parameter which can be made File::FNM_DOTMATCH to match dotfiles (including '.' and '..'. So the Dir[] needs to be rewritten to something like:

Dir.glob("somepath/**/*", File::FNM_DOTMATCH)

And add additional logic to skip '.' and '..'.

Related code is target_files_in_dir method: https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/target_finder.rb#L58

The actual filtering via Include/Exclude, happens AFTER the tree traversal (by invoking file_to_include?(file). So whatever you put in the config file, it is never going to find dotfiles.

@jonas054 jonas054 self-assigned this Nov 1, 2014
@jonas054
Copy link
Collaborator

jonas054 commented Nov 1, 2014

@hashar You're quite right. I've been working on a solution, but got side-tracked for a while. Now I think I'll have something ready pretty soon, and it will follow the solution you outlined. Pretty much.

bbatsov added a commit that referenced this issue Nov 1, 2014
[Fix #1401] Enable including hidden directories in config
@zeljkofilipin
Copy link
Author

Thanks! :)

hashar pushed a commit to wikimedia/VisualEditor that referenced this issue Nov 12, 2014
Bump RuboCop to 0.27.1 so it can recognize '.docs', see upstream bug for
details: rubocop/rubocop#1401

Fix minor whitespaces problem that `rubocop --auto-gen-config` did not
put to .rubocop_todo.yml file.

Bug: 63307
Change-Id: I0a640135ca8e63a8efd6e8dc0e982a05097c20aa
koic added a commit to koic/rubocop that referenced this issue Aug 16, 2018
Fixes rubocop#5181.
Fixes rubocop#4832.

This PR changes the path pattern (`*`) to match the hidden file.

This PR resolves the following problem.

```console
AllCops:
  Exclude:
    - 'vendor/bundle/**/*'
```

The above .rubocop.yml setting is expected to cover
hidden files as well. However, there is a problem that
offenses occur with hidden files.

```console
vendor/bundle/ruby/2.4.0/gems/backports-3.6.8/.irbrc:1:1: C:
Style/SpecialGlobalVars: Prefer $LOAD_PATH over $:.
$:.unshift "./lib"
^^

(snip)
```

This PR solves this problem by including hidden files in
the path match target.

And This PR only changes behavior to hidden files.

As resolved in rubocop#1401, rubocop#1656, hidden directories keep
their previous behavior.
So hidden files in the hidden directory don't match.

Probably it will be an intuitive path match to users.
bbatsov pushed a commit that referenced this issue Aug 16, 2018
Fixes #5181.
Fixes #4832.

This PR changes the path pattern (`*`) to match the hidden file.

This PR resolves the following problem.

```console
AllCops:
  Exclude:
    - 'vendor/bundle/**/*'
```

The above .rubocop.yml setting is expected to cover
hidden files as well. However, there is a problem that
offenses occur with hidden files.

```console
vendor/bundle/ruby/2.4.0/gems/backports-3.6.8/.irbrc:1:1: C:
Style/SpecialGlobalVars: Prefer $LOAD_PATH over $:.
$:.unshift "./lib"
^^

(snip)
```

This PR solves this problem by including hidden files in
the path match target.

And This PR only changes behavior to hidden files.

As resolved in #1401, #1656, hidden directories keep
their previous behavior.
So hidden files in the hidden directory don't match.

Probably it will be an intuitive path match to users.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants