dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.65k stars 1.05k forks source link

Dotnet Build failing with error CS2012: Cannot open .dll for writing, access denied #11593

Open devr24 opened 4 years ago

devr24 commented 4 years ago

My self-hosted build agent started reporting an error during dotnet build yesterday. Full command:

C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src>"C:\Program Files\dotnet\dotnet.exe" build C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\Cloud.Core.Extensions.Nest.csproj "-dl:CentralLogger,\"C:\agent\vsts-agent-win-x64-2.166.4\_work\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.168.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"*ForwardingLogger,\"C:\agent\vsts-agent-win-x64-2.166.4\_work\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.168.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"" --configuration release

Expected build to work fine. Actual output:

Microsoft (R) Build Engine version 16.6.0-preview-20126-02+13cfe7fc5 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
##vso[task.logdetail id=8d38fe20-6aaa-4bc0-a56a-6659df9b8b93;parentid=;name=CSC;type=Build;starttime=2020-05-08T16:07:55.6837318Z;state=InProgress;]
##vso[task.logissue type=Error;sourcepath=CSC;linenumber=0;columnnumber=0;code=CS2012;]Cannot open 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' for writing -- 'Access to the path 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' is denied.'
##vso[task.logdetail id=8d38fe20-6aaa-4bc0-a56a-6659df9b8b93;parentid=;type=Build;result=Failed;finishtime=2020-05-08T16:07:55.6892238Z;progress=100;state=Completed;parentid=;name=;]
CSC : error CS2012: Cannot open 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' for writing -- 'Access to the path 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' is denied.' [C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\Cloud.Core.Extensions.Nest.csproj]

Build FAILED.

CSC : error CS2012: Cannot open 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' for writing -- 'Access to the path 'C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\obj\release\netstandard2.1\Cloud.Core.Extensions.Nest.dll' is denied.' [C:\agent\vsts-agent-win-x64-2.166.4\_work\19\s\src\Cloud.Core.Extensions.Nest\Cloud.Core.Extensions.Nest.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.83

Project file for csproj is defined as:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
    <Company></Company>
    <Version>3.1</Version>
    <PackageIconUrl></PackageIconUrl>
    <Copyright></Copyright>
    <Description>A set of extension methods for the Elasticsearch Nest client</Description>
    <PackageTags>Nest Elasticsearch Extension Cloud.Core .net-Core Core aspnetcore</PackageTags>
    <SignAssembly>false</SignAssembly>
    <ProjectGuid>{9BCDBAA0-141F-404B-864B-74A35A476AAD}</ProjectGuid>
    <RepositoryUrl></RepositoryUrl>
    <RepositoryType>Private</RepositoryType>
    <Authors></Authors>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="NEST" Version="6.3.1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Polly" Version="7.2.0" />
  </ItemGroup>
</Project>

Any ideas why? I added extra permissions to the folder for Reading/Writing that the agent uses, no joy. I also changed from Network Service to a higher privileged account and still get the issue. Restarted machine multiple times.

This was working up until yesterday. Thanks for any pointers in advance.

hisuwh commented 7 months ago

@devr24 did you ever get to the bottom of this? We're having the same issue

ttsukune commented 6 months ago

If you usually have antivirus software notifications turned off, it's worth checking whether the specific binary is not being recognized as a virus or a suspicious file.

As software development becomes more complex and you perform frequent builds, some "intelligent" antivirus programs may start identifying the output as a virus or suspicious software and removing it. If the main components of your software once become the target, the necessary binaries to build may be removed immediately with an alert. However, if you have turned off antivirus notifications (like Avast), you might not be aware that this is happening.

Additionally, many questions online related to CS2012 or similar build error issues are likely linked to this antivirus behavior and this is not VS / .NET issue. If this assumption is correct, then it's natural that "rebooting has no effect," as they mentioned.