Problem/Motivation
(Why the issue was filed, steps to reproduce the problem, etc.)
Proposed resolution
When you have the config rewrite file for the configuration that might exist in your system, but it is not, config rewrite creates the new configuration with the settings only from your changes and that cause a lot of problems with invalid configuration.
This method from configFactory class:
$config = $this->configFactory->getEditable($file->name);
returns the config object always, even if config is not present.
This causes original_data from here
$original_data = $config->getRawData();
to be empty and after merging it with rewrite it gives the same rewrite array as you have in your rewrite yml file.
I propose to check if the original_rewrite array, read from yml file, is equal to the array after rewriteConfig method. This will allow to avoid the issue with corrupted configuration.
Remaining tasks
- Review the patch
- Write tests
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | 2903629-handle-optional-nonexisting-19.patch | 15.78 KB | saltednut |
| #14 | interdiff-2903629-13-14.txt | 816 bytes | samuel.mortenson |
| #14 | allow_optional_config-2903629-14.patch | 7.52 KB | samuel.mortenson |
Comments
Comment #2
saltednutThis looks like a good solution. Can we get an additional test added?
Comment #3
saltednutHere's an updated patch that should address the issue. I've included a bit more logic for checking if the rewrite should be saved in addition to some more logging and added a step to the test that should pass...
Comment #4
saltednutComment #6
saltednutThis is an updated patch that changes a few minor things and accomodates the failing multilingual test.
We had been creating translated config overrides on the fly here too but now that existing translation overrides do not yet exist, the check shows they fail during import. Therefore role.test4 will import as its original version, but config rewrite does not allow for the translation overrides to be written. We actually do want that feature though, so logic had to be changed to accomodate it.
In any case, this should allow for not-yet-existing overrides to be created (yeah, they'll load an empty $original_data object but thats ok).
At the same time, the new logic should catch when we're dealing with a standard config rewrite being attempted on an empty config object (which is the use case described that we need to avoid in the OP).
Comment #8
saltednutMy bad, left some comments in that should have been removed to pass the phplint.
Comment #9
saltednut@a.dmitriiev I am going to leave this as-is over the weekend and look to commit it next week if no objections.
Comment #10
a.dmitriiev commentedHi @brantwynn,
Thanks, for taking care about this issue. I will try to test the new patch, but from the quick view it seems to do what it is supposed to do. I will test and write my feedback in the issue.
Comment #11
saltednutComment #12
saltednutUpdated patch with a minor fix that adds the missing @langcode.
Comment #13
saltednutAdded some better logging for when $langcode is being used.
Comment #14
samuel.mortensonThis updated patch adds support for optional config rewrites as a part of a profile install, which is a tricky problem as optional config is actually installed after all dependencies of an install profile are installed.
This change requires #2924549: Invoke hook after a site install is complete but should not affect the existing behavior of config rewrite if you do not patch core. All install profiles using config rewrite to rewrite optional config will need to patch core, after this is committed.
Comment #15
a.dmitriiev commentedComment #16
manuel.adan#14 works for me, but as mentioned, it depends on #2924549: Invoke hook after a site install is complete that blocks the patch to be committed into config_rewrite.
Another approach to solve this with no dependency on additional core hooks could be act on config save event. Optional configuration that currently cannot be rewritten could be added to a delay queue and be applied by a config save event subscriber.
Comment #17
jepster_Still not handled. We recommend to use our fork: https://www.drupal.org/project/config_replace. There this issue is already tackled.
Comment #18
saltednutWait... you forked config_rewrite as config_replace? Are you deliberately trying to confuse people?
We probably could've worked together. Heartbroken!
I agree its not ideal we need a core patch to make this fix, but seems like a pretty big deal to fork just for one issue? Were there other issues we could've helped on?
As a general practice, its not really good form to just fork a module then pimp your fork in their issue queue. Why not try to collaborate or co-maintain? Honestly disappointed.
Comment #19
saltednutI went ahead and checked the codebase for config_replace and it looks like aside from renaming a bunch of classes and adding some additional tests re: writing nonexiting config that everyone could've used, nothing has really changed.
I don't have time today, but honestly not much stopping us from just taking back what you "contributed" in your module to provide fixes for the larger user base. What a colossal waste of everyone's time.
Next time try my user contact form, filing an issue here, or even emailing my very obvious work email before spending TONS of cycles on forks that no one needs. Ugh...
Comment #20
saltednutI freed up some cycles to try this out. Here's the patch. Its really not too big once you undo all the renaming, most of which could be undone via perl or by using the rename utility.
Anyway, here is is, with Peter listed as a co-maintainer. And yes, if he comes back here and wants it, he can RTBC the patch and be added to the list of maintainers. No hard feelings here, I just want what's best for the community of 2000 installs for this module and the other one combined. (1700 here, 300 there).
Maybe someone can review?
Comment #21
saltednutComment #22
a.dmitriiev commentedHi @brantwynn, please check also the related issue https://www.drupal.org/project/config_replace/issues/2986272 . There is a problem now with throwing the exception during config rewrite. Because the modules could have optional configs that they want to rewrite (e.g. rewrite a view, but views module is not enabled, but you add views.view.your_name.yml in your config/rewrite folder) the module will never be installed, because on pre-install there would be an exception thrown. I think that log message and skipping of such a config is enough, no need to throw the unhandled exception. What do you think?
Comment #23
saltednutI see that the issue linked was closed, works as designed by Peter and I kind of agree with him there. That said, I think the patch you proposed to gracefully fail in those cases is a good work-around, its probably best to not obfuscate these things, but adjust architecture accordingly.
If anyone has time to review/RTBC this patch, I will bring in these changes and we can move forward.
Comment #24
a.dmitriiev commentedBut do you agree that module can have in its config/rewrite folder the optional configuration and it should be still possible to install the module, but without optional configurations? And without seeing white screen and no explanations?
Comment #25
saltednutNo, I don't agree with that and I don't appreciate you derailing this issue with that edge case. If you want to file a different issue to talk out that problem, we can do that there, but this is not the place.
You shouldn't be trying to rewrite configuration that's optional. You should only be rewriting config that you know is already installed.
Now, the issue here, really, is that we dont want to create new config, we want to throw an exception because the developer has tried to do something that will break things.
What you're asking for is to violate the terms of the API in random cases because you do not know if you'll have modules installed. That's only going to cause you problems.
Yes, right now, we want an exception thrown. No, we don't want to soft obfuscate the error in cases where people provide rewrites for optional config that's not there.
The use case you describe should only exist when one stubbornly tried to force in rewrites without declaring dependencies in a way that ensures the config is installed before it's attempted to be rewritten.
Comment #27
saltednutComment #28
a.dmitriiev commentedOk, this is your module and your choice.
Comment #30
smulvih2Patch fails to apply to config_rewrite 1.4.0. Will try the dev version.