Skip to content

Disable SDK from autoloading during composer execution. - #1727

Merged
brettmc merged 4 commits into
open-telemetry:mainfrom
ChrisLightfootWild:composer/psr3-compat
Jan 15, 2026
Merged

brettmc merged 4 commits into
open-telemetry:mainfrom
ChrisLightfootWild:composer/psr3-compat

Conversation

@ChrisLightfootWild

Copy link
Copy Markdown
Contributor

Proposal to prevent the SDK from auto-loading during composer script execution.

During a composer script, the packaged dependencies in the composer.phar are currently locked to PSR-3 v2 and thus any local application dependencies which expect v3 can trigger a fatal error.

By preventing the SdkAutoloader from being auto-loaded, we can hopefully avoid the fatal error from triggering.

Fixes #1673.


To test, we can perform the following actions:

make update PHP_VERSION=8.3

make bash PHP_VERSION=8.3

OTEL_PHP_AUTOLOAD_ENABLED=true composer run-script test-psr3

Before:

> OpenTelemetry\Tests\Integration\Composer\Psr3CompatabilityTest::run

Fatal error: Declaration of Psr\Log\AbstractLogger::emergency(Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::emergency($message, array $context = []) in /usr/src/myapp/vendor/psr/log/src/LoggerTrait.php on line 18
echo $?
255

After:

> OpenTelemetry\Tests\Integration\Composer\Psr3CompatabilityTest::run
echo $?
0

@ChrisLightfootWild
ChrisLightfootWild requested a review from a team as a code owner October 10, 2025 15:26
@codecov

codecov Bot commented Oct 10, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.29%. Comparing base (ddb222d) to head (b3771cb).
⚠️ Report is 50 commits behind head on main.

Files with missing lines Patch % Lines
src/SDK/Common/Util/ComposerHandler.php 0.00% 4 Missing ⚠️
src/SDK/_autoload.php 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1727      +/-   ##
============================================
- Coverage     68.37%   68.29%   -0.08%     
- Complexity     2971     2978       +7     
============================================
  Files           448      450       +2     
  Lines          9020     8731     -289     
============================================
- Hits           6167     5963     -204     
+ Misses         2853     2768      -85     
Flag Coverage Δ
8.1 68.07% <0.00%> (+0.01%) ⬆️
8.2 ?
8.3 ?
8.4 ?
8.5 67.44% <0.00%> (-0.76%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/SDK/_autoload.php 0.00% <0.00%> (ø)
src/SDK/Common/Util/ComposerHandler.php 0.00% <0.00%> (ø)

... and 222 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ddb222d...b3771cb. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/SDK/_autoload.php Resolved
Comment thread src/SDK/_autoload.php Outdated
*
* @see https://github.com/open-telemetry/opentelemetry-php/issues/1673
*/
if (getenv('COMPOSER_DEV_MODE') === false) {

@Nevay Nevay Oct 17, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (getenv('COMPOSER_DEV_MODE') === false) {
if (getenv('COMPOSER_BINARY') === false) {

COMPOSER_DEV_MODE is only set by the run-script command, running scripts directly still fails:

OTEL_PHP_AUTOLOAD_ENABLED=true composer test-psr3

Edit: checking solely for COMPOSER_BINARY will be too strict as this environment variable is also propagated to @php scripts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Nevay Ah, that is unfortunate - thanks for highlighting. Realistically, there may be no safe way to do this then? 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not start with COMPOSER_DEV_MODE, since that covers some failure cases? It could be broken out into its own class+function such as ComposerHandler::isRunning() to allow for future expansion of methods to identify whether composer is running?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've made the suggested change to move to a ComposerHandler::isRunning() check.

I have also added an additional rudimentary check to try to infer whether or not the composer executable has been called directly.

This does fix the problem with running this way:

OTEL_PHP_AUTOLOAD_ENABLED=true composer test-psr3

Are there any foreseen problems in doing these additional checks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also noted a discussion started here which links to this issue.

@bobstrecansky

Copy link
Copy Markdown
Contributor

@Nevay / @brettmc - can y'all take a look at this when you get a moment?

Comment thread src/SDK/Common/Util/ComposerHandler.php Outdated

if (
($entrypoint = $_SERVER['argv'][0] ?? '') === getenv('COMPOSER_BINARY')
|| in_array(basename($entrypoint), ['composer', 'composer.phar'], true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking class_exists(\Composer\Console\Application::class, false) is likely the most reliable way to detect whether we are running as composer command.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I hadn't seen this comment until today (it is timestamped as 2 weeks ago though?! 🤔)

Would you prefer switching to the class_exists check?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It sounds like a good check to me, yes please @ChrisLightfootWild

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@brettmc apologies for the huge delay here - just revisited this PR and made the suggested change.

@brettmc
brettmc merged commit bfef968 into open-telemetry:main Jan 15, 2026
5 of 9 checks passed
@ChrisLightfootWild
ChrisLightfootWild deleted the composer/psr3-compat branch January 15, 2026 11:23
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.

Psr\Log\AbstractLogger Fatal Error

4 participants