Skip to content

A whitespace-only SourceFileTemplate.Namespace writes namespace ; (CS1001), though blank usings are skipped #203

Closed

Description

@matt-edmondson

What's wrong

WriteFileScopedNamespace checks string.IsNullOrEmpty(namespaceName) (CodeBlocker/CodeBlockerExtensions.cs:86). WriteUsings and the template lists skip null, empty and whitespace entries (IsNullOrWhiteSpace, since #189, #191 and #200), but the namespace check was never changed to match.

Repro (net10.0, at c739886)

var file = new SourceFileTemplate { Namespace = " " };
file.Usings.Add(" "); file.Usings.Add("System");
file.Classes.Add(new ClassTemplate { Name = "C" });
using var w = CodeBlocker.Create(); w.AddSourceFile(file); Console.Write(w);

Observed: the first line is namespace ;, followed by using System; and the class. The blank using is correctly skipped.

Expected: no namespace line, the same as for Namespace = "".

Why it matters

The generated file doesn't compile (CS1001: Identifier expected). A namespace that comes from configuration or user input with a stray space hits this. It is also inconsistent with how the library handles blank entries everywhere else.

Suggested fix

Use string.IsNullOrWhiteSpace at CodeBlockerExtensions.cs:86, and add a test asserting that Namespace = " " writes no namespace line.

Activity

  1. added
    bug
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions