dotnet/wpf · error · FormatException

SR.UnmatchedLocComment

Error message

SR.UnmatchedLocComment

What it means

ParsePropertyComments throws this FormatException when scanning ends with an unterminated construct: either a property name was read but no comment value followed, or buffered tokens remain without a complete PropertyName(Value) pair. Every entry in a Localization.Comments string must be a matched 'PropertyName(Comment)' pair.

Solutions

  1. Close every comment with a matching ')' for its opening '('.
  2. Give every listed property a parenthesized value; remove bare property names that have no comment.
  3. Check for line-wrapping or trimming in resource files that dropped trailing parentheses.
  4. Escape any literal '(' or ')' inside the comment text so nesting balance is correct.

Example fix

// before
[Localization.Comments]
Text(The window title
// after
[Localization.Comments]
Text(The window title)
Defensive patterns

Strategy: validation

Validate before calling

static bool HasBalancedPairs(string v)
{
    if (string.IsNullOrWhiteSpace(v)) return false;
    bool escaped = false; int open = 0;
    foreach (char c in v)
    {
        if (escaped) { escaped = false; continue; }
        if (c == '\\') { escaped = true; continue; }
        if (c == '(') open++;
        else if (c == ')') open--;
    }
    return open == 0 && !escaped;
}

Try / catch

try { ParsePropertyComments(input); } catch (FormatException) { log.Error($"Unmatched Property(Comment) pair in '{input}'"); }

Prevention

When it happens

Trigger: Input like 'Text(' with no closing ')', or a bare property name 'Text' with no parenthesized comment at all, when the Comments attribute is parsed (via pairs / LookupAndSetLocalizabilityAttribute).

Common situations: Truncated attribute values after a copy/paste, multi-line comment strings where the closing parenthesis was lost across line breaks, and hand-written [Localization.Comments] entries missing the value part.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/926c9511528fc5e1. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/LocalizationComments.cs:192

                            // comment
                            if (input[i] == EscapeChar && !escaped)
                            {
                                escaped = true;
                            }
                            else
                            {
                                tokenBuffer.Append(input[i]);
                                escaped = false;
                            }
                        }
                    }
                }
            }

            if (currentPair.PropertyName != null || tokenBuffer.Length != 0)
            {
                // unmatched PropertyName and Value pair
                throw new FormatException(SR.Format(SR.UnmatchedLocComment, input));
            }

            return tokens.ToArray();
        }

        //------------------------------
        // Private methods
        //------------------------------
        private static LocalizabilityGroup LookupAndSetLocalizabilityAttribute(string input)
        {
            //
            // For Localization.Attributes, values are seperated by spaces, e.g.
            // $Content (Modifiable Readable)
            // We are breaking the content and convert it to corresponding enum values.
            //
            LocalizabilityGroup attributeGroup = new LocalizabilityGroup();

            StringBuilder builder = new StringBuilder();

View on GitHub (pinned to 81131a70a4)