aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

dotnet publish behaviour inconsistent

Open brgrz opened this issue 3 years ago • 7 comments

Describe the bug

dotnet publish includes apsettings.json, appsettings.Development.json and appsettings.Production.json in Release builds and/or disregards the provided environment to the dotnet publish command and copies all the mentioned files (or a random sub selection of them) into the output directory.

This blog post (not mine) describes the issue and possible, imo unwanted, workaround https://slothycode.com/output-configuration-appsettings-json-on-build-publish/

To Reproduce

Any ASP.NET Core project with multiple appsettings files and configurations will do.

Also, besides this inconsistent behaviour I just noticed after playing around with various Build Actions in Visual Studio and Copy to Output Directory settings that dotnet publish will not pick them up or act accordingly.

Also, after changing these settings in Visual Studio suddenly a completely new setting appeared which I've never seen before called CopyToPublishDirectory

Here is a section from csproj for a file that has Build Action set to None and Copy to Output directory set to Never.

	  <None Include="appsettings.Development.json">
	    <CopyToOutputDirectory>Never</CopyToOutputDirectory>
	    <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
	    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
	  </None>

So now there are two, almost identical settings which really baffles me.

Why the PreserveNewest when I explicitely said Never?

Further technical details

.NET SDK (reflecting any global.json): Version: 5.0.403 Commit: bf9d168ac2

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

Host (useful for support): Version: 5.0.12 Commit: 7211aa01b3

.NET SDKs installed: 3.1.101 [C:\Program Files\dotnet\sdk] 3.1.120 [C:\Program Files\dotnet\sdk] 3.1.415 [C:\Program Files\dotnet\sdk] 5.0.104 [C:\Program Files\dotnet\sdk] 5.0.209 [C:\Program Files\dotnet\sdk] 5.0.401 [C:\Program Files\dotnet\sdk] 5.0.403 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

brgrz avatar Dec 05 '21 12:12 brgrz

@vijayrkn I believe the websdk manages the appsettings behavior, correct?

Also, pinging @tmeschter for what sounds like project system property change behavior leading to separate output and publish values in the project file.

marcpopMSFT avatar Dec 08 '21 00:12 marcpopMSFT

@marcpopMSFT I don't think the Project System is involved here--CopyToPublishDirectory is not an item property that we are aware of. @BillHiebert, do you know which tooling sets CopyToPublishDirectory?

tmeschter avatar Dec 08 '21 16:12 tmeschter

@tmeschter presumably this is set somewhere in the sdk target and props files. @vijayrkn - do you know?

BillHiebert avatar Dec 08 '21 17:12 BillHiebert

@BillHiebert I meant that some tooling in VS must be changing the value of the CopyToPublishDirectory property, and I'm not sure where that is happening.

tmeschter avatar Dec 08 '21 17:12 tmeschter

@tmeschter - I suspect it is related to globbing and default metadata. There isn't any code that I could find that sets that msbuild property.

BillHiebert avatar Dec 08 '21 17:12 BillHiebert

Here is the glob that adds the json files to the publish output: https://github.com/dotnet/sdk/blob/main/src/RazorSdk/Sdk/Sdk.Razor.StaticAssets.ProjectSystem.props#L35-L37

The reason it is not copying to the local bin might be because this property is set - ExcludeConfigFilesFromBuildOutput

vijayrkn avatar Jan 26 '22 20:01 vijayrkn

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Jan 27 '22 17:01 ghost

@brgrz did you try out @vijayrkn 's suggestion to set the ExcludeConfigFilesFromBuildOutput setting in your project file to get the desired result?

mkArtakMSFT avatar Oct 26 '22 17:10 mkArtakMSFT

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

ghost avatar Dec 25 '22 20:12 ghost