Skip to content

ApiCompat.Task's ValidateAssemblies fails when multi-targeting with UseArtifactsOutput=true #52873

@k-cup-vm

Description

@k-cup-vm

Describe the bug

Microsoft.DotNet.ApiCompat.Task.ValidateAssembliesTask fails to create a semaphore file because it expects a folder to be present which does not exist. This causes build to fail.

The task expects obj/Project/<configuration>/ to exist. This is not a problem when dual-targetting on its own, but it is when UseArtifactsOutput is set to true.
Due to the Pivot instead of obj/Project/Release/net8.0 folder structure we get obj/Project/release_net8.0/. The expected directory does not exist, and thus semaphore file creation fails. Current workaround is to add an MSBuild task to ensure the expected directory exists.

To Reproduce

ApiCompat.Tasks issue example.zip

Attached sample project, but essentially:

  1. Create directory.build.props in solution/project's folder and set UseArtifactsOutput to true (having it in build.props may not be necessary)
  2. In the project:
    2.1. Add a reference to Microsoft.DotNet.ApiCompat.Task
    2.2. Set <TargetFrameworks> to ensure multi-targeting
    2.3. Add: <ApiCompatValidateAssemblies>true</ApiCompatValidateAssemblies>
    2.4. Set: <ApiCompatContractAssembly> to a valid value so it doesn't fail the initial check. In my case I just used the built .dll itself:
$(ArtifactsPath)\bin\ApiCompatReproducible\$(Configuration.ToLowerInvariant())_$(TargetFramework.ToLowerInvariant())\ApiCompatReproducible.dll

Exceptions (if any)

I believe the exception originates from here:
https://github.com/dotnet/sdk/blob/v10.0.102/src/Compatibility/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs#L167

The "Microsoft.DotNet.ApiCompat.Task.ValidateAssembliesTask" task failed unexpectedly.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\vs-sandbox\ApiCompat.Tasks issue example\artifacts\obj\ApiCompatReproducible\debug\Microsoft.DotNet.ApiCompat.ValidateAssemblies.Common.semaphore'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at Microsoft.DotNet.ApiCompat.Task.ValidateAssembliesTask.ExecuteCore() in /_/src/sdk/src/Compatibility/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs:line 171
   at Microsoft.NET.Build.Tasks.TaskBase.Execute() in /_/src/sdk/src/Tasks/Common/TaskBase.cs:line 45
   at Microsoft.DotNet.ApiCompat.Task.ValidateAssembliesTask.Execute() in /_/src/sdk/src/Compatibility/ApiCompat/Microsoft.DotNet.ApiCompat.Task/ValidateAssembliesTask.cs:line 125
   at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

Further technical details

This has been 'fixed' (with a workaround) for ValidatePackages, but not ValidateAssemblies here: #31906 (review)

details of dotnet --info

Host: Version: 10.0.2 Architecture: x64 Commit: 4452502459

.NET SDKs installed:
8.0.417 [C:\Program Files\dotnet\sdk]
9.0.308 [C:\Program Files\dotnet\sdk]
9.0.309 [C:\Program Files\dotnet\sdk]
10.0.102 [C:\Program Files\dotnet\sdk]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions