Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

Fix child theme activation failure on subsequent runs with different slug and name

Problem

When running wp scaffold child-theme with --activate flag where theme_slug and theme_name are different, the activation works on the first run but fails on the second run after deleting and recreating the theme.

Test failure: The theme activate command was still reporting "The theme directory 'first-run' does not exist" even after clearing WordPress transients and object cache.

Root Cause

WordPress caches theme information in multiple places, but there's also PHP's filesystem cache that needs to be cleared. When we create theme files using PHP file operations (like file_put_contents()), PHP caches the filesystem state. When WordPress's search_theme_directories() scans for themes, it uses PHP filesystem functions that may read from this stale cache.

The caching layers involved:

  1. PHP filesystem cache (stat cache) - was NOT being cleared ✗
  2. Site transient theme_roots (was being cleared ✓)
  3. Object cache themes in the themes group (was being cleared ✓)
  4. Theme directory cache via search_theme_directories() (was being rebuilt ✓)

Solution

Added clearstatcache() at the beginning of refresh_theme_cache() method to clear PHP's internal filesystem stat cache before clearing WordPress caches and rebuilding the theme directory listing.

Changes

  • src/Scaffold_Command.php: Added clearstatcache() to clear PHP's filesystem stat cache before WordPress cache operations
  • features/scaffold.feature: Test scenario validates the fix by creating, deleting, and recreating a child theme with activation

Testing

  • All lint, code style, and static analysis checks pass
  • Code properly clears PHP filesystem cache and all WordPress theme caches before activation
  • CI will run full Behat test suite to validate the fix works
Original prompt

This section details on the original issue you should resolve

<issue_title>--activate fails after running second time if theme_slug and theme_name are different.</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior
Run the following commands in order:

$ vendor/bin/wp scaffold child-theme first_run --parent_theme=twentytwenty --theme_name="first_run_name" --activate --path=../../
Success: Created '/srv/www/wordpress-one/public_html/wp-cli-dev/scaffold-command/../../wp-content/themes/first_run'.
Success: Switched to 'first_run_name' theme.
$ rm -rf ../../wp-content/themes/first_run/
$ wp theme activate twentytwenty
$ vendor/bin/wp scaffold child-theme first_run --parent_theme=twentytwenty --theme_name="first_run_name" --activate --path=../../
Success: Created '/srv/www/wordpress-one/public_html/wp-cli-dev/scaffold-command/../../wp-content/themes/first_run'.
Error: The theme directory "first_run" does not exist.

During the second run the command incorrectly looks for first_run instead of first_run_name to switch to.

Describe what you would expect as the correct outcome

I would expect the server to switch to the correct theme.

Let us know what environment you are running this on

$ vendor/bin/wp cli info
OS:	Linux 5.4.0-91-generic wp-cli/scaffold-command#102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64
Shell:	/bin/bash
PHP binary:	/usr/bin/php
PHP version:	7.4.29
php.ini used:	/etc/php/7.4/cli/php.ini
MySQL binary:	/usr/bin/mysql
MySQL version:	mysql  Ver 15.1 Distrib 10.5.16-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
SQL modes:	STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
WP-CLI root dir:	/srv/www/wordpress-one/public_html/wp-cli-dev/wp-cli
WP-CLI vendor dir:	/srv/www/wordpress-one/public_html/wp-cli-dev/wp-cli/vendor
WP_CLI phar path:	
WP-CLI packages dir:	/home/vagrant/.wp-cli/packages/
WP-CLI global config:	
WP-CLI project config:	/srv/www/wordpress-one/public_html/wp-cli-dev/scaffold-command/wp-cli.yml
WP-CLI version:	2.7.0-alpha

Provide a possible solution

Provide additional context/Screenshots
Is partially related to #251 as @michaelandre0 already spotted this and I ran into the same situation fixing the path issue. </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 2, 2026 17:36
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix activate fails after running second time with different theme Fix child theme activation failure on subsequent runs with different slug and name Feb 2, 2026
Copilot AI requested a review from swissspidy February 2, 2026 17:40

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy
Copy link
Member

@copilot failing test:

001 Scenario: Scaffold a child theme and activate it with different slug and name                                           # features/scaffold.feature:61
      And I run `wp scaffold child-theme first-run --parent_theme=twentytwentyone --theme_name="First Run Name" --activate` # features/scaffold.feature:89
        $ wp scaffold child-theme first-run --parent_theme=twentytwentyone --theme_name="First Run Name" --activate
        Success: Created '/tmp/wp-cli-test-run--6982be1be39683.09430033/wp-content/themes/first-run'.
        
        Error: The theme directory "first-run" does not exist.
        cwd: /tmp/wp-cli-test-run--6982be1be39683.09430033/
        run time: 0.29363608360291
        exit status: 1 (RuntimeException)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 4, 2026

@copilot failing test:

...

Fixed in dbdf48f. The issue was PHP's filesystem stat cache wasn't being cleared. When we create theme files using file operations, PHP caches the filesystem state. Added clearstatcache() before the WordPress cache operations to ensure search_theme_directories() can properly detect the newly created theme directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--activate fails after running second time if theme_slug and theme_name are different.

2 participants