beyondcode / herd-community

89 stars 1 forks source link

[Bug]: Config value is null, causing type error on array_key_exists #1028

Closed CamKem closed 1 month ago

CamKem commented 1 month ago

Platform

macOS

Operating system version

macOS Sonoma 14.5 (23F79)

System architecture

ARM64 (M1, M2, etc)

Herd Version

1.11.0

PHP Version

PHP 8.3.11

Bug description

I have noticed several times I am getting an error that null is being passed into array_key_exists() in the HerdConfiguration causing the request to die before hitting the Laravel app. It's happening maybe once a week or something.

Looking under the hood it seems like its not parsing the config values.

As its not throwing an exception here: throw new Exception("Herd configuration file not found at {$path}");

So I assume that it's probably related to here:

if (array_key_exists('HERD_CONFIG_TEST', $_SERVER) && array_key_exists('HERD_CONFIG_JSON', $_SERVER) && $_SERVER['HERD_CONFIG_TEST'] === true) {
    return self::fromJson($_SERVER['HERD_CONFIG_JSON']);
}

I guess its possible that the 'HERD_CONFIG_JSON key is not being correctly set somewhere else in the application, as you can see in the stack trace it's trying to pass an empty string to fill, which means that the $config = json_decode($json, true); is parsed as null.

Maybe a further conditional ! empty($_SERVER['HERD_CONFIG_JSON'])

Really hard to say without knowing more about the underlying architecture of Herd, so I'll leave it to your team for further debugging.

Steps to reproduce

I haven't been able to find any action that is producing the result consistently.

Relevant log output

[25-Sep-2024 16:40:27 Australia/Melbourne] PHP Fatal error:  Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /Applications/Herd.app/Contents/Resources/valet/HerdConfiguration.php:124
Stack trace:
#0 /Applications/Herd.app/Contents/Resources/valet/HerdConfiguration.php(124): array_key_exists('dumps', NULL)
#1 /Applications/Herd.app/Contents/Resources/valet/HerdConfiguration.php(54): BeyondCode\HerdConfiguration\HerdConfiguration->fill('')
#2 phar:///Applications/Herd.app/Contents/Resources/valet/dump.phar/src/HerdInjector.php(24): BeyondCode\HerdConfiguration\HerdConfiguration::load()
#3 phar:///Applications/Herd.app/Contents/Resources/valet/dump.phar/src/HerdInjector.php(47): Herd\HerdDumper\HerdInjector->getConfiguration()
#4 phar:///Applications/Herd.app/Contents/Resources/valet/dump.phar/src/HerdInjector.php(32): Herd\HerdDumper\HerdInjector->canInject()
#5 /Applications/Herd.app/Contents/Resources/valet/cli/Valet/Drivers/LaravelValetDriver.php(29): Herd\HerdDumper\HerdInjector->inject()
#6 /Applications/Herd.app/Contents/Resources/valet/server.php(91): Valet\Drivers\LaravelValetDriver->beforeLoading('/Users/cam/Site...', 'pinkary-repo', '/@NewPerson')
#7 {main}
  thrown in /Applications/Herd.app/Contents/Resources/valet/HerdConfiguration.php on line 124
mpociot commented 1 month ago

@CamKem thank you for reporting this.

It seems like your herd configuration file is empty, as the fill method gets called with an empty string. Do you have a way to reproduce this issue?

We are looking for the configuration file at this location:

$_SERVER['HOME'] . '/Library/Application Support/Herd/config/herd.json';

Could it be that the HOME variable is modified in your nginx configuration or something similar? I assume that you have an existing herd.json config file, right?

CamKem commented 1 month ago

Hey Marcel, Yeah I certainly have the herd config in the default location, I haven't played around with anything other than to do a little looking around re: this issue.

Screenshot 2024-09-25 at 5 07 47 PM

I opened the file just to confirm that it wasn't empty & it's certainly got values.

Screenshot 2024-09-25 at 7 08 49 PM

I haven't touched the herd.conf or nginx.conf in the nginx directory, heres the home settings.

Screenshot 2024-09-25 at 7 12 10 PM

That was why I assumed it was related to the super globals, because I knew the herd.json was present where it was expected to be & the file contents were as you would expect them to be.

The only server configuration changes on this machine are to the default macos apache install, httpd.conf, where I have set the server root the following settings, for serving sites on my local network.

DocumentRoot "/Users/cam/Sites/"
<Directory "/Users/cam/Sites">

I doubt that would have any effect on config values...

mpociot commented 1 month ago

@CamKem this will be fixed in the next release

mpociot commented 1 month ago

This is now fixed in Herd 1.11.1