Problem/Motivation
Seeing changes in your Twig templates is difficult without specific Drupal knowledge.
Here is my go to configuration when theme building in Drupal:
Turn on Twig debug in sites/default/services.yml:
twig.config:
debug: true
auto_reload: null
cache: true
Disable page, dynamic_page, render cache so I get fresh data as I edit Twig templates:
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['page'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
Rebuild Drupal cache. Launch Laravel Mix with Browsersync, and go!
The problem is that this is fairly clunky and very backend orientated.
Proposed resolution
A walkthrough on the new form is below. This was approved by UX maintainer @ckrina (any changes will be captured in followup issues).

Additional notes
When enabled, this will output an error on the status report page:

Remaining tasks
- #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase
Build it!- Get tests to pass
Follow-up issues
#3278887: Update development.services.yml to include twig debug by default
A new “Development settings” page at /admin/config/development/settings that contains Twig development settings, as well as the ability to disable various caches. The settings are stored within the state table (as opposed to configuration), so the settings cannot be accidentally committed and uploaded to production environments.
| Comment | File | Size | Author |
|---|---|---|---|
| #121 | interdiff.txt | 1.34 KB | lauriii |
| #121 | 3278493-121.patch | 16.07 KB | lauriii |
| #117 | interdiff-111-117.txt | 738 bytes | mherchel |
| #117 | 3278493-117.patch | 15.81 KB | mherchel |
| #102 | Status_report___Drupal.png | 67.87 KB | mherchel |
Issue fork drupal-3278493
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mglamanComment #3
jeroentI think this issue is very similar to #1969278: UI for enabling twig debugging
Comment #4
mglamanIt is similar, but adds scope to disabling caches, because Twig debug is kind of useless without that.
Per https://twitter.com/plopesc/status/1521720652457205760?t=vfyE2hfnG0QIBjq... from plopesc, we could give this a UI which reads from the state key value storage.
Comment #5
mherchelHuge +1 on this:
+1
I actually talked to @lauriii about this, and he seemed really on board with this idea too!
I also like the idea of having a UI to turn on and turn off.
Let me know what I can do to help!
Comment #6
mglamanHere is my thought process:
* Add a checkbox to "Performance" or something under theme settings. This sets a value in the `state` key value store.
* Add a new ThemeDebugPass compiler pass in \Drupal\Core\CoreServiceProvider
* ThemeDebugPass checks if `state` is viable (install)
* If `theme_debug` is set, ThemeDebugPass sets twig.config.debug to true, registers cache.backend.null and sets it for page, dynamic_page, render cache bins
* Add something to the status report page warning site is in theme debug mode. Ideally it'd be a badge in the admin toolbar as well.
This shouldn't cause any conflict with backend devs modifying services.yml or settings.php, since they probably won't click the UI button.
The only "problem" is there isn't a code based way to trigger this setup. You have to click something in the UI. So it might be nice to have a settings.php setting AND state value.
Comment #7
tim.plunkettThis isn't directly accomplished now by using
sites/example.settings.local.php, but there's overlap. Not suggesting we expand scope here, but perhaps that file will need to be adjusted to acknowledge this new feature.Comment #8
andy-blumWould it be a bad practice/poor DX to have multiple avenues of activation? While a simple checkbox in performance would be great, as would a one-liner in settings.php, I could also see agencies liking the idea of an environment variable so that dev servers could just have all sites in dev mode all the time.
Also, would having the setting in config have any kind of tangible performance hit versus settings.php/environment variable?
Comment #9
mherchelYeah, if we were starting from scratch. And this is definitely something we need to consider for the DX.
However, keeping the activation of the debugging/cache as it is, is very bad DX. I'm not sure we can or should remove the old method.
Comment #10
mglamanConfig has the danger of being exported.
As @mherchel said, yes if it was fresh. However, this helps combine a poor DX. Do themers, right now, know to disable render cache to avoid cache rebuilds?
Drupal often has a UI option with code option. This new "switch" just combines something which exists in service definition and another in settings cache bin overrides. I don't think the existing options need to be changed. I think we just need a way to do both at the same time fairly easily.
Correct, it's half documented in the example.settings.local.php where it adds the development services definition and commented out lines for render caches. However it requires reading _a lot_ to know. And it's in two places
* copy default.services.yml to services.yml, modify a boolean in twig.config.debug
* copy example.settings.local.php, uncomment 3 lines
IDEALLY we could set the environment for the DrupalKernel to `dev` and it'll set this up automatically. But Drupal isn't there, yet. And there are reasons to use caching locally for debugging cache metadata.
Comment #11
rlahoda commentedA UI switch would be ideal for this, especially if it's tied to an environmental variable or something that would keep it from activating on a production environment. It would make the experience much easier for someone unfamiliar with Drupal who's trying to get started and be much easier for everyone else to do as part of the process.
Another, less friendly option, would be to just have devlopment.services.yml updated so that it has the twig debug settings already in there. This would at least mean that the option would already be there, similar to how example.settings.local.php has everything needed in there already it's just up to the user to uncomment what they want. Doing that would at least mean that the user wouldn't have to go find what the options they need to add in.
Generally, though, this seems like the kind of functionality that should be a single-step option to enable. The current process is definitely not beginner-friendly and if something is missed or incorrectly entered, the user can get pretty frustrated trying to figure out what was missed. I know this from my experience first learning Drupal right when D8 was coming out.
Comment #12
mglaman+1 that would be nice and we should open a follow up ticket. Having specific control of Twig via a development services file vs a "all the things" is still a use case in my opinion.
---
Assigning to myself. We've had quite a bit of feedback and I'll work on an initial MR/patch from my proposal in #6
Comment #13
btully commentedThank you for raising this @mglaman! This would be a huge improvement in DX. I'd go so far as to say that Drupal should give a warning when it recognizes that Twig debug is enabled but cache (page cache/dynamic page cache/render) is not disabled! I've been burned by that more times than I'd like to admit ;)
Comment #14
rlahoda commented@mglaman Based on your suggestion I created that issue: https://www.drupal.org/project/drupal/issues/3278887
Comment #15
WebbehComment #16
WebbehComment #17
mglamanWorking on this
Comment #19
mglamanI have a WIP test. It might be impossible with a Kernel test. Kernel tests replace the cache_factory with Drupal\Core\Cache\MemoryBackendFactory which makes it impossible to test settings in CacheFactory.
Copy of test I was hacking together:
Unassigning for now.
Comment #20
andy-blumWould it be possible to also remove development.services.yml from Drupal's default scaffolding?
The issue #3278887: Update development.services.yml to include twig debug by default would help a lot of people, but the problem remains that any development service customizations will be overwritten with every composer require/update/install. If a site has made a customization to the CORS config, for example, they'll also need to make sure they disable the scaffolding for that file or it'll soon be reset to its default state.
We don't overwrite settings.php or settings.local.php, so why should we be overwriting development.services.yml?
Comment #21
jwilson3Is this a duplicate of #2839709: Make Debugging Twig easier ?
The approach there lets us get the benefits of turning on twig auto_reload for all developers in development.services.yml, while leaving Twig debug off by default, and enabling it via a separate services file (by uncommenting a line of code in settings.local.php). The reason we took this approach is because twig debug can break some websites, whereas auto_reload has no drawbacks.
Comment #22
jwilson3Comment #23
adamps commentedThanks @jwilson3. The problems with twig debug are listed in #2914733: [META] twig debug can break parts of site.
Comment #24
purencool commentedI have created two test patches that if theme_debug is set not on the file system the configuration that is managed by core/modules/system/src/Form/ThemeAdminForm.php is used. The image below gets the twig.debug.debug_setting configuration created by the form through the Drupal UI and then sets the twig.config debug to true or false.
Comment #25
purencool commentedUpdated to the two patches so they will pass the automated testing.
Comment #26
mherchelHere's a contrib module that seems to add the same functionality https://www.drupal.org/project/twig_debugger
Comment #27
neclimdulmostly though it works by writing
default.services.ymlwhich might actually just destroy your site :-/Comment #28
neslee canil pinto@neclimdul Twig debugger module as got a config form where we can enable twig debugging by ticking the checkbox and saving the form, where it will create a new file called
services.ymland copydefault.services.yml'sfile into it.And in services.yml for now we have just manually done
debug: true, auto_reload: true, cache: false, but we can also provide users expose these 3 keys by giving users to toggle these 3 keys in form, I have thought about it and will be pushing it in next release. Not sure if this is the right way to play with drupal sites folders and files.Comment #29
ghost of drupal pastThis is absolutely lovely but have we considered storing this in session instead of config? Would need some thought for anonymous but I wanted to float the idea.
Comment #32
rabbitlair commentedI created a new MR, based on previous work from mglaman, implementing the following approach:
- A checkbox to enable and disable the theme debug mode is added to `/admin/appearance/settings`.
- The checkbox value is stored in Drupal's state, so no changes are generated when configuration is exported.
- In form submit, when the value is changed only, caches are cleared for debug mode to be enabled/disabled immediately.
- The value stored in Drupal's state can be overridden from settings.php file by setting a boolean variable named $settings['theme_debug']. When this variable is set, the checkbox is disabled to avoid confusion about what the expected behavior is.
- Added the test from comment #19 with some minor changes.
- A warning is displayed in `/admin/reports/status` when the theme debug mode is enabled.
Comment #33
andy-blumDid some in-person review at DrupalCon Prague and there are a couple issues:
Comment #34
andy-blumFollowing 8a5c49fb this functionality works beautifully
I don't have the skillset to do an analytical code review, but I can say with certainty that frontend devs around the world are going to LOVE this.
Comment #35
rabbitlair commentedThank you @andy-blum and @alexpott for your help! I have added a new commit to move the checkbox from the Theme settings form to the Performance form.
Comment #36
rabbitlair commented.
Comment #37
adamps commentedGreat that this issue is active and making progress. I have commented already on #2839709: Make Debugging Twig easier. This issue is seemingly taking over, with the advantage of adding a GUI so I'll make the same comments here.
There are 3 relevant Twig settings:
I suggest we could usefully use the same approach adopted in the other issue, and allow twig:debug to be controlled separately. So there would be a checkbox "enable debug", and if this is set, another checkbox "twig debug", which defaults to enabled, but can be disabled if needed. There can be a warning about the possible problems in the field description.
What do people think?
Comment #38
jwilson3Personally, I don't think a GUI is needed to change one single config. It should be a one-line code change in settings.local.php (hence the other issue), but I'm biased towards low-code solutions and small core.
Even Matt in the OP on this issue mentioned the Proposed Solution:
At the very least the approach on the PR on this issue needs an IS update.
Comment #39
mherchelI hear what you're saying, and I'm assuming this will be changeable via the settings.php, but IMO one of the primary purposes of this is to make it easier for new-to-drupal front-enders to get up and running. Even a making a change to settings.php can sometimes be difficult.
Comment #40
mherchel@rabbitlair and @mglaman - thank you so much for working on this! This is really going to smooth the process for front-enders starting out w Drupal.
I'm setting this back to Needs Work because it's consistently failing tests (I re-ran them and they still failed). The failures seem like they should be unrelated, but either way we need to investigate.
I'd also like to spend a bit of time bikeshedding on the verbiage.
Theme debug mode- This setting affects more items than just theming, right? Maybe we should call it "Development mode" or something?Enable theme debug mode. This option will turn Twig debug mode on, and set Null cache backend for page, dynamic_page, and render cache bins. Cache will be rebuilt after changing this setting.. I personally think we can skip saying exactly what caches are disabled, and maybe something along the line of "You can see changes in your templates without rebuilding cache", and it will show template suggestions within the HTML comments. We also need a big ol' warning that says, "DO NOT USE ON PRODUCTION!" or something :)Thoughts on all of this? I'm really excited about this. Thank you everyone for working on it!
Comment #41
andy-blum@adamPS brings up a good point:
We're currently setting all three of these values with a single checkbox as well as nullifying a bunch of caches. I wonder if a new fieldset with multiple checkboxes would be good to have more granular control?
Also, to @mglaman and @jwilson3's point:
@rabbitlairs solution allows settings.php overrides and avoids using config to set the values. So there's virtually no risk of accidentally exporting the values, and a production site can always set the values how they want them in settings.php to prevent accidental usage there.
Comment #42
bronzehedwickFirst, this is a great option that will certainly make my life easier. I'd like to add my voice to having the option exposed in all 3 places: settings.php, in the performance admin ui, and as an environment variable. Each serves the needs of a different user archetype.
Comment #43
mherchelI reached out to @lauriii and @mglaman on this to brainstorm. I’m updating the issue summary with a solution that we all believe works really well.
Comment #44
mglaman🥳 I love it.
Comment #45
adamps commentedGreat thanks it's a good start, but misses some important points.
1) This solution doesn't cover comment #37. I'll post that again to save people searching back:
There are 3 relevant Twig settings:
I suggest we create two separate settings:
dump()function.2) It doesn't cover comment #42 and various others also to allow control from settings.php.
3) The description in the IS of the checkbox seems wrong, see documentation).
The setting should enable
auto_loadnot disablecache. I don't see any need for a relationship to the "Disable all caches" setting.Comment #46
mherchel@AdamPS The thought process that was discussed with @lauriii is that the use-case of enabling only one at a time is an extreme edge-case (in fact, in all my years, I've yet to run into it). In the event that someone does need to enable only one or the other, they can do so using the current means (
services.yml+settings.php)The new states would still be configurable via
settings.php.Which checkbox? Caching needs to be disabled for changes in Twig templates to be immediately viewable.
Comment #47
adamps commentedThanks for the reply.
Well we could try to measure this quantitatively:
I have hit it on several sites. At least one other commenter has made the same point (so that makes 2 out of the 16 people commenting, more than 10%). If we make a GUI setting that makes it easy for people to enable twig.debug then it's likely a lot more people will hit it, and it's pretty hard to debug when it happens. The "current means" to workaround you refer to are not easy, hence this issue.
What is the big problem with splitting this out in the end? The current solution bundles 3 settings that are clearly distinct in the TWIG world, and it makes for a long description to explain because the effects are quite distinct from each other. Would it be such a bad thing to split into 2 separate settings each with a shorter description?
Great. Please this could be clarified in the IS with an example?
Currently the checkbox is called "Enable Twig Debug Mode".
No that's my point - please read the docs I linked to😃.
auto_reloadneeds to be enabled to have this effect.Comment #48
mherchelThat's a good point. The thought is to provide a simplified UI.
I'll clarify that this needs to be available within settings.php, but we'll leave the actual code to whoever is developing this.
In my testing and conversations, I'm not sure this is correct. If
debugis enabled, the other two settings do not have an effect.Comment #49
mherchelI'll reach out to @lauriii to make a decision on the points that you've discussed. I'd like to keep momentum on this issue.
Comment #50
adamps commentedThanks @mherchel
My recollection is that if debug is enabled then auto_reload is automatically enabled. Disabling cache is clearly documented as not recommended, so presumably we shouldn't do that.
Comment #51
lauriiiI think it makes sense to try to cater to #2914733: [META] twig debug can break parts of site here even though it doesn't seem like something every user would have to run into. It could even be helpful to provide an explanation in the UI that the Twig Debug mode modifies the HTML markup and may lead into different behavior from production.
I like the simplicity of a simple checkbox, but I do believe we could make the UI with two checkboxes apprehensible as well. We could potentially visualize some of this as well by hiding the auto reload checkbox when the full "Twig debug" mode is turned on.
Comment #52
lugir commentedMark
Comment #53
rabbitlair commentedI implemented some changes to the open MR.
A new form named "Developer settings" has been added, with its own route and menu item. It contains a checkbox, "Enable Twig debug mode", which enables the Twig "debug" flag, and two other conditional checkboxes. These are to handle Twig's "autoload" and cache disabling. The one disabling cache disables both Twig cache flag and the Drupal's page, render and dynamic page caches.
The three options can also be overridden from settings file. Please, let me know if this implementation works better!
Comment #54
dan_metille commentedComing here to find a way to disable auto_reload, which seems to be automatic while debug is set to 'true'.
I understand that in most case it is useful, but from a theming side it is NOT ALWAYS the case and I would like to have the option to disable it. (If there is already a way, could someone please let me know). In case if we are using Vite.js to compile the theme, auto_reload is unnecessary and time consuming.
Comment #55
star-szr@sahaj you can set
debugtotrueandauto_reloadtofalseif you want debug but not auto_reload.The behaviour is that if
auto_reloadis not specified, then it inherits the value ofdebug.Comment #56
ressaGreat work, this will be a nice improvement. Could the same be done for core and module development, so that all caches can eventually be easily disabled, via the GUI or running a single command such as
drush cache:disable? #3342678: Easy way to bypass caching and aggregation with Drush or settings.php.Comment #57
mglamanBumping the version to 10.1.x
Comment #58
mherchelRe-rolled the MR and created a patch (since MR is against 10.0.x and Zequi is probably sleeping)
Comment #59
mglamanReworked the patch. Moved settings to Performance from Developer settings. Streamlined the options a little bit. Removed anything involving settings.php overrides to simplify the logic. The settings.php work in the existing patch is already possible via services.yml, this issue should focus on a UI for those values.
Comment #60
mglamanI'm going to revise #59. There's no test coverage and I think empty values should be deleted from state and not saved as
false.Comment #61
adamps commentedGreat to see this moving forward thanks everyone. The issue summary seems to be getting increasingly left behind.
1)
I can see your point, as previously there was "Admin > Config > Development > Developer Settings" - and what's the difference between development settings and developer settings😃.
On the other hand we now seem to have "Admin > Config > Development > Performance" and then a details box titled "Developer settings", which still doesn't seem quite right. How about reorganising the Performance page something like this:
2) Two of your four settings are "disable XXX" rather than "enable XXX" - potentially this negation is a confusing UI??
3) It would be great to have warnings in the form UI for the two settings that have potential disadvantages:
twig_debug: This modifies the HTML markup and may lead into different behavior from production, even causing bugs.
cache: This is recommended; even in the dev environment, because the auto_reload option ensures that cached templates which have changed get compiled again.
4) The patch seems to save the new values to state rather than config, which seems unusual and potentially confusing. I understand that the values are likely to be different on dev vs production. However that's also true for many of the other development settings. What is the advantage to this approach?
5) The code seems to invalidate the container if any value is enabled. Shouldn't this be if any value has changed?
Comment #62
mglamanI'll reply to #61 in full later, but
This shouldn't be a configuration that can be persisted. It should be something specific to that environment, which is what the state key value store is for.
This is why it needs test. It should be invalidating if it's ever changed but there may be edge cases.
Also: wording. I avoided trying to make it perfect because the issue comments will provide the bikeshedding for best words.
Comment #63
mglamanNew patch
- fixed twig_autoreload to twig_auto_reload for cspell
- improved UX (auto-check sub-checkboxes if nothing is enabled) and values bug
- container invalidation anytime twig dev mode values change
I think it should link to official docs, because they'll stay up to date. @mherchel informed me about twig debug adding HTML comments which breaks RSS feeds. So there are edge cases and this is best handled in guides.
All wording and variable names are up for discussion, just getting work up so we can review.
Comment #64
mglamanForgot the patch 🙈
Comment #65
adamps commentedThanks for the responses @mglaman
Great - the wording was from the docs anyway
It's funny how many people who haven't hit this themselves say that it's an edge case😃. It's not only RSS, please see #2914733: [META] twig debug can break parts of site (which when counting together all the child issues has more followers than this one😃).
@lauriii commented in #51
It can save people some trouble so I wonder is there any significant disadvantage???
Comment #66
mglamanNew patch: fixes phpcs, and container invalidation when root twig development mode toggle used
Comment #71
mherchelDid some hallway testing at MidCamp with front-end devs @hbrokmeier, @callinmullaney, @mndonx (crediting).
We worked on verbiage and expectations:
Comment #72
benjifisherI looked at this issue with M&M at MC (@mherchel and @mglaman at MidCamp 2023). What is the best way to present the options under "Twig development mode"?
The options "Enable Twig auto reload" and "Disable Twig cache" are not independent, so it does not make sense to have a checkbox for each. If I have it straight, the first option invalidates the cache if it is older than the source, and the second option completely bypasses caching. Instead of two checkboxes, I think there should be three radio buttons:
I also reviewed the current labels and descriptions:
It is confusing to use "Enable" and "Disable" for checkboxes. We should avoid repeating the label in the description. We do not need a description at all if it just repeats the label. I came up with some alternative text:
Maybe remove "in HTML comments" from the first label.
The last option on the page is
I suggest changing that to
Comment #73
mglamanNew work-in-progress patch since I'm leaving MidCamp soon. Captures some ideas from @benjifisher where there we unify auto_reload and disable caches as a radio option. Enabling auto_reload is useless if caches are disabled because there are no compiled templates to checksum and reload.
Comment #74
benjifisherI talked about this issue at MidCamp with @akalata and Jen Stein (not sure of the d.o username). We agreed that the UI should have three radio buttons, and we recommend this:
(That is, the middle option is the only one that needs description/help text.) Probably the middle option should be selected by default when "Twig development mode" is enabled.
Comment #75
mglamanRemoving myself from assignee. Not actively working on it right now, so someone else can try a reroll with with feedback included
Comment #76
mherchelThe issue with this option is that it doesn't give the granularity of enabling and disabling the 3 options, as pointed out in #37 by @adamPS and +1'd by @lauriii in #51
Comment #78
mherchelAdding credit for @akalata's discussion with @benjifisher.
Pretty sure Jen Stein isn't on Drupal.org. I found her on MidCamp's site, but not here.
Comment #79
mherchelVerbiage changes
Comment #80
benjifisherI am afraid I was not clear in #74.
There are currently three checkboxes:
I meant to suggest that we keep the first one and replace the other two with three radio buttons.
That reduces 4 options to 3. The one we leave out is disabling the second option and enabling the third. That combination does not make much sense: see #72. Anna, Jen, and I think this is a better UI than the suggestion at the end of #51:
Comment #81
mherchelAdding a missing comma
Comment #82
benjifisherLet me see if I can help a little here.
If I read it correctly, PHPStan is complaining that you are telling it to ignore some lines that do not have any problems. So this patch removes the
@phpstan-ignore-next-lineannotations.I am a little unsure, since
core/scripts/dev/commit-code-check.shpasses locally with or without those annotations.Comment #83
mherchelWorked with @ckrina (UX Maintainer) and @mglaman on a Zoom call to nail down the user interface. For any additional improvements, please open up a followup issue.
On
admin/config, this item will go directly under “Performance”This form will go under
admin/config/development/settings. The title of the page and menu item is “Development settings”When “Twig development mode” checkbox is checked, both checkboxes within the child fieldset are checked by default.
Note that we removed the Twig auto-reload setting, as when Twig debug mode is enabled, this will automatically get enabled. This is the most common development setting for new developers. For more advanced configuration, changes can still be made in
development.services.ymlNote we will open up a followup issue to provide a indication in the UI if settings are overridden.
Comment #84
ckrinaThis is a huge improvement for front-end DX, thanks for working on this!
Comment #85
mglamanComment #86
mherchelThe form doesn't tell the user that the changes have been saved.
Comment #87
mglamanBad interdiff. Here it is manually:
Comment #88
mglamanWrong page! And could use individual boolean checks versus array_reduce now that only two options exist.
And cspell:
/var/www/html/core/modules/system/src/Form/DevelopmentSettingsForm.php:74:160 - Unknown word (recompiles)
Comment #89
mherchelMatt's going for a run. Updating the patch to fix 👆
Comment #90
mherchelNo clue if this is correct. Let's find out! 🤞
Comment #92
mglamanDebugging test failures. Also replacing usage "recompiles" to just "recompile"
Comment #93
mglamanSo it is due to fetching the state service during the altering of the service container.
I should have expected that I suppose. I know this approach works with config because JSON:API Extras does it, but that's because we have
BootstrapConfigStorageFactory.Something about fetching the key-value store here breaks things. I don't know why yet.
Edit:
I think it might be because KernelTestBase switches the key-value store with a memory backend. that's why it breaks in Kernel tests but not functionally. Yep, that's it. I used Xdebug and the
\Drupal\Core\KeyValueStore\MemoryStorageends up empty at some point in time and loses its data.When we fetch the state service and it grabs a key-value instance, I think the
persisttag is bypassed, or something. When tests call\Drupal\KernelTests\KernelTestBase::enableModulesit calls\Drupal\Core\DrupalKernel::updateModuleswhich initializes the container. Then the key-value store gets reconstructed and all in-memory data is lost.Persisting doesn't seem to work as expected here.
It's related to our code in
\Drupal\Core\DrupalKernel::compileContainerand\Drupal\Core\DrupalKernel::getServicesToPersistDebugged. Our alter causes the state service to be defined before the persisted service can be re-attached
We cause
stateto be initialized so the persisted service is not re-attached.I tried moving to a CompilerPass and that didn't fix this problem.
Comment #94
mglamanAH! So our test fails are caused by
persistand #2368263: Remove the persist service tag would fix it by handling the persisted key value memory factory.Comment #95
mglamanOkay, here is an updated patch that s/recompiles/recompile and moves logic to a compiler pass outside of the service provider.
I'm going to work on a new issue that fixes the testing bug in KernelTestBase based on #2368263: Remove the persist service tag without trying to deprecated the
persisttag.Comment #96
mglamanOne more comment spam for the day. Linking to #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase. Hopefully, that can be merged as a bug fix to unblock this issue.
Comment #97
dwwAs a sort of test-only patch for #3358048 and to see how much closer we are, here's #95 and the MR from #3358048 applied together. Interdiff is https://git.drupalcode.org/project/drupal/-/merge_requests/3920.diff ;)
Comment #98
dwwcore/scripts/dev/commit-code-check.sh --cachednow passing. 😅 Sorry for the noise.Comment #99
dwwI wanted to compare #95 with #98 to prove that we should commit #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase, so I queued the test run. Sadly, that failed for the unused use. So here's just #95 after running
phpcbfto makecore/scripts/dev/commit-code-check.shhappy. This will definitely still fail. Hopefully more like #90 than #98. 🤞Comment #100
mglamanHere is #95 rebased off of 10.1.x, which should be the same as #99.
Comment #101
mglamanOkay, I did a horrible merge here and didn't catch it.
Comment #102
mherchelUpdating issue summary.
Comment #103
mherchelComment #104
mglamanNew patch
- fixes menu link and routing changes
- renames compiler pass from DeveloperSettingsPass to DevelopmentSettingsPass to match form name
- moves DevelopmentSettingsPass earlier before BackendCompilerPass so setting default_backend is respected
Comment #106
alexpottThis is the reason that we needed to do #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase. I think this is wrong. We can't use service in compiler passes. That's really really fraught. I think we should have left #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase alone. This change will result in all KernelTests now using the database for state because it results in the instantiation of the state service prior to \Drupal\KernelTests\KernelTestBase::register() running.
We need to do something different here.
Comment #107
jwilson3Since enabling the "Twig development mode" checkbox adds a Warning message to status report page, and "Do not cache markup" is also being introduced as a separate, top-level checkbox alongside Twig development mode, and both of these can be detrimental to website performance on production/live sites, it seems it would follow that we should also have a separate Warning message when "Do not cache markup" checkbox is enabled, right?
Comment #108
mglamanIt doesn't, this compiler pass runs after all other service providers register definitions.
What's the alternative?
BootstrapConfigStorageFactorycauses database calls during container compilation. I don't think Drupal core has this, but contrib does.It'd be a horrible idea to use config, because it could get exported and persist. Drupal core provides no alternatives or ways to prevent individual configuration objects from being exported.
Per #107: yes, you're right. It needs its own status check.
Comment #109
mglamanJust to clarify the container compilation flow:
\Drupal\Core\DrupalKernel::compileContainerinvokes all service providers:Kernel tests register in
$GLOBALS['conf']['container_service_providers']whichdiscoverServiceProvidersadds to$this->serviceProviderClasses['site']. Kernel test registers its alias here.The
DeveloperSettingsPassruns afterModifyServiceDefinitionsPass. This compiler pass runs all service providers which implementServiceModifierInterface.I agree it's not the best to initialize a service during compilation. However, it is being done after all registration and alters, resulting in final processing by any remaining compiler passes.
Comment #110
bnjmnmThis is something I've wanted for years. The fact that it wasn't in core made me think there was some huge obstacle but this is a really solid way of doing it.
Outside of what has been reported, here's the one whole thing I noticed in my review:
Could this get test coverage? TBH not concerned about it working right in this scope, but more to ensure some later addition doesn't break it.
Comment #111
mglamanAdds status warning for disabled markup output caching. Adds tests for status report.
Comment #112
alexpott@mglaman so why was the change in #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase necessary - what is the root cause? My guess is that persistence is done at a different time. Ah yep \Drupal\Core\DrupalKernel::attachSynthetic() is called after everything is compiled. Should have read #93 - thanks @mglaman
I wish we'd added a bigger comment in #3358048: Do not use persist tag for keyvalue.memory in KernelTestBase :) but I can open an issue for that. I agree with @mglaman that accessing the db here through state is probably okay. We also probably should use the PassConfig::TYPE_AFTER_REMOVING etc... more in CoreServiceProvider::register() - nearly all of our stuff is being added as an optimisation pass when everything should run after the removing pass. I consider #106 resolved.
Comment #113
mglamanThanks, @alexpott! Yeah I'd love to dive into CoreServiceProvider and our CompilerPass after this, first time I really explored that area.
Comment #114
dwwRe: #112: Here you go. 😉 #3358328: Improve how KernelTestBase manages its persistent key value storage
Comment #115
smustgrave commentedUsing a fresh install on D10.1 without a settings.local.php file and with a default development.services.yml file.
1.
Unchecked
Twig development modeChecked
Do not cache markupWent to /admin/reports/status
Verified I see "Markup caching disabled
Render cache, dynamic page cache, and page cache are bypassed. Go to development settings page to enable them."
Unchecked
Do not cache markupVerified warning in status page goes away.
2.
Before checking
Twig development modeverified that debug info is not appear in markupChecked
Twig development modeBut didn't check any sub items.
Clicking save doesn't save
Twig development modeSeems expected but slightly odd. But don't think a show stopper
3.
Checked
Twig development modeChecked
Twig debug modeDid not check
Disable Twig cacheClicked save
Twig development modeis unchecked BUGInspect the markup and debug info is appearing.
Go to status page verify I see "Twig development mode
Twig development mode settings are turned on. Go to development settings page to disable them."
4.
Checked
Twig development modeChecked
Twig debug modeChecked
Disable Twig cacheClicked save
Twig development modeis checkedInspect the markup and debug info is appearing
Test coverage seems good.
Other then that one small point think this is good to go.
And as posted in slack a change record should be made.
Comment #116
mglamanwhomp whomp.
should be
||Comment #117
mherchelGood catch. Fixed in this patch.
Comment #118
mherchelChange record created at https://www.drupal.org/node/3359728
Comment #119
smustgrave commentedIssue I found in #115 has been addressed.
Comment #120
crasx commentedTested this using the standard profile, with olivero:
core/themes/olivero/templates/layout/html.html.twig)I would expect the new version, is that an issue?
Comment #121
lauriiiI gave the patch a thorough review and it looks solid. Also the UX is pretty good. I made super minor nitpick changes to the patch. Posting just to make sure custom commands pass.
I think the UI texts in the patch are fine but I'm still planning on creating a follow-up to bikeshed some of the texts.
#120: That's interesting. Maybe worth fixing in a follow-up because you'd have the same behavior right now 🤔
Comment #122
alexpottCommitted da2eaec and pushed to 10.1.x. Thanks!
Comment #124
alexpottAdding release note.
Comment #125
alexpott@crasx / #120 - that's a smart comment. I agree with @lauriii that we can address that in a follow-up.
Comment #127
lauriiiHere’s the follow-up I filed: #3359859: Improve UI texts for Twig development settings
Comment #128
wim leersI observed this:
Moved to that follow-up. 👍
Comment #129
ckrinaYay for getting this in! I just added #3359931: Latest/real markup should be used when caches are enabled as a followup per #120.
Comment #130
generalredneck@mherchel,
Tracked down Jen Stein's username: jen.stein
Comment #132
mherchelCrediting. Thanks!
Comment #133
ressaThanks everyone, this is an awesome improvement. I just used it to try the New clean_unique_id Twig filter in Drupal 10.2 (
11), and it worked flawlessly.@mherchel suggested at one point, which I agree with:
So I created #3363676: Enable Twig debugging and disable render cache via Drush and settings.php.
Comment #135
chi commentedI'm late to the party, but don't you think that having a dedicated checkbox to expose another two checkboxes looks a bit weird from the UX perspective? That also made the logic in the form much more complex.
Comment #136
ressaI created Add Drush command to toggle Twig debugging and CSS/JS aggregation #6267.
Comment #137
ressa@keshav patel created a Drush command (
theme:dev) which toggles Twig debugging, caching and aggregation, included in the latest Drush 13.6 release. See also Drupal Documentation page Disabling and debugging caching > Toggle Twig debug, caching and CSS/JS aggregation with Drush.