dotnet / dotnet/sdk

`dotnet publish` fails when `IsPublishable` is `false` for `Microsoft.NET.Sdk.Web`-based projects: `System.IO.DirectoryNotFoundException`

Open
#49,997 2 comments 1 reaction 0 assignees View on GitHub
Area-WebSDK untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

# Summary

Attempting to `dotnet publish` a solution containing a `Microsoft.NET.Sdk.Web`-based project with `IsPublishable` set to `false` causes a `DirectoryNotFoundException` exception to be thrown during the `TransformWebConfig` `MSBuild` task, halting the operation.

# Repro steps

1. Create a new .NET API project
2. Add the `IsPublishable` property to the `.csproj`, set to `false`
3. Run `dotnet publish`
4. Observe the exception

# Context

We have a large solution (~180 projects) that we are in the process of migrating from a raw, manual deployment process, to a more modern approach based on `dotnet publish`.

Because this is a migration effort, we want to do it incrementally, moving one "leaf" project at a time to the new system.

To make the migration incremental, we added `False` to the main `Directory.Build.props`, and then `True` on just the projects that are being migrated (currently, 3 out of the ~180 total projects). The idea is that we can run `dotnet publish` on the entire solution, and still control which projects will have their contents published.

Right after making that configuration, I noticed one of our API projects that uses the `Microsoft.NET.Sdk.Web` SDK, started to throw an exception when `dotnet publish` was executed on the solution, _even though `IsPublishable` was set to `False` for it_ (it is one of the projects that have not yet been migrated).

```
C:\Users\JulianoLealGoncalves\source\repos\WebAppPublishTest>dotnet publish
Restore complete (0.6s)
WebAppPublishTest failed with 1 error(s) (0.0s)
C:\Program Files\dotnet\sdk\9.0.301\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:
The "TransformWebConfig" task failed unexpectedly.
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\JulianoLealGoncalves\source\repo
s\WebAppPublishTest\WebAppPublishTest\bin\Release\net8.0\publish\web.config'.
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, Fil
eShare share, FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare
share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare sha
re, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, Fil
eShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Microsoft.NET.Sdk.Publish.Tasks.TransformWebConfig.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLogging
Context taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)

Build failed with 1 error(s) in 0.9s
```

The exception comes from the `TransformWebConfig` `MSBuild` task, which inspects the target `publish` folder for the `web.config` file to transform. For some reason, this task is running even if publish is explicitly disabled, and then it crashes since the publish folder was not created at all. I'm not familiar with the whole publish targets, but this feels incorrect: if a project is set to not be published, it should not attempt to execute any `web.config` transformations on it since that is part of the publish procedure.

From what I can tell, this is specific to the `.Web` SDK. Testing with the `.Worker` SDK doesn't reproduce the problem.

# Workarounds

Adding the following property to the project resolves the issue (but of course feels like a hack):
```xml
True
```

# Related

- https://github.com/Dotnet-Boxed/Templates/issues/818
- https://github.com/dotnet/docs/issues/13365
- https://github.com/dotnet/aspire/issues/3766

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.