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

brew edit <formula> open the formula executable in editor #14895

Closed
3 tasks done
axiac opened this issue Mar 5, 2023 · 6 comments
Closed
3 tasks done

brew edit <formula> open the formula executable in editor #14895

axiac opened this issue Mar 5, 2023 · 6 comments
Labels
bug Reproducible Homebrew/brew bug outdated PR was locked due to age

Comments

@axiac
Copy link
Contributor

axiac commented Mar 5, 2023

brew doctor output

Your system is ready to brew.

Verification

  • My "brew doctor output" above says Your system is ready to brew. and am still able to reproduce my issue.
  • I ran brew update twice and am still able to reproduce my issue.
  • This issue's title and/or description do not reference a single formula e.g. brew install wget. If they do, open an issue at https://github.com/Homebrew/homebrew-core/issues/new/choose instead.

brew config output

HOMEBREW_VERSION: 4.0.4-202-gb1ef41c
ORIGIN: https://github.com/Homebrew/brew
HEAD: b1ef41c25d12795dc93975e337e8644d0974b53c
Last commit: 3 hours ago
Core tap origin: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 833b95fbfdef6b3c79fda599c7a1c5949cad6775
Core tap last commit: 39 minutes ago
Core tap branch: master
Core tap JSON: 05 Mar 18:52 UTC
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_DISPLAY: :0
HOMEBREW_EDITOR: vi
HOMEBREW_GITHUB_API_TOKEN: set
HOMEBREW_MAKE_JOBS: 16
Homebrew Ruby: 2.6.10 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: 16-core 64-bit kabylake
Clang: 14.0.0 build 1400
Git: 2.39.2 => /usr/local/bin/git
Curl: 7.86.0 => /usr/bin/curl
macOS: 13.2-x86_64
CLT: 14.2.0.0.1.1668646533
Xcode: N/A

What were you trying to do (and why)?

I ran brew edit vault to see some details about the formula definition. Homebrew opened the binary executable vault in my editor (vi).

Context: After I upgraded Homebrew to version 4, a couple of weeks ago, I untapped homebrew/core.
I understand that the formula definition is not available locally any more and consequently cannot be edited. But opening the executable in vi does not look like a good idea.

What happened (include all command output)?

vi started and opened a binary file, probably /usr/local/bin/vault.
When I exited vi, the output of brew on terminal was:

$ brew edit vault
Editing vault

I re-tapped homebrew/core and tried again. Nothing changed; brew edit vault still opens the vault executable.

However, for some formulae, brew edit works as expected and opens the formula definition file in editor (vi).
Example "good" formulae: awscli, php, [email protected], nvm, bash-completion@2, postgresql@14 etc.

$ brew edit awscli
Editing /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/awscli.rb

For other formulae, it opens the binary executable: vault, mysql, jq, redis etc.

Note: All the formulae listed above are installed. I think that brew edit correctly opens the formula definition file for formulae that are not installed (or, at least, I tried about a dozen and could not find any problem.)

I uninstalled jq and then brew edit jq worked fine. I installed jq again and brew edit jq reverted to opening the binary executable.

It could be related to the macOS version or Ruby version. I tried on two other MBPs running macOS 12 (Monterey) and I could not reproduce the behaviour.
This is the output on one of them:

$ brew edit jq
Editing /usr/local/opt/jq/.brew/jq.rb

Please remark that it opens the formula definition cached next to the executable, not the one from the Homebrew installation directory.

The output of brew config on the Monterey system (the one that works fine) is below. I did not re-tap homebrew/core on it because brew edit works fine.

HOMEBREW_VERSION: 4.0.4-202-gb1ef41c
ORIGIN: https://github.com/Homebrew/brew
HEAD: b1ef41c25d12795dc93975e337e8644d0974b53c
Last commit: 4 hours ago
Core tap JSON: 05 Mar 19:45 UTC
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CASK_OPTS: []
HOMEBREW_DISPLAY: :0
HOMEBREW_EDITOR: vi
HOMEBREW_GITHUB_API_TOKEN: set
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.10 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit haswell
Clang: 14.0.0 build 1400
Git: 2.39.2 => /usr/local/bin/git
Curl: 7.79.1 => /usr/bin/curl
macOS: 12.6.3-x86_64
CLT: 14.2.0.0.1.1668646533
Xcode: N/A

What did you expect to happen?

If the tap homebrew/core is available locally, I expect that Homebrew starts my editor (vi) to edit the file /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/vault.rb.

If the tap homebrew/core is not available locally, it can show an error message and (preferably) directions about how to get the formula definition (brew tap etc.)

Step-by-step reproduction instructions (by running brew commands)

# No formula definition locally => opens binary executable
brew edit vault

# Re-tap => still opens the executable
brew tap homebrew/core
brew edit vault

