dotnet / docker-tools

This is a repo to house some common tools for our various docker repos.
MIT License
124 stars 46 forks source link

MEF import failures should cause exceptions #1273

Closed mthalman closed 5 months ago

mthalman commented 6 months ago

The changes from https://github.com/dotnet/docker-tools/pull/1272 caused an issue with the satisfying the MEF imports for BuildCommand. These show up in the output (see below) but do not cause an exception when loading Image Builder. This makes it more difficult to diagnose the issue. You don't know anything is wrong until an attempt to use the affected command occurs. And for some commands, that might not even happen in the validation pipeline. We should update Image Builder such that these import failures cause an exception that causes Image Builder to fail immediately.

System.ComponentModel.Composition Warning: 1 : The ComposablePartDefinition 'Microsoft.DotNet.ImageBuilder.Commands.BuildCommand' has been rejected. The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No exports were found that match the constraint: 
    ContractName    Microsoft.DotNet.ImageBuilder.IDockerService
    RequiredTypeIdentity    Microsoft.DotNet.ImageBuilder.IDockerService

Resulting in: Cannot set import 'Microsoft.DotNet.ImageBuilder.Commands.BuildCommand..ctor (Parameter="dockerService", ContractName="Microsoft.DotNet.ImageBuilder.IDockerService")' on part 'Microsoft.DotNet.ImageBuilder.Commands.BuildCommand'.
Element: Microsoft.DotNet.ImageBuilder.Commands.BuildCommand..ctor (Parameter="dockerService", ContractName="Microsoft.DotNet.ImageBuilder.IDockerService") -->  Microsoft.DotNet.ImageBuilder.Commands.BuildCommand -->  DirectoryCatalog (Path="C:\repos\docker-tools\src\Microsoft.DotNet.ImageBuilder\src\bin\Debug\net8.0")
mthalman commented 6 months ago

In addition to fixing this, there should be a test added which executes this code path. This will allow us to discover any MEF import/export issues via the tests instead of later when executing the validation pipeline.

lbussell commented 6 months ago

The same outcome could also be accomplished by switching to a compile-time dependency injection library, e.g. Jab, StrongInject, or Pure.DI. That would cause a build error instead of a runtime error.