Problem/Motivation
#2361539: Config export key order is not predictable for sequences, add orderby property to config schema introduced predictable sequence ordering. It introduced orderby: value and orderby: key. But quite a few things in core and contrib depend on "source order": where the stored order actually has meaning. This was overlooked. Therefore those cases are left with an unclear upgrade path.
Relevant comments lifted from #2361539, which is closed:
- Wim Leers:
Actually, there is an important question that is not answered by the CR or the IS, at least not AFAICT. Quoting #2874633-5: [PP-1] Update CDN module for Drupal 8.4.x: 'orderby' in config schema for sequences:
+++ b/config/schema/cdn.mapping.schema.yml @@ -27,6 +27,7 @@ cdn.mapping.auto-balanced: + orderby: value @@ -43,6 +44,7 @@ cdn.mapping.complex: + orderby: valueActually, these should not be sorted/ordered… the order does matter in this case, and it cannot be ordered by key (there isn't any). In fact, the key is numerical, hence indicating the order already.
So these should be
key, notvalue. I think that that must be the general rule for non-associative arrays where the order matters?Reopening this issue to get an answer to that question documented in the CR, because more modules will struggle with that.
- Alex Pott:
@Wim Leers hmmm this is a good question. Actually in this instance the best practice is to have a weight key and sort by something that does not change. This means that if things are re-ordered when you review the config change the only thing you have to review is the weight key changes. However maybe we need to add "in the order added" type of "sort" so at least the current behaviour of cdn could be documented and we could document that this is bad practice and recommend ways of fixing it.
- Wim Leers;
However maybe we need to add "in the order added" type of "sort" so at least the current behaviour of cdn could be documented and we could document that this is bad practice and recommend ways of fixing it.
+1 to both of those.
- Wim Leers:
By the way, an example of exactly the same problem/requirement can be found in
ckeditor.schema.yml: the Toolbar configuration mapping contains a sequence of rows, which contains a sequence of buttons groups, which contains a sequence of buttons. None of them have "weight" or "priority"; the order is relevant there, too.So this is not a problem that's exclusive to contrib. And probably there are even more like these in other places in core.
Proposed resolution
TBD
Remaining tasks
TBD
User interface changes
None.
API changes
TBD
Data model changes
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | 2885368-nr-bot.txt | 132 bytes | needs-review-queue-bot |
| #12 | 2885368-12.patch | 3.24 KB | dawehner |
Issue fork drupal-2885368
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
dawehnerI would always recommend that code to have a priority in their values, as this makes it way more explicit.
On the other hand you are absolutely right, order matters in sequences:
.
Comment #3
wim leersYep. I was thinking the same thing when I wrote this IS this morning: #2361539: Config export key order is not predictable for sequences, add orderby property to config schema effectively changed sequences to sets (for
orderedby: key, because there can't be multiple keys) and alphabetical sequences (fororderedby: value.Comment #4
wim leersI think we should consider elevating this to critical because if it doesn't get sorted out, 8.4.0 will effectively be causing a sort-of-BC-break and sort-of-regression…
Comment #5
wim leersAt this stage, it looks like #2361539: Config export key order is not predictable for sequences, add orderby property to config schema will not be reverted, and this is not getting resolved before 8.4.0, which means this newly introduced ambiguity in config schema semantics will have to be mentioned in the release notes to warn contrib/custom module authors.
Comment #6
catchIs there anything missing from the change record at https://www.drupal.org/node/2852566 that would help to explain this issue better?
I've just tagged #2361539: Config export key order is not predictable for sequences, add orderby property to config schema for a release notes mention.
I don't see this as an API change, it's a change in behaviour for something that was never previously defined, so just documenting it seems fine.
Comment #7
dawehnerThe question I would like to ask here: did we really guaranteed the order before? I remember having quite some issues with that in views. We ended up switching to using weights instead. I'm curious why you never ran into those ordering problems before.
Comment #8
wim leersThe CR says
There are at least three problems here:
orderbykey, or else!editor.settings.ckeditor.toolbar.*, it will be impossible to addorderby, because it's actually using sequences for what they were intended.IOW: those cases where sequences were used as intended actually are left in an ambiguous place.
Comment #9
wim leersI just realized something: I think it'd have been better to add a
type: setthat extendstype: sequence, addingorderby: key|value. #2361539: Config export key order is not predictable for sequences, add orderby property to config schema didn't clear up semantics, it just made it more confusing :( (Despite all good intentions obviously!)Comment #10
dawehnerReading the typed data definition:
this really makes it clear, that its not an unorded hashmap, like many other languages use by default.
Here is a question though: They code which does the sorting:
and
don't imply any kind of sorting by default. Am I missing something obvious?
Comment #12
dawehnerWhile my question in #10 is still outstanding, I think having an explicit way to enforce no sorting is helpful
Comment #13
xjm@cottser, @cilefen, @effulgentsia, and I discussed this issue and agreed it is not critical and also not really a bug. There is no regression; core does not yet specify
orderbyeverywhere -- #2855675: Add orderby key to all sequences in core has not had any activity at all -- and there is no hard requirement currently thatorderbyshould be set.When a specific ordering other than key or value is needed, it's currently the responsibility of the implementation to enforce that order on save or to add (e.g.) a weight that is independent of the configuration storage. I was going to state that the CR at https://www.drupal.org/node/2852566 indicated this, but it looks like that got omitted somehow. So, I think we should update the change record there with that information and maybe (as @effulgentsia suggested) an example of how an implementation might enforce custom ordering.
We also have this other followup still open: #2856336: Add docs for the new orderby key for sequences in configuration schema
As for the scope of this issue, it's really a request for an API addition. However, I'm not convinced that the current patch is the right approach. Arbitrary "insert order" is not something we should be encouraging because that could vary from one update to the next and create meaningless diffs and compromise the data integrity, which is what #2361539: Config export key order is not predictable for sequences, add orderby property to config schema tries to address in the first place. The fact is that arbitrary ordering has led to lots of data integrity issues in the past so we should encourage developers to rethink why they want this "insert" order in the first place.
Comment #14
dawehnerWe could go the go route which means that unless specified, iterating over a map happens in random order, so you cannot rely on it. In this case we would enforce that contrib/custom code sorts by some parameter ... which might be quite hard, but at least it prevents any other possible future bugs.
Comment #15
xjmWell, that would guarantee a config export diff on every single export, which is the kind of thing we're trying to avoid. :) Also that carries its own set of problems for us: #2571183: Deprecate random() usage in tests to avoid random test failures.
I think we can maybe go the route of deprecating the null sort order and add an example of how to implement one's own sort.
Comment #16
dawehnerI think I was suggesting somehow a read only behaviour.
Comment #18
wim leersTriaging the CDN issue queue made me look at #2874633 again, which made me look at this again.
orderbyis considered a best practice. I want my modules to follow every best practice possible. But it doesn't apply to/work for my use case, and it's not the only example! See #8.This is not "arbitrary" insert order. This is intentional insert order. The order has meaning. I already gave examples in the IS/in #8. So order changes in config diffs do have meaning in those cases.
I'd wholeheartedly support this, if it weren't for the fact that this also needlessly complicates a common use case. For example, from
editorconfig:would need to become the much more verbose:
and then we'd be able to use
orderby: key(but only because we're exploiting that it'd do increasing alphanumerical sorting, it'd be just as valid to do decreasing, it would still ensure consistent config exports, but it'd break the interpretation of the config!). What did we gain there? I'd argue nothing.Again, this is because https://www.drupal.org/node/2852566 and https://www.drupal.org/project/drupal/issues/2361539 only concerned themselves with "sequences that are sets" — either sets where each set item is identified by value (
orderby: value) or a set where each value is identified by key (orderby: key). The "sequences that are lists" use case seems to have been forgotten.If we'd change the CR from
to
Then we could close this issue.
Comment #19
wim leersWe already have
type: sequencewith an optionalorderbyin two minor releases now (8.4 and 8.5). We can't change that anymore.To improve the situation, we could:
type: setwhich requires anexport_order_bytype: listfor the use case that I've been explaining abovetype: sequence("sequence" implies order matters, but that's not the case in practice)Comment #21
amber himes matzUpdated title and issue description with correct key name (was 'orderedby'; corrected to 'orderby').
Comment #29
quietone commentedUpdating tag
Comment #31
wim leersI think perhaps the existing
orderby: ~would be sufficient, although this seems it would improve clarity?Comment #32
bircherI think we should make it more explicit and call it
orderby: none.because it is not a default order, it is the absence of sortability.
This would be already much better, because then we know where the sorting is explicitly not wanted or where it is not yet defined.
Clarifying a bit: if we add
noneas an option then we could do sorting by default by key if keys are non numeric and value if keys are integers in Drupal 11Comment #33
wim leers#32++
💯 — love this plan! Not sure if
keyorvaluemake more sense as the default (the problem is IMHO thatsequenceboth allows keyed and keyless sequences…), but not havingnonebe the default absolutely makes sense! It'll most certainly lead to a better config management experience.Comment #34
birchergiven the isdues could be more sorted and organised, the plan is basically to add more options for
orderby.The core extensions need
values_then_keys, others needweightyet others will need some more complex algorithm. I wanted to make it so one could add a php callback, but the wise alexpott remembered that this would make it impossible for external systems to manipulate the config.So part of the challenge is to assess what sorting algorithms we need. And
nonecould be an option. Then "consumers" of this schema information like Config Split can just treat unsortable config as a blob and not do anything with it.The default could then be the most complex one which tries to be smart and detect all sorts of possible sorting options at the price of performance.
Comment #35
wim leersI'd rather not have such magic for something as fundamental as this. 🙈 Explicitness is preferred?
Comment #36
bircherwell yes of course!
The default could also be
valueif the keys are integers andkeyotherwise.The goal should be that core defines
orderbyfor all its sequences.Comment #37
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #39
damienmckennaThis keeps coming up with so many projects, it's annoying to still deal with lines changing their ordering in config exports. I opened #3489389: Sort config alphabetically if no orderby defined as an alternative to this, because there should be a simply, predictable default to this, instead of having to manually specify it every time (as some have suggested here).
Comment #43
xjmCrediting for triage as per #13.