Skip to content

fix: always emit the Add<Assembly>Services extension - #236

Merged
MarcelRoozekrans merged 1 commit into
mainfrom
fix/empty-add-services
Oct 28, 2026
Merged

MarcelRoozekrans merged 1 commit into
mainfrom
fix/empty-add-services

Conversation

@MarcelRoozekrans

Copy link
Copy Markdown
Collaborator

Summary

Problem: Add<AssemblyName>Services() was generated only when the assembly had at least one [Transient], [Scoped] or [Singleton] type. GenerateSources returned early on zero services, so a composition root that already called the extension failed with CS1061, for example in layered solutions or after the last service is removed.

Before: no extension and no container classes for an assembly without attributed types.
After: the extension is always emitted and returns services when there is nothing to register. With ZeroAlloc.Inject.Container referenced, the empty service provider classes are emitted as well.

Changes

  • Remove the early return in GenerateSources. The diagnostics path was already separate and is untouched, so decorator diagnostics behave as before.
  • Add generator tests for an assembly with no attributed types, with and without the Container, including a check that the generated output compiles.
  • Rename the old NoAttributedClasses_GeneratesNothing test, which asserted nothing about output, to match the new behaviour.
  • There are no snapshot files in this repo, and the full solution builds and passes, so no other updates were needed.

Tests

  • main: 781 passed, 0 failed
  • branch: 784 passed, 0 failed

Behaviour change

An assembly with no attributed services now gets a public Add<Assembly>Services extension, and with the Container referenced, empty service provider classes. Previously nothing was generated.

Fixes #235

GenerateSources returned early when the assembly had no attributed services, so no extension
and no container classes were emitted. A composition root that already called
Add<Assembly>Services then failed with CS1061 until a placeholder service was added, and the
same break happened when the last service was removed.

The early return is gone. With zero services the extension just returns services, and with
ZeroAlloc.Inject.Container referenced the empty provider classes are emitted too. The
diagnostics path is separate and unchanged, so decorator diagnostics still work.

Tests cover an assembly with no attributed types, with and without the container, and compile
the generated output.
@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

@MarcelRoozekrans
MarcelRoozekrans merged commit 5309af1 into main Oct 28, 2026
11 checks passed
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.

Add…Services is not generated when the assembly has no attributed services

1 participant