Skip to content

Fatal error during upgrading to Symfony 7.3 and rebuilding container: Uncaught Error: First array member is not a valid class name or object #60585

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
robiningelbrecht opened this issue May 29, 2025 · 8 comments

Comments

@robiningelbrecht
Copy link

robiningelbrecht commented May 29, 2025

Symfony version(s) affected

7.3.0

Description

When running composer update symfony/* to upgrade from 7.2 to 7.3, all packages are downloaded and installed. When composer tries to clear the Symfony cache and the container is rebuilt, the following error is thrown:

Image

Line 799 contains:

 return $container->privates['App\\Infrastructure\\ValueObject\\Measurement\\UnitSystem'] = [NULL, 'from']($container->get("app.config")->get("appearance.unit_system", ));

The error is thrown due to the first value in the array NULL. The reason for NULL being there is that my service is defined as:

    App\Infrastructure\ValueObject\Measurement\UnitSystem:
        factory: [ null, from ]
        arguments: ['@=app_config("appearance.unit_system")']

How to reproduce

  • Add an entry in services.yaml with a factory method on the class itself:
    App\Namespace\To\Class
        factory: [ null, yourFactoryMethod ]
        arguments: ['your-argument']
  • update composer.json and edit all Symfony packages from 7.2.* to 7.3.*
  • Run composer update symfony/*

Possible Solution

No response

Additional Context

The Symfony docs state that this approach should be possible: https://symfony.com/doc/current/service_container/factories.html#using-the-class-as-factory-itself

@HypeMC
Copy link
Member

HypeMC commented May 30, 2025

The main issue here is that UnitSystem is an enum. In #60095, the container.excluded tag was added to all enums, so the ResolveFactoryClassPass no longer resolves the class:

if ($v instanceof Definition && $v->hasTag('container.excluded')) {
continue;
}

Based on the discussion there, this was intentional, so I'm not sure it qualifies as a bug, even though it is technically a BC break.

@robiningelbrecht
Copy link
Author

It makes sense that this does not work anymore if it has explicitly been removed. But I'd expect that minor upgrades do not introduce these kind of breaking changes?

@ro0NL
Copy link
Contributor

ro0NL commented Jun 2, 2025

No need to support this, it'd be broken design :) (https://github.com/symfony/symfony/pull/60095/files#r2021037158)

so it's expected yes.

@robiningelbrecht
Copy link
Author

robiningelbrecht commented Jun 2, 2025

It's indeed removed but

  1. No clue why it was decided to introduce a breaking change between minor versions
  2. Why it was decided that "it'd be broken by design"
  3. Why this breaking change is not mentioned in the docs

Anyway, worked my way around this by referencing the FQCN instead of null on enum factory methods

@nicolas-grekas
Copy link
Member

As a workaround you can disable autoconfiguration for that definition:

    App\Infrastructure\ValueObject\Measurement\UnitSystem:
        autoconfigure: false
        factory: [ null, from ]
        arguments: ['@=app_config("appearance.unit_system")']

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 2, 2025

This is how it works by design indeed. I get the BC aspect of this. Changing the behavior of the app is something that's inherent to autoconfiguration. Of course, we're careful not to break anything as much as possible, but I see little alternatives here.

We could find a way to track explicitly-defined services and never auto-exclude them. But that introduces an exception to the rules of autoconfiguration so this might be hard to justify on the long run.

We might also make a special case for enums only, where the autoconfiguration rule is replaced by some similar logic in the FileLoader that does the auto-discovery.

Dunno if any of these options make sense.

I'd be tempted to close as "won't fix" unless we gather more feedback on the topic. (who else is using an enum class as a service? please let us know if this is common enough practive.)

@robiningelbrecht
Copy link
Author

robiningelbrecht commented Jun 2, 2025

For me this issue can be closed, the autoconfigure: false approach is a valid alternative

My use case for using enums as a service is a self-hosted app running in a docker container. Users can configure this app by means of several yaml files. A lot of the configurable options are a pre-defined list of strings, hence the usage of enums.
I then inject the configured values (enums) in any service where I need them

@nicolas-grekas nicolas-grekas closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2025
@erik-treehouse
Copy link

I see the issue is already closed. But to give some insight in this, we apply a string enum as a service (with the same factory pattern as described above) as a better alternative to injecting a raw string value. That enum also has a few convenience methods so we don't have to do string comparison everywhere.

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

7 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy