Aliases of private services are public

Created on 22 March 2024, 3 months ago
Updated 12 June 2024, 13 days ago

Problem/Motivation

core.services.yml marks form_cache as a private service:

  form_cache:
    class: Drupal\Core\Form\FormCache
    arguments: [...]
    public: false  # Private to form_builder
  Drupal\Core\Form\FormCacheInterface: '@form_cache'

form_cache is private, but the Drupal\Core\Form\FormCacheInterface alias is public:

> \Drupal::service('form_cache');

   Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException  You have requested a non-existent service "form_cache". Did you mean this: "views.exposed_form_cache"?

> \Drupal::service('Drupal\Core\Form\FormCacheInterface');
= Drupal\Core\Form\FormCache {#9939}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 2 hours ago

Created by

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

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @longwave
  • Status changed to Needs review 3 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    MR reuses the visibility of the aliased service, unsure if this is the correct approach but seems to work.

    > \Drupal::service('Drupal\Core\Form\FormCacheInterface');
    
       Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException  You have requested a non-existent service "Drupal\Core\Form\FormCacheInterface".
    
    > unserialize(Drupal::service("kernel")->getCachedContainerDefinition()['services']['form_builder']);
    = [
        "class" => "Drupal\Core\Form\FormBuilder",
        "arguments" => {#7337
          +"type": "collection",
          +"value": [
            {#7341
              +"type": "service",
              +"id": "form_validator",
              +"invalidBehavior": 1,
            },
            {#7339
              +"type": "service",
              +"id": "form_submitter",
              +"invalidBehavior": 1,
            },
            {#7338
              +"type": "private_service",
              +"id": "private__G8Uu4hTcUgjMeXKACab9jV6Lz0_L84M9F590Ayb57E0",
              +"value": [
                "class" => "Drupal\Core\Form\FormCache",
                "public" => false,
                "arguments" => {#7336
    ...
    
  • Status changed to Needs work 3 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Some test failures to sort out.

  • Status changed to Needs review 3 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Fixed some tests. backend_overridable backend services must follow the visibility of the service being overridden. JSON:API had a public alias to a private service in a test, which I think was relying on this bug; worked around this by adding another compiler pass to the test to make the service public again.

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

    Alternatively maybe we should just explicitly copy the visibility of the original alias in autowired service aliases? AutowireTest could be extended to check for this.

  • πŸ‡¬πŸ‡§United Kingdom catch

    One very small nit on the MR, but overall the approach seems good to me, as you say a good first step to private-by-default services.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    @longwave don't mind marking if you can take a look at the one thread.

  • Status changed to RTBC 14 days ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Had some time so went ahead and applied the change mentioned in the MR.

  • Pipeline finished with Success
    14 days ago
    Total: 721s
    #196621
  • Status changed to Needs work 14 days ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    It feels like we're missing test coverage of the heart of this change - the bit where we copy the visibility from the service definition to the alias.

  • Status changed to Needs review 13 days ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Added test coverage.

  • Pipeline finished with Success
    13 days ago
    Total: 577s
    #197183
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Done, let's see what the tests say. Also reapplied @catch's nit which I inadvertently removed when rebasing earlier.

  • Pipeline finished with Failed
    13 days ago
    Total: 589s
    #197432
  • Status changed to Needs work 13 days ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Well, that breaks the tests that use backend overridden services..

Production build 0.69.0 2024