Skip to content

Fix child-theme/plugin path check when --path contains relative segments#385

Closed
armorbreak001 wants to merge 1 commit intowp-cli:mainfrom
armorbreak001:fix/child-theme-path-canonicalization
Closed

Fix child-theme/plugin path check when --path contains relative segments#385
armorbreak001 wants to merge 1 commit intowp-cli:mainfrom
armorbreak001:fix/child-theme-path-canonicalization

Conversation

@armorbreak001
Copy link
Copy Markdown

Problem

Using wp scaffold child-theme with a --path argument that contains relative path segments (e.g. --path=../mywpsite/) fails with:

Error: Invalid theme slug specified. The target directory '.../../mywpsite/wp-content/themes/slug' is not in '.../../mywpsite/wp-content/themes'.

The same issue affects scaffold plugin and other subcommands that use check_target_directory().

Root Cause

In check_target_directory(), the `` is canonicalized (resolving .. and `.` segments) before comparison, but the reference paths (`WP_CONTENT_DIR . '/themes'` and `WP_PLUGIN_DIR`) are not canonicalized. When `--path` contains relative segments, `WP_CONTENT_DIR` retains them unnormalized, causing the string comparison to fail even though both paths refer to the same directory.

Fix

Apply canonicalize_path() to both sides of the comparison in check_target_directory() so paths are resolved consistently. This is a 2-line change.

Fixes #251

When --path includes .. or . segments (e.g. --path=../mysite/),
WP_CONTENT_DIR retains the unnormalized path, but check_target_directory()
canonicalizes the target_dir before comparing. This mismatch causes
false rejection of valid theme/plugin directories.

Apply canonicalize_path() to both sides of the comparison so paths
with relative segments are resolved consistently.
@armorbreak001 armorbreak001 requested a review from a team as a code owner April 21, 2026 08:41
@github-actions
Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@github-actions github-actions Bot added bug command:scaffold-child-theme Related to 'scaffold child-theme' command command:scaffold-plugin Related to 'scaffold plugin' command scope:testing Related to testing labels Apr 21, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the path comparison logic in src/Scaffold_Command.php by applying canonicalize_path to WordPress directory constants, ensuring consistency with the target directory's format. A review comment suggests refactoring this logic to reduce duplication and improve robustness against edge cases in path strings.

Comment thread src/Scaffold_Command.php
Comment on lines +989 to 995
if ( 'theme' === $type && self::canonicalize_path( str_replace( '\\', '/', WP_CONTENT_DIR . '/themes' ) ) !== $parent_dir ) {
return sprintf( 'The target directory \'%1$s\' is not in \'%2$s\'.', $target_dir, WP_CONTENT_DIR . '/themes' );
}

if ( 'plugin' === $type && str_replace( '\\', '/', WP_PLUGIN_DIR ) !== $parent_dir ) {
if ( 'plugin' === $type && self::canonicalize_path( str_replace( '\\', '/', WP_PLUGIN_DIR ) ) !== $parent_dir ) {
return sprintf( 'The target directory \'%1$s\' is not in \'%2$s\'.', $target_dir, WP_PLUGIN_DIR );
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The path comparison logic is duplicated and potentially fragile if the WordPress constants contain trailing slashes or double slashes, as canonicalize_path() does not collapse them. Refactoring this to use a single normalized check improves maintainability and robustness. Using dirname() on the reference path (after appending /.) ensures it is normalized consistently with $parent_dir.

		$check_dir = 'theme' === $type ? WP_CONTENT_DIR . '/themes' : WP_PLUGIN_DIR;
		if ( in_array( $type, [ 'theme', 'plugin' ], true ) && dirname( self::canonicalize_path( str_replace( '\\', '/', $check_dir . '/.' ) ) ) !== $parent_dir ) {
			return sprintf( 'The target directory \'%1$s\' is not in \'%2$s\'.', $target_dir, $check_dir );
		}

@swissspidy
Copy link
Copy Markdown
Member

Looks like you opened this just after I assigned it to Copilot 😅 see #384

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy
Copy link
Copy Markdown
Member

Closing in favor of #384 which contains the exact same changes, but also tests. Thanks for your PR though!

@swissspidy swissspidy closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:scaffold-child-theme Related to 'scaffold child-theme' command command:scaffold-plugin Related to 'scaffold plugin' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

child-theme throws an error if the --path argument is set

2 participants