I have a Rails 6 project and am trying to use RuboCop's auto-correct option to fix the many Style/FrozenStringLiteralComment errors (mostly in generated files).
No matter how many times I run bundle exec rubocop --auto-correct --only Style/FrozenStringLiteralComment from the project root, Rubocop will only report the errors, but not auto-correct them:
app/models/product.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
From running with the --show-cops option I can see that this cop 'Supports --auto-correct,' so am not sure what's wrong.
My .rubocop.yml file has only one config setting: require: rubocop-rails.
I've also run bundle exec rubocop -a and it has corrected other errors in config and similar files (single-quoting, mostly).
Any idea why auto-correct won't work in this case? Thanks.
This is a breaking change introduced in the version 0.87 (see the issue).
Quoting from there:
rubocop -a does all autocorrections, including unsafe ones. One has to add --safe-auto-correct to exclude unsafe ones.
You should use the -A flag now to safe and/or unsafe autocorrect your files:
rubocop -a / --autocorrect no longer run unsafe corrections; rubocop -A / --autocorrect-all run both safe and unsafe corrections. Options --safe-autocorrect is deprecated
It should work for you as:
bundle exec rubocop -A --only Style/FrozenStringLiteralComment
Related
A NixOS user, I am studying the Nix Pills. They frequently reference ~/.nix-profile/etc/profile.d/nix.sh as a means of "entering the Nix environment". E.g. quoting from chapter 5:
I remind you how to enter the Nix environment: source
~/.nix-profile/etc/profile.d/nix.sh
However, my NixOS machine does not even have a ~/.nix-profile/etc directory, let alone a ~/.nix-profile/etc/profile.d/nix.sh file. My questions:
Why might my machine not have ~/.nix-profile/etc?
Is there an ordinary way to generate ~/.nix-profile/etc/profile.d/nix.sh?
Is there straightforward alternative way to "enter the Nix environment" in NixOS?
I do not yet have answers to (1) and (2), but do have an answer to (3). That is:
$ nix repl
works to enter the nix environment.
According to the official documentation, profile could be automatically generated when you've switched to it by command:
$ nix-env --switch-profile /nix/var/nix/profiles/default
So, you should try this command and it should create missed files and directories.
Here is the text copied from official doc:
You generally wouldn’t have /nix/var/nix/profiles/some-profile/bin in your PATH. Rather, there is a symlink ~/.nix-profile that points to your current profile. This means that you should put ~/.nix-profile/bin in your PATH (and indeed, that’s what the initialisation script /nix/etc/profile.d/nix.sh does). This makes it easier to switch to a different profile. You can do that using the command nix-env --switch-profile:
$ nix-env --switch-profile /nix/var/nix/profiles/my-profile
$ nix-env --switch-profile /nix/var/nix/profiles/default
These commands switch to the my-profile and default profile, respectively. If the profile doesn’t exist, it will be created automatically. You should be careful about storing a profile in another location than the profiles directory, since otherwise it might not be used as a root of the garbage collector.
I'm upgrading a rails 5.2.4.1 app to rails 6. There are a few config files I want to merge instead of manually adding information from a copy of the old file.
The first time I entered m to merge the file I got the following message.
Please specify merge tool to `THOR_MERGE` env.
I did a search and found this blog post. The folder that this person found after Googling does not exist on my Mac computer.
FileMerge doesn't exist and DiffMerge is very old. I haven't found any information about using one with rails app:update.
What Mac merge tools are currently used that I can set the env var THOR_MERGE to?
Stumbled upon this answer searching for the same thing.
You can launch vscode diff tool by setting the THOR_MERGE env variable as follows:
THOR_MERGE="code -d $1 $2"
This is assuming you have code in your PATH, which you can setup by following the instructions here.
It seems that XCode includes /usr/bin/opendiff, which is a binary that launches FileMerge.app. So I was able to:THOR_MERGE=opendiff rails app:update
RubyMine can also be used as the merge tool. To make that work on my macOS Monterey system, I created a new Bash script at /usr/local/bin/rubymine-merge (based in part on the rubymine script provided by JetBrains Toolbox) with the following code:
#!/usr/bin/env bash
declare -- wait="-W"
bundle exec rubocop --server -A "$1"
open -na "$HOME/Library/Application Support/JetBrains/Toolbox/apps/RubyMine/ch-0/222.3739.56/RubyMine 2022.2 EAP.app/Contents/MacOS/rubymine" $wait --args merge "$2" "$1" "$2"
After this, I was able to invoke the Rails update script as follows:
THOR_MERGE=rubymine-merge bin/rails app:update
This is working as of RubyMine 2022.2.1 in mid-August of 2022.
The Rails update script seems to require that the merge tool behave as if it had received a --wait parameter, because after the last merge the script deletes all temporary files. Passing --wait as a command-line parameter seems problematic with the 2022.2 version of the command-line script, so I hard-coded the -W on line 3.
I also inserted a call to rubocop on line 5 so that the proposed changes from the Rails app:update script would already be aligned with the standards for this project. Skip that part if it's not meeting your needs. I had to use bundle exec rubocop instead of bin/rubocop because the Rails app:update script is not always running in the context of the root directory of your project.
It is suboptimal for this script be separate from the /usr/local/bin/rubymine script that is generated by JetBrains Toolbox. Every time a new version of RubyMine is installed, the path to the actual RubyMine application can change.
Simple Solution:
THOR_MERGE=kdiff3 rails app:update
or use opendiff / meld / kdiff etc.
Credit to #pduey (thanks).
I am experiencing an issue with pre-commit hooks in overcommit, which is configured to run rubocop and rails_best_practices.
In short, all three commands listed below passed but overcommit wouldn't let me commit to git. Any advice on why/how to circumvent this issue would be appreciated.
# These passed
rubocop -a
rails_best_practices .
overcommit --run
# Git commit failed
$ overcommit --run
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] OK
Analyze with RuboCop........................................[RuboCop] OK
✓ All pre-commit hooks passed
$ git commit -m 'Ensure lower case with Attr API'
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:2 - remove unused methods (LowerCaseString#cast)
Analyze with RuboCop........................................[RuboCop] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:3:3: C: Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.
✗ One or more pre-commit hooks failed
Now, I read here that overcommit --run doesn't trigger the hooks in the same ways that it does when you actually try to commit (whole project vs only changes to be committed). However, I'm not sure how this would affect my case, especially since rubocop and rails_best_practices both passed individually as well. By the way, the errors raised here are both false alarms. rails_best_practices shouldn't be checking for unused methods in the first place, since that check has been turned off in config/rails_best_practices.yml.
After a few weeks of working with these gems, I think I've figured it out. I'll leave some pointers here for anyone who encounters similar issues.
1. Stage your changes before doing overcommit --run with git add
<file>
As this comment and docs say, the --run command does not check untracked files.
2. Add gemfile option to .overcommit.yml
One issue I had was that overcommit was failing to read my config/rails_best_practices.yml (it wasn't supposed to be checking for unused methods in the first place, since that check was turned off in the config file).
It seems that when I was trying to git commit, overcommit was using gems installed in my system (not in the Gemfile), and as a result somehow failing to read my config file. So I added the gemfile option as recommended in the docs, and made sure overcommit uses the Gemfile/bundler version. I haven't gotten the subject error ever since. As the docs say:
If you are using Bundler to manage your Ruby gem dependencies, you'll likely want to use the gemfile option to control which gem versions are available during your hook runs.
My .overcommit.yml file for reference:
gemfile: Gemfile
PreCommit:
RuboCop:
enabled: true
on_warn: fail
RailsBestPractices:
enabled: true
on_warn: fail
command: ['bundle', 'exec', 'rails_best_practices', '-c', 'config/rails_best_practices.yml']
As a side note - I haven't tried this, but apparently you can also create a separate Gemfile just for overcommit purposes, if you feel that loading your original Gemfile slows down the hook execution (docs).
The command that I'm trying to run is rails _3.2.13_ new App
When I run it in command line it creates a Rails app with version 3.2.13, but when I run it with system "rails _3.2.13_ new #{self.name} -T -B" it creates a Rails app with the latest version of Rails not 3.2.13 version.
This is a result of Ruby using /bin/sh to execute shell commands, whereas you are probably using /bin/bash in Terminal on a daily basis. The way each is loaded and the specific configurations present in each will alter the configuration.
If you run which rails from both calls to system and in your terminal you'll likely see different paths. Check echo $PATH and you'll likely see different results too.
To resolve the situation, you can check out What's the difference between .bashrc, .bash_profile, and .environment? which will give you a much better understanding of what's going on, then adjust your shell configuration to accomodate.
My Capistrano deployments are hanging on the second command in a composite command.
https://github.com/capistrano/capistrano/blob/5986983915163e6681f2546bf6fad599d58cd024/lib/capistrano/dsl.rb#L59
At the point that SSHKit tries to run the following command:
cd /home/lawfm/deployments/staging/repo && git rev-parse
I get a
bash: git: command not found error.
I believe the issue is documented here:
https://github.com/capistrano/capistrano/issues/675
My deploy was working in 3.0.1, but an upgrade to 3.1 introduced the error. At first, I read that pty was now on by default, so I set:
set :pty, false
Unfortunately, it seems neither .bash_profile nor .profile are being loaded, so any attempt to augment the PATH there have been fruitless.
I can't seem to figure out a way to set the path or even map the git command directly since it's the second command.
I can't figure out a way to fix the path not being applied to the second command, but I did place the PATH definition in my .bashrc file instead of .bash_profile and it now works.