Skip to content

Fix duplicate and stale custom fields in MetadataCache - #432

Open
hahn-kev wants to merge 2 commits into
developfrom
fix-duplicate-custom-fields
Open

hahn-kev wants to merge 2 commits into
developfrom
fix-duplicate-custom-fields

Conversation

@hahn-kev

@hahn-kev hahn-kev commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes an issue Claude found which would result in replicating custom fields in FdoClassInfo. This also resolves a potential stale cache when a custom field type changes, I understand this is not permitted in FLEx, however 2 users could create the same field (unlikely), or a user could just delete a field, and create the same one again with a different type (likely), not sure how this would break if someone actually did it, but it's now a little less broken.

I'm not sure I love the approach taken in CustomPropertyCacheTests to build the fwdata file, I started working on some simple e2e split/unify tests and those are using the same approach. Do we keep doing this? or do we build up the fwdata file some other way for these tests? Maybe via LCM?


AI summary

Summary

MetadataCache is a process-wide singleton. In one Send/Receive the splitter adds the project's custom fields to it, and the unifier (plus any re-split) adds them again. Each add was appended, and GetProperty returns the first match, so:

  • the cache collected duplicates (2 after split + unify, 4 after a second round);
  • a custom field whose type changed in the pull kept its old, stale type.

All custom-field adds now go through one path that replaces an earlier custom definition of the same name:

 FdoClassInfo
   AddProperty(prop)
-    _directProperties.Add(prop)
+    if prop.IsCustomProperty
+      AddOrReplaceCustomProperty(prop)   # replace same-named custom prop; never touch a model prop
+    else
+      _directProperties.Add(prop)

 MetadataCache.AddCustomPropInfo(className, prop)   # splitter, nester, handlers
-  AddProperty(prop)
+  AddOrReplaceCustomProperty(prop)

 MetadataCache.AddCustomPropInfo(MergeOrder)        # reads .CustomProperties, e.g. post-merge validation
-  if field not already known: add
+  add or replace (the file's definition wins)

 FLExProjectUnifier.WriteOptionalCustomProperties
-  GetClassInfo(cls).AddProperty(prop)
+  AddCustomPropInfo(cls, prop)

[!NOTE]
Custom field type changes are not supported, but they can still happen.
FLEx won't change an existing field's type: the type box in AddCustomFieldDlg is enabled only for new fields, and liblcm's FieldDescription treats Type, Class and Name as unchangeable once created. Renaming only changes the label; the internal name stays the same.
A field can still reach FLEx Bridge with a new type in two ways (from reading the code, not tested in FLEx):

  • Delete and recreate: a new field's internal name comes from its label, so deleting "Rank" and creating a new "Rank" with another type reuses the same name and key.
  • Concurrent creation: two users add a field with the same label on the same class but different types. The 3-way merge of .CustomProperties conflicts on type and one side wins.

This PR only makes the cache use the most recent definition. That makes CustomPropertiesTypeHandlerStrategy.ValidateFile's post-merge refresh of the cache take effect; before, it skipped already-known fields, so it couldn't update a type. Nothing reconciles existing field data written under the old type; that stays out of scope.

Evidence

New tests in CustomPropertyCacheTests split a small generated .fwdata with custom field LexEntry.Rank (Integer), then unify it.

  • Before:

    SplitThenUnify_CustomFieldIsDefinedOnce          after unify  Expected: 1  But was: 2
    SplitThenUnifyTwice_CustomFieldIsDefinedOnce                  Expected: 1  But was: 4
    Unify_ChangedCustomFieldType_ReplacesEarlierType              Expected: String  But was: Integer
    Unify_ChangedCustomFieldType_ValidatesDataOfTheNewType        Invalid Integer 'Rank' property of class: LexEntry … Wrong number of attributes
    AddCustomPropInfoFromMergeOrder_ChangedCustomFieldType_ReplacesEarlierType  Expected: String  But was: Integer
    

    The validation failure is the downstream symptom: after the field becomes String, CmObjectValidator (used by Chorus post-merge validation) rejects correct String data because the cache still says Integer.

    After: LibFLExBridge-ChorusPluginTests: 523 passed, 0 failed. FLEx-ChorusPluginTests: 6 passed, 0 failed.

Merge Danger

Door: two-way

Cache-only change, no on-disk format change; a revert restores the old behavior.

Blast Radius: narrow

Every caller that adds a custom property to the cache (splitter, unifier, FwdataNester, merge handlers, RepositoryUtility) now gets replace instead of append. One subtle difference: the MergeOrder path used to skip a field whose name matched any property, including inherited model properties. Now it checks only the class's own properties, so a custom field named like a superclass model property would be added alongside it. FLEx shouldn't produce such a name.

Test plan

  • dotnet test src/LibFLExBridge-ChorusPluginTests/LibFLExBridge-ChorusPluginTests.csproj -c Release "-p:SolutionDir=<repo root>\": 523 passed. Without SolutionDir, the Mercurial package doesn't unpack when building a single project and 12 unrelated Mercurial tests fail.
  • Same for FLEx-ChorusPluginTests: 6 passed.
  • Send/Receive a project with custom fields in FLEx and confirm nothing changes.

🤖 Generated with Claude Code


This change is Reviewable

hahn-kev and others added 2 commits November 20, 2026 12:46
One Send/Receive splits and unifies the project in one process, and each
pass added the project's custom fields to the shared MetadataCache again.
The cache collected duplicates, and since lookups return the first match,
a custom field whose type changed in the pull kept its old type. Post-merge
validation then rejected data of the new type.

All custom-field adds now go through one path that replaces an earlier
custom definition of the same name and never touches model properties.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Any caller that adds a custom property directly now gets replace
semantics too, so a future call site can't reintroduce duplicates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Test Results

563 tests  +7   563 ✅ +7   3m 37s ⏱️ -8s
  6 suites ±0     0 💤 ±0 
  6 files   ±0     0 ❌ ±0 

Results for commit 71ce1bb. ± Comparison against base commit c51e79b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant