Skip to content

HookConvertRector writes a services.yml for themes (not supported by DrupalKernel) #385

Description

@bbrala

Companion to GitLab issue #3600784 (reported by tobiasb, Drupal 10, drupal-rector dev-main).

Problem

When HookConvertRector converts a theme's hooks to OOP hook classes, it still generates a mytheme.services.yml. Themes don't load service providers (\Drupal\Core\DrupalKernel::discoverServiceProviders() ignores them), so the generated config is non-functional. For themes the src/Hook/<Class>.php (auto-discovered via the #[Hook] attribute + namespace) is enough; no services.yml should be written.

Root cause

.theme files became eligible for conversion in #325 ("Add theme extension"), but __destruct() writes the services.yml for any extension, guarded only against the core path — there's no theme-vs-module distinction:

// src/Rector/Convert/HookConvertRector.php  (~lines 236-238)
if (!str_starts_with($this->moduleDir, $this->drupalCorePath)) {
    static::writeServicesYml("$this->moduleDir/$this->module.services.yml", "$namespace\\$className");
}

Proposed fix

Track whether the extension being processed is a theme (the .theme extension is already known at HookConvertRector.php:142, or read the info.yml type:), and skip the writeServicesYml() call for themes. Add a theme fixture asserting the hook class is written but no services.yml is produced.

Related: #3481215, #3596308, #2002606.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions