Repository navigation
feat: let a facet assembly reference nothing it was shaped from - #442
Open
kevin-mcmanus wants to merge 1 commit into
Open
kevin-mcmanus wants to merge 1 commit into
kevin-mcmanus wants to merge 1 commit into
Conversation
A facet in a contract or domain project drags that project's dependency on the source layer into the compiled output, which is what makes Facet awkward in a layered solution (Tim-Maes#143). Three separate things do it: the typeof inside [Facet], the generated mapping members that name the source type, and nested property types copied across as the source's own. Two properties close the first two: Facet_GenerateMappings=false omits the *.Mappings.g.cs half Facet_EmitAttributeMetadata=false keeps Facet's attributes, and so the typeof inside them, out of metadata The attributes are now [Conditional(FACET_KEEP_ATTRIBUTES)]. The generator reads them from the semantic model either way, so generation is unaffected; only the emitted blob changes, and the compiler then elides the references that only those arguments were keeping alive. Facet.Attributes.targets defines the symbol by default, so every existing project is unchanged — and it lives in that package rather than in Facet because the [Conditional] is on a type there: a project referencing the attributes without the generator would otherwise lose its attributes with no targets file through which to ask for them back. NestedFacets already closes the third, but silently. Two new diagnostics cover the ways this quietly fails, both only where the project has declared its facets to be contracts: FAC116 a generated property still typed from the source layer FAC117 a mapping hook that cannot run, because the members that would call it are the suppressed ones Facet.Tests.ExternalLib doubles as a contract assembly so the claim is tested rather than argued: ContractAssemblyReferenceTests reads the emitted metadata and asserts no source-typed constructor, no Projection, no FromSource and no Facet.Attributes reference. Verified end to end through the built package: a contract project whose facets include a nested one compiles to an assembly whose only reference is System.Runtime. This removes the mapping rather than relocating it — a partial type cannot span assemblies, so the mapping has to be something free-standing, in a project that references both layers. Facet's facets suit that already: their members are settable and the projection is an object initializer. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Oct 12, 2026
kevin-mcmanus
marked this pull request as ready for review
October 12, 2026 14:46
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.
This addresses most of what #143 asks for, and the part that works without the second generator. Please refer to the issue for the background and motivation.
A facet in a contract or domain project drags that project's dependency on the source layer into the compiled output. Three independent things do it: the
typeofin[Facet], which is serialised into the attribute blob; the generated*.Mappings.g.cs, where the constructor,FromSourceandProjectionall name the source type; and nested property types, which are copied with the source's type rather than the facet's.Two properties close the first two:
The attributes become
[Conditional(FACET_KEEP_ATTRIBUTES)]. The generator reads them from the semantic model either way, so generation is unaffected — only the emitted blob changes, and the compiler then elides the references that only those arguments were keeping alive.Facet.Attributes.targetsdefines the symbol by default, so every existing project is unchanged. It lives in that package rather than inFaceton purpose: the[Conditional]is on a type there, so a project referencing the attributes without the generator would otherwise lose its attributes with no targets file through which to ask for them back.NestedFacetsalready closed the third — what it didn't do was say so when you'd forgotten. Two new warnings cover the silent failures, both only whereFacet_GenerateMappings=false:[Facet]attribute, whereNestedFacetsgoes.Configuration,BeforeMapConfiguration,AfterMapConfigurationorToSourceConfigurationon a facet whose mappings are suppressed. The members that would call the hook are never generated, so it compiles and does nothing.Facet.Tests.ExternalLibnow doubles as a contract assembly, so the result is tested rather than argued:ContractAssemblyReferenceTestsreads the emitted metadata and asserts no source-typed constructor, noProjection, noFromSource, and noFacet.Attributesreference. Separately, I built a contract project whose facets include a nested one against the packed output, and its only assembly reference wasSystem.Runtime.What it doesn't do
It removes the mapping rather than relocating it. The mapping has to be written in a project that references both layers. Facet's facets already suit that — their members are settable and the generated projection is an object initializer, so a hand-written static class can both populate one and project to one in a query.
docs/22_ContractAssemblies.mdshows it. Generating that mapper is a follow-up PR (#443), which can be taken or left independently of this one.Trade-offs
Facet.Extensions' single-generic overloads stop working in an assembly that opts out:ToFacet<TTarget>(object),ToSource<TSource>(object)andBackTo<TSource>(object)(which delegates toToSource) all resolve the type by readingFacet.FacetAttributeat runtime. The two-generic overloads are unaffected — they go through the generated constructor and read no attribute — so the workaround is the variant the library already recommends for performance. In practice a contract assembly has no mapping members either, so it's mapped from outside regardless.DefineConstantsand generator configuration apply to the whole compilation.Checklist
dotnet testlocally.