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

rubocops/text: Enforce bin/"formula" instead of "#{bin}/formula" #17826

Merged
merged 4 commits into from
Jul 25, 2024

Conversation

issyl0
Copy link
Member

@issyl0 issyl0 commented Jul 22, 2024

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

@issyl0 issyl0 marked this pull request as draft July 22, 2024 23:17
@issyl0 issyl0 marked this pull request as ready for review July 23, 2024 11:57
@p-linnane
Copy link
Member

When testing locally I'm not able to find any violations despite using --strict.

@issyl0
Copy link
Member Author

issyl0 commented Jul 23, 2024

What command are you running? It worked yesterday on the formula you suggested this be a thing for (with the change reapplied). Maybe I've done something wrong though.

@p-linnane
Copy link
Member

I tried a few formulae, but brew audit --strict ansible is an example. Let me know if I need to change that.

@issyl0
Copy link
Member Author

issyl0 commented Jul 23, 2024

Ohhhh, I see what's happening. You did it right, I did it wrong. There's always a case I don't expect.

@issyl0
Copy link
Member Author

issyl0 commented Jul 23, 2024

So the ansible case is failing because the starts_with matcher only matches "#{bin}/ansible" and not "#{bin}/ansible-lint".

@p-linnane
Copy link
Member

Can we match #{bin}/ in general? Or is there somewhere we really need that interpolation?

@reitermarkus
Copy link
Member

Or is there somewhere we really need that interpolation?

Given we have a Pathname#to_str monkey-patch, I don't think so.

In that case we should also match all other methods that return a Pathname.

- Previously this only included the formula name.
- But, for example in tests, we have "#{bin}/ansible-test",
  not just "#{bin}/ansible". So handle that too.
- I decided to make the error message better by extracting the
  binary name from the interpolation, but I'm not sure it was worth it.

```
$ brew audit --strict ansible
ansible
  * line 580, col 29: Use `bin/"ansible-test"` instead of `"#{bin}/ansible-test"`
Error: 1 problem in 1 formula detected.
```
- I couldn't get
  https://docs.rubocop.org/rubocop-ast/node_pattern.html#param_name-for-named-parameters
  to work like it said it should (bad syntax in the node_matcher, apart
  from with `bin = false` which RuboCop complained about boolean args not
  being named), so here's a workaround.
@issyl0
Copy link
Member Author

issyl0 commented Jul 24, 2024

Right, I made this handle "#{bin}/foo", "#{bin}/foo-bar" etc. I didn't go as far as all of "#{bin}/". If we need "#{bin}/foo_bar" or indeed all of "#{bin}/", or every Pathname, then we can iterate in a follow-up PR?

Copy link
Member

@p-linnane p-linnane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely think iterating is the way to go. Thanks so much!

@Bo98
Copy link
Member

Bo98 commented Jul 25, 2024

Or is there somewhere we really need that interpolation?

Given we have a Pathname#to_str monkey-patch, I don't think so.

Typechecking. Some functions will only take a string and this will break that. Especially where we use standard library functions (e.g. Open3).

The ohai example in the test is already one that probably would be String-only if we typechecked it properly.

IMO if we do this we need to play it safe and only flag known safe functions.

@issyl0 issyl0 merged commit ac97353 into master Jul 25, 2024
24 checks passed
@issyl0 issyl0 deleted the no-interpolated-bin branch July 25, 2024 10:39
@issyl0 issyl0 restored the no-interpolated-bin branch July 25, 2024 10:39
@issyl0 issyl0 deleted the no-interpolated-bin branch July 25, 2024 10:39
issyl0 added a commit that referenced this pull request Jul 25, 2024
…s fine

- This would be weird to change because it's a string not a pathname passed to `shell_output`.
- I had misunderstood #17826 (comment).
issyl0 added a commit that referenced this pull request Jul 25, 2024
- This would be weird to change because it's a string not a pathname passed to `shell_output`.
- I had misunderstood #17826 (comment).
issyl0 added a commit that referenced this pull request Jul 25, 2024
- This would be weird to change because it's a string not a pathname passed to `shell_output`.
- I had misunderstood #17826 (comment).
ctaintor pushed a commit to ctaintor/brew that referenced this pull request Sep 4, 2024
- This would be weird to change because it's a string not a pathname passed to `shell_output`.
- I had misunderstood Homebrew#17826 (comment).
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

Successfully merging this pull request may close these issues.

Make a RuboCop to recommend bin/"foo" over "#{bin}/foo"
4 participants