# Uninstall formula => opens the formula definition
brew uninstall vault
brew edit vault

# Install again => opens the executable again
brew install vault
brew edit vault
@axiac axiac added the bug Reproducible Homebrew/brew bug label Mar 5, 2023
@MikeMcQuaid
Copy link
Member

Thanks for the issue!

It could be related to the macOS version or Ruby version. I tried on two other MBPs running macOS 12 (Monterey) and I could not reproduce the behaviour.

Makes me think it may be related to the current working directory you are in. Can you reproduce if you do this?

Step-by-step reproduction instructions

Do these reproduce on all machines?

@MikeMcQuaid MikeMcQuaid added the help wanted We want help addressing this label Mar 6, 2023
@axiac
Copy link
Contributor Author

axiac commented Mar 7, 2023

Makes me think it may be related to the current working directory you are in. Can you reproduce if you do this?

Indeed, the current directory is the problem. On the computer where I detected the issue I ran the brew edit commands in /usr/local/bin. On the computers where the problem could not be reproduced, the current directory was a subdirectory of my home directory.
The formulae whose definition files are opened correctly have the executable name different than the formula name, i.e. a file with the name I put in the command line does not exist in the current directory.

I think that I can provide reproducible step-by-step instructions.
If a file or directory named foo exists in the current directory, the command brew edit foo opens the file or directory named foo in the editor.

# Make sure that a file named `foo` does not already exist in the current directory (to not destroy it)
# If it exists then pick another name (or skip the next step)
ls -l foo

# Create the file `foo`
echo "123" > ./foo

# Run buggy `brew edit`
brew edit foo

# Confirm that the new file `foo` containing one line of text ("123") is open in the editor
# Close the editor, `brew` has displayed "Editing foo" on `stdout`

@apainintheneck
Copy link
Contributor

Essentially we use the NamedArgs#to_path method to evaluate the params. It checks first to see if the arg is a valid path before trying to get the package path by other means.

# Keep existing paths and try to convert others to tap, formula or cask paths.
# If a cask and formula with the same name exist, includes both their paths
# unless `only` is specified.
sig { params(only: T.nilable(Symbol), recurse_tap: T::Boolean).returns(T::Array[Pathname]) }
def to_paths(only: parent&.only_formula_or_cask, recurse_tap: false)
@to_paths ||= {}
@to_paths[only] ||= downcased_unique_named.flat_map do |name|
if File.exist?(name)
Pathname(name)
elsif name.count("/") == 1 && !name.start_with?("./", "/")
tap = Tap.fetch(name)
if recurse_tap
next tap.formula_files if only == :formula
next tap.cask_files if only == :cask
end
tap.path
else
next Formulary.path(name) if only == :formula
next Cask::CaskLoader.path(name) if only == :cask
formula_path = Formulary.path(name)
cask_path = Cask::CaskLoader.path(name)
paths = []
paths << formula_path if formula_path.exist?
paths << cask_path if cask_path.exist?
paths.empty? ? Pathname(name) : paths
end
end.uniq.freeze
end

I'm not sure I understand why it'd be super useful to be able to load from a path in this command. In fact, looking at the other commands where this same method is used I feel the same way about them too. We already had to deprecate this behavior in the brew audit command a little while ago because it lead to unexpected results.

It's used in the following commands:

  • brew audit
  • brew bump-unversioned-casks
  • brew cat
  • brew edit
  • brew formula
  • brew style

Of those brew style is probably the only one that really needs it to allow you to run the linter on individual files.

@apainintheneck
Copy link
Contributor

We'd probably have to go through the whole deprecation cycle to remove the ability to pass paths to these commands (other than brew style of course). In the meantime, we could just evaluate the arg as a path last instead of trying to do that first which would most likely have the desired effect. It also would be less likely to break anyone's workflow with the current state of these commands.

@MikeMcQuaid MikeMcQuaid removed the help wanted We want help addressing this label Mar 9, 2023
@MikeMcQuaid
Copy link
Member

Thanks for jogging my memory here @apainintheneck.

Yes, annoyingly this is desired behaviour by some users/commands and not other users/commands. I know this is used heavily for e.g. brew edit in the current working directory by people. In this case therefore the workaround is just: don't do this in HOMEBREW_PREFIX/bin, sorry!

@MikeMcQuaid MikeMcQuaid closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2023
@apainintheneck
Copy link
Contributor

I don't really understand what the use case is here. If you already know the path, you can just open it in your editor with $EDITOR file_path. The brew edit command essentially does that for you internally already but doesn't expect you to know the path to the package, just the name.

@github-actions github-actions bot added the outdated PR was locked due to age label Apr 10, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Reproducible Homebrew/brew bug outdated PR was locked due to age
Projects
None yet
Development

No branches or pull requests

3 participants