question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
JamesQMurphycommented, Jan 15, 2021

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.

3reactions
geirs73commented, Dec 26, 2020

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
                 {
Read more comments on GitHub >

github_iconTop Results From Across the Web

The "TransformWebConfig" task failed unexpectedly - ...
For me, there was a permission issue which is why, while publishing, a transformation task was running on web.config file. That file did...
Read more >
Publishing ASP.NET Core app from Visual Studio 2022 to ...
NET Core app from VS 2022 (latest version) to Azure, the publishing fails in case the projects web.config file is not checked out....
Read more >
Asp.net core app with a web.config cannot use IIS Express
The issue is that when you have a web.config file inside your project IIS Express will use that instead of using the one...
Read more >
clear read-only attribute for Web.Config when specified as ...
I am using ISLE-2011 to setup an asp.net app on windows 2008, including setup a new web site and application in IIS. There...
Read more >
Untitled
11th gen si tune dotnet publish fails for web project when web.config is … "TransformWebConfig" task could not be loaded #1073 - GitHub...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found