sdk icon indicating copy to clipboard operation
sdk copied to clipboard

dotnet publish fails for web project when web.config is read-only

Open geirs73 opened this issue 5 years ago • 8 comments

Publishing a web project fails when a web.config exists and is readonly, when running Windows. This causes problems, because I sometimes use Perforce as a code repository, and Perforce works best if I let it keep its unchanged files in read-only state.

Steps to reproduce:

> mkdir PublishProblem
> pushd PublishProblem
> dotnet new mvc
> dotnet publish 
[No errors]
> dotnet new webconfig
> dotnet publish
[No errors]
> attrib +R web.config
> dotnet publish
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\PublishProblem.dll
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\PublishProblem.Views.dll
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\publish\
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018: The "TransformWebConfig" task failed unexpectedly. [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018: System.UnauthorizedAccessException: Access to the path 'C:\Development\PublishProblem\bin\Debug\net5.0\publish\web.config' is denied. [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.FileStream..ctor(String path, FileMode mode) [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.NET.Sdk.Publish.Tasks.TransformWebConfig.Execute() [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Development\PublishProblem\PublishProblem.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\Development\PublishProblem\PublishProblem.csproj]
>

Dotnet info:

> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.101
 Commit:    d05174dc5a

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.101\

Host (useful for support):
  Version: 5.0.1
  Commit:  b02e13abab

.NET SDKs installed:
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.503 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.508 [C:\Program Files\dotnet\sdk]
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.1.515 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  5.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

However, also reproduced with 5.0.100 earlier.

I have made a Windows-only workaround that I paste into Directory.Build.targets until the problem is fixed: https://gist.github.com/geirs73/e57539d8e8e20bcd98ad31d4758e0d67 It will remove the read-only attribute for a short while, while publishing, and then restore it.

> rmdir bin /s /q
> dotnet publish
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\PublishProblem.dll
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\PublishProblem.Views.dll
  PublishProblem -> C:\Development\PublishProblem\bin\Debug\net5.0\publish\
  FIX: web.config is set to writable
  FIX: web.config is restored to read only
>

My guess is that the read-only flag is not cleared when a build step or the transform task is first copying the web.config to the publish directory, and then it croaks when trying to write to that web.config file under transform.

geirs73 avatar Dec 21 '20 08:12 geirs73

The root cause of my issue may be located inside TransformWebConfig.cs, after further study and some experimentation on a local clone of 5.0.2xx branch. I changed Execute() so that it removes any ReadOnly attribute on the copied web.config file in the publish directory, and got a "works on my machine". I do not know enough about the internal structure of this project to do a proper fix that is testable and guaranteed to work on any platform, but I hope it is helpful anyway.

diff --git a/src/WebSdk/Publish/Tasks/Tasks/TransformWebConfig.cs b/src/WebSdk/Publish/Tasks/Tasks/TransformWebConfig.cs
index 07b957dbe..56bb5bea9 100644
--- a/src/WebSdk/Publish/Tasks/Tasks/TransformWebConfig.cs
+++ b/src/WebSdk/Publish/Tasks/Tasks/TransformWebConfig.cs
@@ -89,6 +89,13 @@ public override bool Execute()
                 if (File.Exists(projectWebConfigPath))
                 {
                     File.Copy(projectWebConfigPath, publishWebConfigPath, true);
+                    FileAttributes publishWebConfigAttr = File.GetAttributes(publishWebConfigPath);
+
+                    if ((publishWebConfigAttr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
+                    {
+                        publishWebConfigAttr = publishWebConfigAttr & ~FileAttributes.ReadOnly;
+                        File.SetAttributes(publishWebConfigPath, publishWebConfigAttr);
+                    }
                 }
                 else
                 {

geirs73 avatar Dec 26 '20 15:12 geirs73

This is also an issue for repositories that still use TFVC, where files are set to read-only during a "get latest" operation. I'm using a similar workaround in my build pipeline, where I am removing the read-only attribute from the web.config file prior to calling dotnet publish.

JamesQMurphy avatar Jan 15 '21 15:01 JamesQMurphy

FYI @vijayrkn

joeloff avatar Feb 17 '21 20:02 joeloff

You can add below command in prebuild of the project (assume file name is test or you can set to full project folder). This way you don't need to change any sdk files/ build pipeline and below change is applicable to the only project which is in error. call attrib -r "$(ProjectDir)test.config" /s

girishbhatt84 avatar Feb 24 '21 06:02 girishbhatt84

Did anyone ever pinpoint what introduced this bug? Is it an update to "dotnet publish"? Something in an update to MSBuild?

I just started experiencing this problem, and my project has been in TFS using TFVC for years, I routinely do a "Get Latest" with the overwrite switch checked\turned-on, and I've been using VS 2019 and updating it regularly since it came out.

Seems odd that this was discovered in 2020, but I didn't start experiencing it till last week. And the only thing I've done between my last publish of my project and the problem first occurring last week is that I updated VS 2022.

Can we get a fix for this please? Not all of us are fortunate enough to be using git with TFS. The TFS project I'm working on is using TFVC, and I'm not going to migrate the source to a git repo and lose five years of source control history.

theBoringCoder avatar Nov 04 '22 17:11 theBoringCoder

You can add below command in prebuild of the project (assume file name is test or you can set to full project folder). This way you don't need to change any sdk files/ build pipeline and below change is applicable to the only project which is in error. call attrib -r "$(ProjectDir)test.config" /s

That's a nice work-around. It'd be even nicer if the dev team would fix the problem.

theBoringCoder avatar Nov 04 '22 17:11 theBoringCoder

I have got this error after migrating my project from .NET 6 to .NET 7. The aforementioned workaround has helped, but I would rather you fixed the issue.

LarinLive avatar Nov 30 '22 16:11 LarinLive

I have got this error after migrating my project from .NET 6 to .NET 7. The aforementioned workaround has helped, but I would rather you fixed the issue.

Same issue, just update visual studio, getting same error

Edo90 avatar Dec 07 '22 20:12 Edo90