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

docs: updates the node formula example to use the node installed by Homebrew #17539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctaintor
Copy link

The existing example uses a symlink which means that whatever node is first in the PATH will be used. This can mean that a Homebrew-installed nodejs executable may not work reliably (since it may execute with a different version of node)

This PR changes it so that using the Homebrew-installed node will be used. It also includes common other options (like shell completion) as well as the old symlink solution.

  • 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?

@ctaintor ctaintor changed the title (Docs) Updates the node formula example to use the installed node version docs: Updates the node formula example to use the installed node version Jun 20, 2024
@ctaintor ctaintor changed the title docs: Updates the node formula example to use the installed node version docs: updates the node formula example to use the node installed by Homebrew Jun 20, 2024
Comment on lines +114 to +117

# Uncomment if you want to write the completion scripts for bash, fish, and zsh (assuming
# your executable has a command "completion" which returns a completion script)
# generate_completions_from_executable(libexec/"bin/foo", "completion", base_name: 'foo')
Copy link
Member

Choose a reason for hiding this comment

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

How many formulae does this?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure but personally when I was writing a formula I didn't know that this was an option (adding completions) and I also did not know that the generate_completions_from_executable function exists. I kept these comments here to help people know that adding completions is easy.

(In my case, my CLI already supported completions – so it was as simple as knowing that it's possible and easy to have Homebrew set it up)

Copy link
Member

Choose a reason for hiding this comment

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

Looks like 387 formulae use generate_completions_from_executable in total.

However, out of the 173 formulae that have require "language/node", only 10 use generate_completions_from_executable.

Of those 10, only cdktf uses the generate_completions_from_executable(libexec/"bin/foo") scheme described here, the other 9 use generate_completions_from_executable(bin/"foo").

Copy link
Author

Choose a reason for hiding this comment

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

Of those 10, only cdktf uses the generate_completions_from_executable(libexec/"bin/foo") scheme described here, the other 9 use generate_completions_from_executable(bin/"foo")

Note that when you use write_env_script then you must use libexec here (using bin will result in a runtime error)

Copy link
Member

Choose a reason for hiding this comment

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

Given this is used by <10% of Node formulae: I'm not sure it's worth documenting specifically here.

Comment on lines +106 to +113
# "link" the executable `foo`, ensuring that the version of node installed by Homebrew is used
# (the created `foo` will set the ENV before exec'ing your executable)
env = { PATH: "#{HOMEBREW_PREFIX/"bin"}:$PATH" }
(bin/"foo").write_env_script "#{libexec}/bin/foo", env

# Uncomment if you simply want to symlink the executable – note that this means the first
# `node` on the PATH will be used (not necessarily the one Homebrew installed)
# bin.install_symlink Dir["#{libexec}/bin/*"]
Copy link
Member

Choose a reason for hiding this comment

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

Which/how many formulae do this each of these ways? CC @homebrew/core for thoughts.

Copy link
Author

@ctaintor ctaintor Jun 26, 2024

Choose a reason for hiding this comment

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

I personally don't know when you'd ever want it this way – it's just how it was documented previously. At the very least, if the formula has a depends_on to node then I'd expect it to always use the write_env_script to force the usage of the Homebrew-installed node.

yarn will use the first node on the path but I'd imagine this is an outlier (and it is also not calling bin.install_symlink – but it is not trying to always use homebrew's node)

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the 173 formulae with require "language/node":

5 use write_env_script, although only 3 use it in the exact way suggested here:

  • protoc-gen-grpc-web
    • This doesn't actually use system "npm", "install", *Language::Node.std_npm_install_args(libexec) so maybe it shouldn't count
  • mongosh
  • code-server
  • bcoin
  • emscripten
    • This is another case where the formula is much more complex than the sample here os it probably shouldn't be included

On the other hand, there are 155 formulae that use bin.install_symlink and 145 of those use bin.install_symlink Dir[libexec/"foo"]. So, the original way this is written seems to be by far the most prevalent.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @Rylan12!

On the other hand, there are 155 formulae that use bin.install_symlink and 145 of those use bin.install_symlink Dir[libexec/"foo"]. So, the original way this is written seems to be by far the most prevalent.

Given the prevalence here: I think this may be worth opening a Homebrew/core issue to discuss if we want to migrate the approach here. I don't think it makes sense to have the documentation recommend the opposite of what most formulae are doing.

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.

None yet

3 participants