Problem/Motivation
Steps to reproduce:
Create a new theme. In the .info.yml file include this line but don't declare any libraries underneath it or append []
.
libraries:
When installing your theme or clearing caches you should see the following:
Warning: Invalid argument supplied for foreach() in Drupal\Core\Extension\ThemeHandler->addTheme() (line 203 of core/lib/Drupal/Core/Extension/ThemeHandler.php).
Proposed resolution
Either ignore that libraries is not an array at this point, or throw a meaningful exception. Either would improve the experience.
Since the following keys in theme .info.yml are allowed to be empty without throwing errors we have landed on ignoring the key without a value:
ckeditor_stylesheets
quickedit_stylesheets
libraries-extend
libraries-override
Remaining tasks
Patch
Tests
Review
User interface changes
n/a
API changes
n/a
Data model changes
n/a
Comment | File | Size | Author |
---|---|---|---|
#45 | empty-libraries-in-theme-2594937-45.patch | 2.3 KB | Anonymous (not verified) |
#41 | empty-libraries-in-theme-2594937-40.patch | 1.75 KB | chgasparoto |
#41 | empty-libraries-in-theme-2594937-38-diff.patch | 792 bytes | chgasparoto |
#38 | empty-libraries-in-theme-2594937-38.patch | 2.32 KB | Peacog |
#34 | interdiff-2594937-28-34.txt | 1.4 KB | Peacog |
Comments
Comment #2
cilefen CreditAttribution: cilefen commentedComment #3
star-szrThanks for the patch @cilefen!
It seems like the following keys don't give any kind of error when they are left empty:
ckeditor_stylesheets
quickedit_stylesheets
libraries-extend
libraries-override
So maybe we should just do the same for a more consistent and friendly experience rather than throwing an exception. This came up in real world usage commenting out a lone library in the .info.yml file for testing purposes.
Where we should throw a friendlier exception IMO is for keys like 'regions:', where when present you are actually overriding something. (That should be a separate issue.)
Comment #4
cilefen CreditAttribution: cilefen commentedSo, maybe quietly make it an array if it isn't one then.
Comment #5
star-szrOr what libraries-override and libraries-extend do is just wrap in an
!empty
check:Comment #6
cilefen CreditAttribution: cilefen commentedComment #7
Wim LeersComment #8
star-szrGood call, thanks @Wim Leers!
I guess this needs work for tests at this point.
Comment #9
anchal29 CreditAttribution: anchal29 as a volunteer commentedWhat exactly that test should do? If i'm going right then it should make sure that $info['libraries'] and themes->library are empty? or something else need to be done?
Comment #10
claudiu.cristeaComment #11
raulbordallo@gmail.com CreditAttribution: raulbordallo@gmail.com at ASPgems commentedI working on it.
Comment #12
raulbordallo@gmail.com CreditAttribution: raulbordallo@gmail.com at ASPgems commentedThis patch #6 work for me.
Comment #13
raulbordallo@gmail.com CreditAttribution: raulbordallo@gmail.com at ASPgems commentedComment #14
betarobot CreditAttribution: betarobot commented#6 worked fine for me too.
Comment #15
claudiu.cristeaComment #16
alexpottStill needs tests.
Comment #17
aerozeppelin CreditAttribution: aerozeppelin commentedTests for #6.
Comment #18
gvso\No newline at end of file patch warning
Shouldn't we add a better description of the exception?
Comment #19
alvar0hurtad0This patch try to solve #18
Comment #20
gvso#19 looks good to me.
Comment #21
joyceg CreditAttribution: joyceg commented#19 works fine. It was applied successfuly.
Comment #22
joyceg CreditAttribution: joyceg commentedComment #23
cilefen CreditAttribution: cilefen commented@joyceg: Cool, but we need a little more in a review. Did you test with a bad theme file and observe the proper error? Did you execute the test on its own to prove it works? We don't know.
On a side note, please do not post images of console output. It is enough to say that the patch applies. We all know what that means.
The final release of 8.0.x is out so this is moved to 8.1.x.
Comment #24
star-szrThis is looking really good, happy to see this still active :)
I think this would more accurately be "Tests empty libraries in theme .info.yml files."
Making it into a complete sentence ending in a period, normalized capitalization and changing the theme.info.yml.
This could be lowercase W - "No warnings" otherwise the capitalization is a bit odd.
Comment #25
Sagar Ramgade CreditAttribution: Sagar Ramgade as a volunteer and at Trigyn Technologies Ltd commentedHere it goes, patch attached addresses #24.
Comment #26
martin107 CreditAttribution: martin107 commentedFixes everything from #24, triggering testbot.
Comment #28
iStryker CreditAttribution: iStryker commented@Sagar Ramgade you forgot a period.
Reroll patch for testing
Comment #29
star-szrAn interdiff would be great here, at a glance looks OK but I always encourage interdiffs.
Comment #30
Sagar Ramgade CreditAttribution: Sagar Ramgade as a volunteer and at Trigyn Technologies Ltd commented@Cottsert, interdiff attached which includes suggestion by @iStryker.
Comment #32
almaudoh CreditAttribution: almaudoh commentedBack to NR. Fail was due to an interdiff posted as a patch.
Comment #33
Wim LeersThe test method name does not reflect what it is testing.
The test asserts true is true, instead of using the pass() method.
The assertion messages in the test don't make sense: they call exceptions "warnings" and don't mention what this is about: the libraries key.
The actual change looks fine :)
Comment #34
Peacog CreditAttribution: Peacog as a volunteer commentedHere's a patch that addresses @Wim Leers suggestions, and adds back the test .info.yml file that fell out of the patch a few rolls back.
ThemeHandler test cases are based on PHPUnit, which doesn't have a pass() method, so I've kept the assertTrue().
"Warnings" is correct. The bug manifests as PHP warnings in the DB log.
Comment #36
Peacog CreditAttribution: Peacog as a volunteer commentedThere was a problem with adding the test .info.yml file. Trying again...
Comment #38
Peacog CreditAttribution: Peacog as a volunteer commentedAnd again...
Comment #39
Peacog CreditAttribution: Peacog as a volunteer commentedComment #40
sebas5384 CreditAttribution: sebas5384 as a volunteer commentedHey! we are reviewing this issue at a code sprint from DrupalCamp Campinas 2016 Brazil.
http://campinas2016.drupalcamp.com.br
Comment #41
chgasparoto CreditAttribution: chgasparoto as a volunteer and at CI&T commentedWe reviewed the patch and worked. @sebas5384 @feraline @racarvalho1987
After run the coder we found a variable that was not being used and we removed it.
It's ready to be commited.
Thank you!
Comment #42
chgasparoto CreditAttribution: chgasparoto as a volunteer and at CI&T commentedComment #45
Anonymous (not verified) CreditAttribution: Anonymous as a volunteer and at CI&T commentedSorry we have upload the wrong patch file.
Comment #46
sebas5384 CreditAttribution: sebas5384 as a volunteer commentedComment #47
erickbj CreditAttribution: erickbj at CI&T commentedReviewed patch at #45 and it works, marking as RTBC.
Comment #48
Wim LeersThe
in the IS says:So this patch went with the second option.
Is that really desirable though? It means it's okay for a key without values to exist in a theme's
*.info.yml
file.I think this change needs to be carefully considered. One could argue for either option. I think this compiles with https://en.wikipedia.org/wiki/Robustness_principle, so I'm personally fine with it (even though it makes me twitch as a developer who favors strictness).
Comment #50
Wim LeersRandom fails in
Drupal\field\Tests\Update\FieldUpdateTest
. Re-testing and restoring previous status.Comment #52
cilefen CreditAttribution: cilefen commentedSame as #50
Comment #54
Peacog CreditAttribution: Peacog as a volunteer commentedThe same random failures are happening elsewhere too, so the errors seem to be unrelated to this patch specifically. See #2749955: Random fails in UpdatePathTestBase tests
Comment #55
star-szr@Wim Leers thanks for the thoughtful input! I think the strictness ship has sailed since most of the other keys allow for a key without values as I pointed out in #3 (added that info to the issue summary). Not sure those have coverage but they worked at least at the time of that writing.
+1 to RTBC from me.
Comment #56
Wim Leers#55: that is then fully consistent behavior. So, no more concerns then! :)
Comment #57
cilefen CreditAttribution: cilefen commentedComment #58
almaudoh CreditAttribution: almaudoh commentedThis is already RTBC, so maybe this is a late comment, but considering that this seems to share the same characteristics with #2730807: WSOD on admin/modules if description is set but is NULL in module.info.yml and #2558645: Malformed module.info.yml prevents install with a confusing error, we should look for a more robust and generic solution. Like using sane defaults for when any of the required entries are missing from a .info.yml file, or better yet, displaying a more helpful message for the admin to react to, rather than the WSOD.
Comment #59
xjmSo from a release management perspective, making this key interpret empty as empty, as it were, is mostly non-disruptive, and consistent with the behavior of some other keys. We also have @Cottser's signoff for the theme system.
However, in general, I too would prefer to fail meaningfully rather than silently. I also agree with @almaudoh's feedback above. The disadvantage of removing the exception here to silently accept an empty value is that we cannot then make it throw an explicit error again in a minor release. So, for example, if we went for a holistic solution for this and #2730807: WSOD on admin/modules if description is set but is NULL in module.info.yml and the like, we would just provide a BC fallback for the keys that do already allow empty values, but this issue would move us away from failing in a way that communicates something.
On the other hand, from a DX perspective, it is actually pretty convenient to just allow an empty template that you fill in, especially for such basic functionality as defining a new project and especially for the theme developer audience. From @Cottser:
To me, that is a compelling reason to allow empty non-required keys in
.info.yml
files in general. The only risk is that you don't realize you left it blank, but that seems a reasonable risk to me because if it's blank, and you go to the source and it seems blank, then that explains why it's blank. :)That would also point to initializing the optional keys to array or emptystring or whatever at a higher level, rather than going through every place one of these keys is defined and adding a babysitting
if (!empty()))
check, but that is something that would work as a followup.I've argued with myself back and forth a lot about whether to just commit this patch, which is probably a sign that we should indeed get that framework manager review. :) I do think we should at least move it to 8.2.x only. I'll reach out to the framework managers.
Thanks everyone!
Comment #60
xjm(Saving credit.)
Comment #62
xjmAdding @racarvalho1987 per #41.
Comment #63
alexpottI think this change is fine - the most important thing is that it behaves predictably - and I don't think this changes that. If you have an empty libraries key in your theme you don't expect it to break or load any libraries. If you have a broken library declared in the libraries key this doesn't affect that either so I'm +1 from a framework manager perspective.
Comment #64
alexpottCommitted 2fb3a05 and pushed to 8.2.x. Thanks!
Comment #66
alexpottI think we should open a follow to address this in a little bit more of generic way... So both modules and themes have a default array to merge to the information in .info.yml - see _system_rebuild_module_data() and \Drupal\Core\Extension\ThemeHandler::rebuildThemeData(). I think it is worth considering that if the yaml value is NULL we should use the default. That would fix both #2730807: WSOD on admin/modules if description is set but is NULL in module.info.yml and #2594937: Empty 'libraries:' in theme .info.yml file produces confusing PHP warning