-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix block type registration bugs on Windows by correctly normalizing paths #8417
base: trunk
Are you sure you want to change the base?
Fix block type registration bugs on Windows by correctly normalizing paths #8417
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a real user test of this to confirm that the assumptions in the PHPUnit test are correct, but this looks right to me.
*/ | ||
private static function find_collection_path( $file_or_folder ) { | ||
if ( empty( $file_or_folder ) ) { | ||
return null; | ||
} | ||
|
||
// Check the last matched collection first, since block registration usually happens in batches per plugin or theme. | ||
$path = wp_normalize_path( rtrim( $file_or_folder, '/' ) ); | ||
$path = rtrim( $file_or_folder, '/' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, because normalization is happening in the public method ::get_metadata()
before passing the value to this and the also private ::default_identifier_callback()
methods. Thanks for clarifying in the docs for both ✨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was a quick follow-up. It all looks good to me. I don't have a Windows machine to verify, but the included tests look fine to me.
For
WP_Block_Metadata_Registry
, the approach taken is that all public methods have their inputs normalized, while the private methods expect already normalized paths (which has been included in the documentation and should be fine since those methods are private to the class).
I remember that in WordPress 6.4 there was a new function introduced get_block_asset_url
as a performant way to calculate the URL for the block's asset (JS or CSS). Example usage:
wordpress-develop/src/wp-includes/blocks.php
Lines 238 to 239 in b45e379
$script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) ); | |
$script_uri = get_block_asset_url( $script_path_norm ); |
It operates on the normalized URLs so that would be similar to how the private methods in the class are handled. It makes sense to me in this particular case that public methods related to blocks at this level of abstraction take any path and normalize everything internally.
Out of curiosity, do you know why in the get_block_asset_url
there is realpath
used in a few places, example:
wordpress-develop/src/wp-includes/blocks.php
Lines 95 to 99 in b45e379
// Path needs to be normalized to work in Windows env. | |
static $wpinc_path_norm = ''; | |
if ( ! $wpinc_path_norm ) { | |
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); | |
} |
Based on git blame, you may be the best person to answer this 😂 cc @audrasjb Based on that ticket, that same bug would currently still apply to |
Intriguing discovery 🙈😃 It looks like this particular issue existed when using symlinks:
In addition, there were mentions in https://core.trac.wordpress.org/ticket/62140 of prior limitations with block metadata collections when using symlinks. However, this was likely addressed in https://core.trac.wordpress.org/changeset/59730. |
The thing is I'm not sure whether that problem is relevant here or not. Of course, we could just go the safe route and include Should we maybe just add them to be safe? Or should we commit this as is and see if any other bug reports come up in the future? |
Let's proceed as is and collect more details before adding additional function calls. |
wp_normalize_path()
correctly throughout block type registration.WP_Block_Metadata_Registry
, the approach taken is that all public methods have their inputs normalized, while the private methods expect already normalized paths (which has been included in the documentation and should be fine since those methods are private to the class).Trac ticket: https://core.trac.wordpress.org/ticket/63027
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.