-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Switch require_once to require for loading asset files #18599
Conversation
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.
Given that WPCS' WordPress-Extra
ruleset warns for using include
instead of require
, and that adding WordPress-Extra
to Gutenberg is on the cards (#18502), we should stick with require
here. 🙂
@pento Nevermind, I followed the links. |
The code required for this scenario is just recreating the The ternary
just recreates what the include() function does, see documentation. Though include returns false instead of null, but for the rest of the function it doesn't make a difference. Also this seems to be a much clearer implementation:
So I politely disagree with the coding standard. ;-) |
Those two implementations are different. |
Agreed, it does also throw a warning, but as I stated in the top description this is a feature, because it should warn that files that are supposed to be part of the built Gutenberg don't exist. The previous implementation the build could be broken with no errors or warnings. |
There is also this documentation section: $script_asset = file_exists( $script_asset_path )
? require( $script_asset_path )
: array( 'dependencies' => array(), 'version' => filemtime( $script_path ) ); Should this example be also updated? |
The move from It could be argued that the need to run |
@mcsf It won't blow up though because it checks for the file_exists first, it just silently fails. We should come to a consensus on what we think it should be and update the spots, the Gutenberg examples repo uses the include function: |
Sure (and I don't remember the details leading to checking for file existence first), but it will fail more aggressively in other circumstances: incorrect read permissions on the file, and — the present issue — a duplicated script registration. Nevertheless, I think this question still stands:
|
Yes, the function should not be called twice, but it is for whatever reason. The function can also be coded in a way that if it is called twice it doesn't break. I switched it over since it seems like the consensus is to use |
Yes - I think it speaks to a deeper Core weirdness and/or some sort of doing_it_wrong. But I agree with @mkaz: using So did this issue come up because of some strangeness somewhere else in the stack? Yes. But is this change an improvement regardless of how it came up? I claim yes :) |
Hi @mkaz, does this change needs to be backported into WordPress core? |
@jorgefilipecosta Yes, it probably should be backported to core for the same reason: It's safer to run with the change vs. potential to break without. |
Hi @mkaz, would you be able to submit a core patch with this change? |
WordPress core is all in great shape: 🎉 |
Thanks @gziolo - just crossed off my task list 👍 |
require_once is unsafe for a require that should return a value on each function call. See WordPress/gutenberg#18599
require_once is unsafe for a require that should return a value on each function call. See WordPress/gutenberg#18599
`require_once` is unsafe for a require that should return a value on each function call. See WordPress/gutenberg#18599
Description
If the function gets called twice the dependency array will reset due to the
require_once()
return value. The asset_file is being included to a variable, not loaded like a class.Fixes #18596
I switched to use
include()
because the function is more clear what is happening. Plus will not throw a fatal if the file does not exist, it simply returns false with a warning. This also allows removing the extra check for file existence.Additionally, if the
index.asset.php
file does not exist, then we have a build issue since it is part of the bundle. We shouldn't need to detect if it exists and swallow that error silently. This PR will properly throw a warning as it should with a missing file that is needed.How has this been tested?
Confirmed loading still works as expected.
Function reference: PHP include documentation
Types of changes
Fixes loading of asset_file and dependencies.