Conversation
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;'.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Copy-pasting the README quickstart into a top-level
Program.cs(types in the global namespace, nonamespacedeclaration) crashed the mapper source generator:Root cause:
ContainingNamespace.ToDisplayString()returns the literal text<global namespace>for types at global scope. It was stored inDbContextInfo.Namespace/EntityInfo.Namespace/NestedTypeInfo.Namespaceand flowed into generator hint names and emitted source. Beyond the hint-nameArgumentException, the emitted file would also contain invalid C# (namespace <global namespace>.X_Mappers,using <global namespace>;).Fix:
SyntaxHelper.GetNamespaceOrEmpty()maps the global namespace tostring.Emptyat all three assignment sites.IsNullOrEmpty(Namespace)guards (filter-namespace fallback,[DocumentMapper]pipeline,usingcollection) actually work for global types.{ns}.{safeName}_Mappersnamespace and the{ns}.{safeName}.Mappers.g.cshint name — now omit the empty prefix, and the DbContext partial class is emitted without anamespacewrapper when the namespace is empty.usingdirectives to the README quickstart (BLite.Bson,BLite.Core.Collections,BLite.Core.Query) since the snippet otherwise doesn't compile —DocumentCollection<,>/UpsertResult<>/ToListAsyncdon't live inBLite.Core.Test plan
Program.cswithDbContext+ entity in the global namespace — generator crashed with CS8785 before, compiles and runs correctly after (dotnet runreturns inserted rows)dotnet build tests/BLite.Tests— all existing namespaced contexts still generate correctlydotnet test --filter InsertAsync|Upsert— 15/15 passGenerated with Devin