Service decorates non-existant service when module not installed

Created on 2 July 2022, over 2 years ago
Updated 19 September 2024, 4 months ago

Problem/Motivation

Under certain circumstances when creating a new service with new service decorators you can end up with the error

The service "moduleb.service" has a dependency on a non-existent service "modulea.service".

Presently the only way to solve this problem is to create a Service Provider with the following code:

public function alter(ContainerBuilder $container) {
    if (!$container->hasDefinition('modulea.service')) {
      $container->removeDefinition('moduleb.service');
    }
  }

There are better ways to address this issue. One potential solution is https://www.drupal.org/project/drupal/issues/3111008 ✨ Use native Symfony YamlLoader + Config Needs work because that will allow service.yml files to set decoration_on_invalid to something other than exception (the default value).

This issue is to provide a smaller solution that will hopefully be easier to get passed.

Steps to reproduce

  1. Create a Drupal site. Create and install moduleb.
  2. Create modulea and create a service
  3. Add a service in moduleb that decorates the service from modulea

After those three steps nothing will function.

Proposed resolution

Add support for Symfony's decoration_on_invalid property in Drupal's YamlFileLoader.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 12 hours ago

Created by

πŸ‡¨πŸ‡¦Canada geekygnr Waterloo

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • πŸ‡³πŸ‡±Netherlands kingdutch

    With the other improvements that were made in Drupal this can now be solved with Symfony's decoration_on_invalid tag directly. Since the related issue about replacing YamlFileLoader entirely has hit some speedbumps I think it's worth it to add decoration_on_invalid support to our own YamlFileLoader already.

    We're seeing that the community is adopting "decorate over extend" more and more and this would be a big DX win by allowing a single property in a YAML file to replace an extra PHP class for optional decoration. It also lessens the gap to Symfony for any future replacement of the YamlFileLoader.

  • Status changed to Needs review 5 months ago
  • πŸ‡³πŸ‡±Netherlands kingdutch

    Forgot to update the IS.

  • Pipeline finished with Success
    5 months ago
    Total: 385s
    #273646
  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Would it be possible to get test coverage for the new exceptions

  • Pipeline finished with Failed
    4 months ago
    Total: 167s
    #294670
  • πŸ‡³πŸ‡±Netherlands kingdutch

    Added tests :D

  • Pipeline finished with Success
    4 months ago
    #294677
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    The MR looks great to me. In general I think we should be bringing in improvements from YamlFileLoader to keep parity with Symfony features, and this is a nice and small self-contained step towards that.

  • Does there need to be test coverage that decoration_on_invalid does what it's expected to do for the values "exception" null and "ignore"? Or is it fine because it's directly copied from Symfony?

    Also, do we need a CR documenting decoration_on_invalid can be set and how to use?

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    setDecoratedService() is provided by Symfony, I don't think we need explicit coverage to check that Symfony works? We can rely on their tests to do that.

    If we think a CR is useful then we can add one, but again given this is a Symfony feature then I'm not sure we need it.

  • If we think a CR is useful then we can add one, but again given this is a Symfony feature then I'm not sure we need it.

    Relying on the Symfony tests makes sense.

    I only mention a CR because bringing a Symfony feature like autowiring was documented in a CR β†’ . Bringing in decoration_on_invalid probably isn't as significant as that, but since there is a delta between Drupal features and Symfony features, and there isn't any use yet of decoration_on_invalid in core, it might be useful for there to be documentation that the feature is available in Drupal.

  • πŸ‡³πŸ‡±Netherlands kingdutch

    I think a change record makes sense as per #21.

    Moving to "Needs Review" for the CR to be reviewed and approved :) Implementation is unchanged and was approved in #18.

  • CR looks good. I also edited the issue title, mostly for a misspelling. Back to RTBC.

    • larowlan β†’ committed c0053e63 on 11.x
      Issue #3293926 by kingdutch, geekygnr, godotislate, m4olivei, longwave:...
  • First commit to issue fork.
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Committed to 11.x

    I think this qualifies as backport eligible to 10.4 under 'Critical API compatibility issues'

    I've created an MR to run tests

  • Pipeline finished with Success
    3 months ago
    Total: 528s
    #318860
  • πŸ‡³πŸ‡±Netherlands kingdutch

    Should this be backported to 10.4.0? I'm not entirely sure what upgrade paths are supported, can site builders go from 10.4.x to 11.0.x or are they expected to jump from 10.4.x to 11.1.x?

    Contrib code that would use this would be compatible with ^10.4 || ^11.1 only, if we backport (i.e. would break on 11.0 since it's introduced as feature in 11.1).

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Yes I thought so, but wanted a green MR first

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Committed to 10.4.x - thanks

    • larowlan β†’ committed a64662a3 on 10.4.x
      Issue #3293926 by kingdutch, geekygnr, godotislate, m4olivei, longwave:...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024