crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.42k stars 1.62k forks source link

Tries to use pkg-config from wrong location after a home-brew install #12066

Open mig-hub opened 2 years ago

mig-hub commented 2 years ago

Hello,

I am actually not sure if it is a crystal problem or if it is due to the formulae or home-brew itself. I have submitted a bug on home-brew-core as well just to be sure.

Basically I have installed with a typical brew install crystal on a Mac with a M1 ship. My install is the regular homebrew with everything under "/opt/homebrew". After the install, I cannot use the crystal command, even crystal --version because it raises an error. Here it is:

Unhandled exception: Unable to get file info: '/usr/local/bin/pkg-config': Permission denied (File::AccessDeniedError)
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'raise<File::Error+>:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Crystal::System::File::info?<String, Bool>:(Crystal::System::FileInfo | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Process::is_executable_file?<Path>:Bool'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Process::find_executable<String>:(String | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in '~Crystal::Program::PKG_CONFIG_PATH:init'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Crystal::OnceState#once<Pointer(Bool), Pointer(Void)>:(Pointer(Bool) | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'main'
Unhandled exception: Unable to get file info: '/usr/local/bin/pkg-config': Permission denied (File::AccessDeniedError)
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'raise<File::Error+>:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Crystal::System::File::info?<String, Bool>:(Crystal::System::FileInfo | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Process::is_executable_file?<Path>:Bool'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Process::find_executable<String>:(String | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in '~Crystal::Program::PKG_CONFIG_PATH:init'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'Crystal::OnceState#once<Pointer(Bool), Pointer(Void)>:(Pointer(Bool) | Nil)'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.4.1/libexec/crystal in 'main'

I do not understand the error message per se, but it seems to expect pkg-config at "/usr/local/bin/pkg-config". However my pkg-config is at "/opt/homebrew/bin/pkg-config" like everything installed by homebrew. And this is the only version I have because when I run which -a pkg-config, this is the only version in the output.

It is worth noting that shards --version works fine and outputs "Shards 0.17.0 (2022-03-24)".

Does anybody know how to correct this?

Thank you in advance, Mickael

mig-hub commented 2 years ago

Further info:

I managed to make it work but there is still something going on.

After discussing on the homebrew thread I realised that the cause was because I had "/usr/local/bin" in my PATH for legacy reasons. But this directory was completely empty. So for some reason down the chain, the location for pkg-config was picked only based on the existence of this directory, not on the existence of the software itself which is in another path in $PATH. I have now removed this path and it works fine, but an empty or non-existing directory in $PATH should not cause this sort of issue. It should keep looking in other paths until it finds it, the way the which command works.

straight-shoota commented 2 years ago

File::AccessDeniedError looks like the path actually exists but is not readable. Otherwise there would not be an error because missing files are ignored in File.info?.

The function stat errors with EACCESS which indicates that search permission is denied for a component of the path prefix.

So this seems to be a specific condition of your system. I don't think it's related to darwin or aarch64 in particular.

But it this is surely a bug in Process.find_executable. It should ignore such errors and continue looking for an executable file.