dotnet/wpf · warning · FormatException

SR.InvalidLocCommentTarget

Error message

SR.InvalidLocCommentTarget

What it means

When parsing localization comments ( Loc comments in XAML), ParsePropertyComments throws this FormatException if a localization target token starts with an unescaped comment-start character. Target names in the comment must be valid identifiers or escaped; an unescaped start character makes the target unparseable.

Solutions

  1. Fix the Loc comment so the target is a valid identifier without a leading unescaped start char
  2. Escape special characters in the target name with the escape character
  3. Check the comment against the documented Loc comment syntax
  4. Regenerate the comments with the localization tooling instead of hand-editing

Example fix

// before
<!-- Loc $$foo --> (unescaped start char in target)
// after
<!-- Loc foo -->
Defensive patterns

Strategy: validation

Validate before calling

// Validate Loc comment syntax before localization processing
if (System.Text.RegularExpressions.Regex.IsMatch(comment, @"\$[^\\$]")) throw new FormatException("Unescaped start char in Loc comment target");

Try / catch

try { comments = ParseLocComments(xamlComment); } catch (FormatException ex) { log.LogWarning($"Skipping malformed Loc comment: {ex.Message}"); }

Prevention

When it happens

Trigger: A Loc comment like [Loc]$[... where the target begins with the comment-start/escape character without escaping — e.g. malformed comment syntax [$...] written incorrectly, or target names containing special characters unescaped.

Common situations: Hand-written Loc comments with typos in the bracket syntax; localization targets whose names contain special characters (like '[') that were not escaped; tools generating comments with wrong escaping.

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/f6f03042de1f202b. Report an issue: GitHub.

Appendix: source

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

                        }

                        // else ignore whitespace at the beginning of the PropertyName name
                    }
                    else
                    {
                        if (input[i] == CommentStart && !escaped)
                        {
                            if (i > 0)
                            {
                                // terminate the PropertyName by an unescaped CommentStart char
                                currentPair.PropertyName = tokenBuffer.ToString();
                                tokenBuffer.Clear();
                                i--; // put back this char and continue
                            }
                            else
                            {
                                // can't begin with unescaped comment start char.
                                throw new FormatException(SR.Format(SR.InvalidLocCommentTarget, input));
                            }
                        }
                        else if (input[i] == EscapeChar && !escaped)
                        {
                            escaped = true;
                        }
                        else
                        {
                            tokenBuffer.Append(input[i]);
                            escaped = false;
                        }
                    }
                }
                else
                {
                    // parsing the "Value" part
                    if (tokenBuffer.Length == 0)
                    {

View on GitHub (pinned to 81131a70a4)