Skip to content

fix(generator): support DbContext/entities declared in the global namespace - #152

Open
IAFahim wants to merge 2 commits into
EntglDb:mainfrom
IAFahim:fix/global-namespace-generator-crash
Open

IAFahim wants to merge 2 commits into
EntglDb:mainfrom
IAFahim:fix/global-namespace-generator-crash

Conversation

@IAFahim

@IAFahim IAFahim commented Oct 1, 2026

Copy link
Copy Markdown

Summary

Copy-pasting the README quickstart into a top-level Program.cs (types in the global namespace, no namespace declaration) crashed the mapper source generator:

warning CS8785: Generator 'MapperGenerator' failed to generate source.
Exception was of type 'ArgumentException' with message 'The hintName
'<global namespace>.MyDbContext_Program.Mappers.g.cs' contains an
invalid character '<' at position 0.

Root cause: ContainingNamespace.ToDisplayString() returns the literal text <global namespace> for types at global scope. It was stored in DbContextInfo.Namespace / EntityInfo.Namespace / NestedTypeInfo.Namespace and flowed into generator hint names and emitted source. Beyond the hint-name ArgumentException, the emitted file would also contain invalid C# (namespace <global namespace>.X_Mappers, using <global namespace>;).

Fix:

  • New SyntaxHelper.GetNamespaceOrEmpty() maps the global namespace to string.Empty at all three assignment sites.
  • This makes the existing IsNullOrEmpty(Namespace) guards (filter-namespace fallback, [DocumentMapper] pipeline, using collection) actually work for global types.
  • The two emit sites that had no guard — the {ns}.{safeName}_Mappers namespace and the {ns}.{safeName}.Mappers.g.cs hint name — now omit the empty prefix, and the DbContext partial class is emitted without a namespace wrapper when the namespace is empty.
  • Also adds the missing using directives to the README quickstart (BLite.Bson, BLite.Core.Collections, BLite.Core.Query) since the snippet otherwise doesn't compile — DocumentCollection<,>/UpsertResult<>/ToListAsync don't live in BLite.Core.

Test plan

  • Repro project: top-level Program.cs with DbContext + entity in the global namespace — generator crashed with CS8785 before, compiles and runs correctly after (dotnet run returns inserted rows)
  • dotnet build tests/BLite.Tests — all existing namespaced contexts still generate correctly
  • dotnet test --filter InsertAsync|Upsert — 15/15 pass
  • Full suite + wasm/AOT paths not exercised

Generated with Devin

ContainingNamespace.ToDisplayString() returns the literal string
"<global namespace>" for types declared at global scope (e.g. a
DbContext defined in a top-level Program.cs). It was stored in
DbContextInfo.Namespace/EntityInfo.Namespace and flowed into emitted
source and generator hint names, producing:

  warning CS8785: Generator 'MapperGenerator' failed to generate
  source ... The hintName '<global namespace>.MyDbContext_Program.
  Mappers.g.cs' contains an invalid character '<'

and, had the hint name not thrown first, invalid emitted C# like
"namespace <global namespace>.X" / "using <global namespace>;".

Map the global namespace to string.Empty via a new
SyntaxHelper.GetNamespaceOrEmpty() helper at all three assignment
sites, emit the DbContext partial class without a namespace wrapper
when the namespace is empty, and omit the empty prefix from the
mapper namespace and hint name. Existing IsNullOrEmpty(Namespace)
guards (filter namespace fallback, [DocumentMapper] pipeline) now
behave as intended for global types too.
The Basic Usage sample uses ObjectId (BLite.Bson),
DocumentCollection<TId,T>/UpsertResult<T> (BLite.Core.Collections)
and async LINQ extensions (BLite.Core.Query), none of which are in
scope with only 'using BLite.Core;'.
Copilot AI balanced review requested due to automatic review settings October 1, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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.

2 participants