-
Notifications
You must be signed in to change notification settings - Fork 115
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
Fail Rubocop on warn logging #11458
Fail Rubocop on warn logging #11458
Conversation
changelog: Internal, Automated Testing, Fail static analysis linting when warning messages emitted
I think this is failing as expected: https://gitlab.login.gov/lg/identity-idp/-/jobs/1513614 Though it also includes an additional warning I wasn't expecting about
It might be a result of calling Alternatively, could look at raising an exception, or curious if there's some equivalent of Node.js's |
scripts/strict_kernel_warn.rb
Outdated
@@ -0,0 +1,12 @@ | |||
# frozen_string_literal: true | |||
|
|||
module Kernel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I might document this with a comment? Big change in behavior from typical Ruby
- This seems like a
lib/core_ext
kinda file to me, I would move it there? Since lib is not autoloaded it should be safe
module Kernel | |
# Module that overrides Kernel#warn to immediately exit | |
# Should NOT be included in normal web server mode, is only used in specific scripts | |
module Kernel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good point on both counts. I'll make those updates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather we not do this. This is an extreme change to Ruby's default behavior.
Instead, can we:
- Disable cache so that we always get the warning output and consume that output to change the exit code OR
- Write a cop to detect this specific issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lmgeorge What do you think of the updated approach? I think it's an improvement. I hadn't realized that these warnings were being emitted to stderr, so it becomes a test of "fail if there's any stderr".
Example build failure: https://gitlab.login.gov/lg/identity-idp/-/jobs/1515745
Example output:
--- rubocop ---
mkdir -p tmp
bundle exec rubocop --parallel --format progress --format junit --out rubocop.xml --display-only-failed --color 2> tmp/rubocop.txt
Inspecting 2259 files
.......
2259 files inspected, no offenses detected
[ -s tmp/rubocop.txt ] && (printf "Error: Unexpected stderr output from Rubocop\n"; cat tmp/rubocop.txt; exit 1)
Error: Unexpected stderr output from Rubocop
lib/identity_config.rb: Metrics/LineLength has the wrong namespace - replace it with Layout/LineLength
lib/identity_config.rb: Metrics/LineLength has the wrong namespace - replace it with Layout/LineLength
make: *** [Makefile:71: lint] Error 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sort of lean towards @lmgeorge's concerns on this in that the change feels pretty heavy (though limited in scope) for the upside. The only alternative I have is to grep the output for the message we don't expect to see. It's not quite the same though. I don't intend for this to be a blocking suggestion since anything going sideways on this will hopefully be pretty apparent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchellhenke I've since changed from the initial approach in recent commits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aduth I can agree with this approach of grepping the stderr output.
My main concern is that it can be easy to see an existing pattern and replicate it elsewhere even when strong language in the form of doc comments warn (heh) against it. I think this new approach addresses that concern. Thank you for being willing to flex on this!
scripts/strict_kernel_warn.rb
Outdated
@@ -0,0 +1,12 @@ | |||
# frozen_string_literal: true | |||
|
|||
module Kernel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather we not do this. This is an extreme change to Ruby's default behavior.
Instead, can we:
- Disable cache so that we always get the warning output and consume that output to change the exit code OR
- Write a cop to detect this specific issue.
This reverts commit 8ecba37.
Doesn't exist by default at this point in CI
This reverts commit 1ff4931.
Previous didn't seem to work in CI, always exited even with empty file?
I think I'm seeing a difference in how this behaves in CI. Basically, I have a job that is failing the I added the rubocop.txt file to the gitlab artifacts and downloaded it to verify that it is present. I think there might be a difference in how the |
@matthinz Interesting, yeah. I think the Maybe it's because it doesn't actually get far enough to the Maybe we can tolerate the non-0 status code result , like throwing a |
Continuing issue with |
🛠 Summary of changes
Updates Rubocop to fail the build if there are any warning messages emitted.
Rubocop may warn when there's incorrect usage, but it doesn't trigger a failure. The changes here override
Kernel#warn
in the context of Rubocop linting to exit with a non-zero exit code.📜 Testing Plan
Prior to fix, verify that the build fails, including an error message. (Edit: See https://gitlab.login.gov/lg/identity-idp/-/jobs/1513614)
Example: