php / php/php-src

FPM vs CLI - getcwd() !== $_SERVER['DOCUMENT_ROOT'] and include_path

オープン
#19,584 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Feature SAPI: fpm
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

I'm not yet sure whether this is a bug or an improvement in documentation suffices.

$_SERVER['DOCUMENT_ROOT'] is set by the FPM SAPI (therefore also in INPUT_SERVER 'DOCUMENT_ROOT') and should generally be equal to nginx's root ($document_root) path

However, there is also getcwd() which, unless set in www.conf chdir and modified in https://github.com/php/php-src/blame/master/sapi/fpm/fpm/fpm_unix.c#L457 which should be identical to DOCUMENT_ROOT.

However it is not.
e.g.
root /foo/bar

request: /hello/world.php
which is located in /foo/bar/hello/world.php

DOCUMENT_ROOT will be /foo/bar
but getcwd() will be /foo/bar/hello (essentially dirname( $_SERVER['SCRIPT_FILENAME'] ) )

I'm unable to find where this change of directory happens though - since it's not in https://github.com/php/php-src/blame/master/sapi/fpm/fpm/fpm_unix.c#L457 and there are no other chdir happening.
Could anyone pinpoint this?

php-fpm 5.2 (?) seemed to have

https://linux.die.net/man/8/php-fpm
-C
Do not chdir to the script's directory

which was removed though (I couldn't find details on why though)

This generally is not an issue, however it might be extremely unexpected when using exec() for example.
Would it not make more sense if by default the fpm SAPI would use the DOCUMENT_ROOT by default as cwd?

Where it becomes confusing though is:
https://www.php.net/manual/en/ini.core.php#ini.include-path

Using a . in the include path allows for relative includes as it means the current directory.

"current directory" means DIR and not getcwd() ?

Except in CLI:
https://www.php.net/manual/en/features.commandline.differences.php

The CLI SAPI does not change the current directory to the directory of the executed script.

where it means getcwd() and not DIR ?

Would it make sense to somehow standardize eventually?

Sorry, if this reads confusing, but so am I atm

PHP Version
8.4
Operating System

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、参照されている行の周辺にある sapi/fpm/fpm/fpm_unix.c と www.conf の chdir 設定を確認し、次にリンク先のドキュメントに記載されている FPM と CLI の動作を比較します。作業の完了とは、作業ディレクトリがどこで変更されるのかを特定し、その動作または include-path のドキュメントに明確化が必要かどうかを判断することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
nginx, php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。