Skip to content

[Process] defaultEnv not properly generated in PHP build-in webserver #44066

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

Closed
lukas-staab opened this issue Nov 16, 2021 · 4 comments
Closed

Comments

@lukas-staab
Copy link

lukas-staab commented Nov 16, 2021

Symfony version(s) affected

process 5.3.7

Description

The php-doc says

* @param array|null $env The environment variables or null to use the same environment as the current PHP process

With $env = null it will take the Enviroment of the current process. This is not allways true.
Using the PHP Build-In Webserver (php -S localhost:80) $_ENV is empty (but getenv() is not), so
foreach ($_ENV as $k => $v) {

gives unexpected result. Especially the PATH variable remains unset, which leads to unfound binaries

How to reproduce

Use php build in webserver (see above) and use e.g

$p = new Process(['pdflatex'], __DIR__, null)

(set $env = null)
-> PATH Variable is not set in $_ENV only in getenv()

Possible Solution

Use getEnv() instead of $_ENV or check for

if (php_sapi_name() === 'cli-server') {

Additional Context

php -S is (often) used in local development environments. Missing PATH Variables can be a big issue there.

@lukas-staab
Copy link
Author

lukas-staab commented Nov 16, 2021

This might be the cause of the issue of the empty $_ENV. Quote from (my) default /etc/php.ini (php 8.0 on Fedora 35)

; This directive determines which super global arrays are registered when PHP
; starts up. G,P,C,E & S are abbreviations for the following respective super
; globals: GET, POST, COOKIE, ENV and SERVER. There is a performance penalty
; paid for the registration of these arrays and because ENV is not as commonly
; used as the others, ENV is not recommended on productions servers. You
; can still get access to the environment variables through getenv() should you
; need to.
; Default Value: "EGPCS"
; Development Value: "GPCS"
; Production Value: "GPCS";
; http://php.net/variables-order
variables_order = "GPCS"

So this might even effect other tools, not only the build-in webserver. For me php -f worked, where the build-in webserver did not with the same files, due to unpopulated $_ENV (E)

@lukas-staab
Copy link
Author

Thanks for the quick commit f608f0b @nicolas-grekas but I do not think this solves the issue. e.g. 'PATH' is neither in $_SERVER nor in $_ENV for me, only in getenv() (Server is not empty, as ENV is, but does not hold the PATH variable by [my] default)

@nicolas-grekas
Copy link
Member

Is that a guess or did you give it a try? To me it should fix the issue... :)

@lukas-staab
Copy link
Author

I reimplemented it, you are right, it works like a charm. I did not expect that the intersection is empty. Thanks for the quick response!

nicolas-grekas added a commit that referenced this issue Nov 16, 2021
… (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Process] intersect with getenv() to populate default envs

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44066
| License       | MIT
| Doc PR        | -

Commits
-------

f608f0b [Process] intersect with getenv() to populate default envs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants