#64229 closed enhancement (fixed)
Enqueueing scripts, styles, and script modules should warn when dependencies are missing
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9.1 | Priority: | normal |
| Severity: | normal | Version: | 6.9 |
| Component: | Script Loader | Keywords: | has-patch has-unit-tests |
| Focuses: | javascript, performance | Cc: |
Description (last modified by )
Given the following plugin code:
<?php /** * Plugin Name: Try in enqueuing classic script and script module with missing dependencies */ namespace TryEnqueueingScriptsWithMissingDependencies; add_action( 'wp_enqueue_scripts', static function () { wp_enqueue_script( 'classic-script-with-missing-dependency', 'https://example.com/classic-script.js', array( 'classic-dependency' ) ); wp_enqueue_script_module( 'script-module-with-missing-dependency', 'https://example.com/script-module.js', array( 'module-dependency' ) ); } );
In WordPress 6.8, the classic script is not printed but the script module is. However, as of WordPress 6.9, script modules now behave the same as classic scripts when they have missing dependencies: the script module is not printed. This change was introduced in r60999 to fix #63486. See code in question.
As was extensively troubleshooted in the #core-interactivity-api channel in Slack, this change in behavior was difficult to debug, and it was difficult to identify why a script was not being printed.
Query Monitor actually helpfully warns when attempting to enqueue a classic script script that is missing dependencies, but it doesn't currently do the same for script modules:
This may be due to #60597, where WP_Script_Modules lacks the necessary accessors.
In any case, WordPress core should itself issue a _doing_it_wrong() for both classic scripts and script modules whenever there is a missing dependency for an enqueued script.
Attachments (1)
Change History (36)
This ticket was mentioned in PR #10545 on WordPress/wordpress-develop by @deepakprajapati.
6 months ago
#2
- Keywords has-patch added; needs-patch removed
@westonruter commented on PR #10545:
6 months ago
#3
Let's make sure to add unit tests for these changes as well.
@westonruter commented on PR #10545:
6 months ago
#5
This is looking good to me. I will plan to finish reviewing and commit by next week.
@westonruter commented on PR #10545:
5 months ago
#6
I'm testing this with the following plugin code:
add_action(
'wp_enqueue_scripts',
static function () {
wp_enqueue_script( 'bad-script', '/bad.js', array( 'nope' ) );
wp_enqueue_style( 'bad-style', '/bad.css', array( 'nope' ) );
wp_enqueue_script_module( 'bad-module', '/bad.mjs', array( 'nope' ) );
}
);
I get the following log entries:
- PHP Notice: Function WP_Dependencies::all_deps was called <strong>incorrectly</strong>. The style with the handle bad-style was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Dependencies::all_deps was called <strong>incorrectly</strong>. The script with the handle bad-script was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Script_Modules::sort_item_dependencies was called <strong>incorrectly</strong>. The script module bad-module was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Script_Modules::sort_item_dependencies was called <strong>incorrectly</strong>. The script module bad-module was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Script_Modules::sort_item_dependencies was called <strong>incorrectly</strong>. The script module bad-module was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Dependencies::all_deps was called <strong>incorrectly</strong>. The style with the handle bad-style was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
- PHP Notice: Function WP_Dependencies::all_deps was called <strong>incorrectly</strong>. The script with the handle bad-script was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in /var/www/src/wp-includes/functions.php on line 6131
The duplication of the notices is indeed not great. I think what I'll do is just add a private member variable to the classes to keep track of what has been flagged with _doing_it_wrong(). This will solve the problem of a static variable not being reset with tests.
What's also confusing is the functions being flagged are unrelated to the functions which may have been called incorrectly by the user. So I think I'll update these to use the WP_Scripts::add(), WP_Styles::add(), and WP_Script_Modules::register() methods which are actually flagged as being called incorrectly. This will make much more sense to a developer.
Also, I'll add quotes around the initial handle ID.
@westonruter commented on PR #10545:
5 months ago
#7
There, I think it is more developer-friendly now. When registering a script, style, and script module with missing dependencies, I now get just three notices:
- <b>Notice</b>: Function WP_Styles::add was called <strong>incorrectly</strong>. The style with the handle "bad-style" was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in <b>/var/www/src/wp-includes/functions.php</b> on line <b>6131</b><br />
- <b>Notice</b>: Function WP_Scripts::add was called <strong>incorrectly</strong>. The script with the handle "bad-script" was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in <b>/var/www/src/wp-includes/functions.php</b> on line <b>6131</b><br />
- <b>Notice</b>: Function WP_Script_Modules::register was called <strong>incorrectly</strong>. The script module "bad-module" was enqueued with dependencies that are not registered: nope. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 7.0.0.) in <b>/var/www/src/wp-includes/functions.php</b> on line <b>6131</b><br />
@westonruter commented on PR #10545:
5 months ago
#8
#9
@
5 months ago
- Owner set to westonruter
- Resolution set to fixed
- Status changed from new to closed
In 61323:
#10
@
5 months ago
- Keywords has-unit-tests added; needs-unit-tests removed
- Milestone changed from 7.0 to 6.9.1
- Resolution fixed deleted
- Status changed from closed to reopened
Given the change in behavior in 6.9 where script modules with missing dependencies are no longer printed, it may be that sites will start breaking where previously they worked in 6.8. This was reported as a big in #64342. Given this, I think we should re-milestone the missing dependency notices for 6.9.1.
The docblocks added in [61323] will need to have their @since tags updated to 6.9.1.
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
5 months ago
#15
@
5 months ago
- Summary changed from Enqueueing scripts and script modules should warn when dependencies are missing to Enqueueing scripts, styles, and script modules should warn when dependencies are missing
This ticket was mentioned in Slack in #core by jorbin. View the logs.
4 months ago
This ticket was mentioned in PR #10789 on WordPress/wordpress-develop by @jorbin.
4 months ago
#18
PR to test the backport of https://core.trac.wordpress.org/ticket/64229
One merge conflict resolved when running svn merge -c 61323 '^/trunk'
#20
@
3 months ago
This is a minor point, but please don't hard-code the comma. The type of separator and whether or not there's a space after the separator may differ depending on the locale.
I think we'll need to use the wp_get_list_item_separator() function.
Recommended code example:
protected function get_dependency_warning_message( $handle, $missing_dependency_handles ) {
$comma = wp_get_list_item_separator();
return sprintf(
/* translators: 1: Script handle, 2: List of missing dependency handles. */
__( 'The script with the handle "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
$handle,
implode( $comma, $missing_dependency_handles )
);
}
This ticket was mentioned in Slack in #core by wildworks. View the logs.
3 months ago
This ticket was mentioned in PR #10799 on WordPress/wordpress-develop by @wildworks.
3 months ago
#22
…
Trac ticket: https://core.trac.wordpress.org/ticket/64229
@wildworks commented on PR #10799:
3 months ago
#23
### How to test this PR
Switch the site locale to Japanese and enqueue a script with a non-existent dependency in an active theme. Example:
-
src/wp-content/themes/twentytwentyfive/functions.php
diff --git a/src/wp-content/themes/twentytwentyfive/functions.php b/src/wp-content/themes/twentytwentyfive/functions.php index 8e4acf1e35..b30bdd93a5 100644
a b if ( ! function_exists( 'twentytwentyfive_format_binding' ) ) : 157 157 } 158 158 } 159 159 endif; 160 161 add_action( 'wp_enqueue_scripts', function () { 162 wp_register_script( 163 'test-missing-deps', 164 includes_url( 'js/wp-embed.min.js' ), 165 array( 'non-existent-dependency-1', 'non-existent-dependency-2', 'non-existent-dependency-3' ), 166 null, 167 true 168 ); 169 wp_enqueue_script( 'test-missing-deps' ); 170 } );
Then, open http://localhost:8889/
Make sure the separator is 、 and not , . This character is a separator used in Japanese.
@mukesh27 commented on PR #10799:
3 months ago
#24
Did some quick review and found same error for https://github.com/WordPress/wordpress-develop/pull/10799#issuecomment-3804272398 diff code.
Function WP_Scripts::add was called incorrectly. The script with the handle "test-missing-deps" was enqueued with dependencies that are not registered: non-existent-dependency-1、non-existent-dependency-2、non-existent-dependency-3. (This message was added in version 6.9.1.)
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
3 months ago
This ticket was mentioned in Slack in #core by jorbin. View the logs.
3 months ago
#27
@
3 months ago
- Keywords commit added; fixed-major dev-reviewed removed
Updating tags based on the need for a follow up commit. https://github.com/WordPress/wordpress-develop/pull/10799 looks good for trunk.
#29
@
3 months ago
I've pushed [61542] to https://github.com/WordPress/wordpress-develop/pull/10789 to test the backport to 6.9.
@wildworks commented on PR #10799:
3 months ago
#31
This PR has been committed in https://core.trac.wordpress.org/changeset/61542
@jorbin commented on PR #10789:
3 months ago
#34
Committed in https://core.trac.wordpress.org/changeset/61550

This PR adds developer-facing _doing_it_wrong() notices when a script or script module is enqueued with dependencies that have not been registered.
Currently, WordPress silently skips printing such assets, which makes dependency issues difficult to diagnose. This change improves debugging clarity during development.
What’s changed
✅ Classic scripts (WP_Scripts)
✅ Script modules (WP_Script_Modules)
Both implementations:
Trac ticket: https://core.trac.wordpress.org/ticket/64229