dotnet / dotnet/aspnetcore

`dotnet publish --no-build` over-publishes from referenced projects

Open
#43,835 4 comments 0 reactions 0 assignees View on GitHub
area-mvc bug
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

## Describe the bug

The Web SDK does not normally publish `*.cshtml` files when using `dotnet publish` (because they're precompiled into the web site assembly and aren't needed). These files are published from referenced projects if `$(NoBuild)` is `true` e.g. using `dotnet publish --no-build`. These files increase deployment payloads and can confuse tests.

### To Reproduce

1. In a clone of the dotnet/aspnetcore repo, run `eng\build.cmd -noBuildManaged -noBuildNative`
2. `dotnet build src\Mvc\test\WebSites\RazorPagesWebSite\`
3. `dotnet publish --no-build src\Mvc\test\WebSites\RazorPagesWebSite\`
4. Observe the artifacts\bin\RazorPagesWebSite\Debug\net7.0\publish\Pages\ClassLibraryPages folder as well as the `*.cshtml` files within it
5. Reset using `rm -Recurse artifacts\bin\RazorPagesWebSite\` and `rm -Recurse artifacts\obj\RazorPagesWebSite\`
6. `dotnet build src\Mvc\test\WebSites\RazorPagesWebSite\`
7. `dotnet publish src\Mvc\test\WebSites\RazorPagesWebSite\`
8. Observe the **lack of** an artifacts\bin\RazorPagesWebSite\Debug\net7.0\publish\Pages folder

### More detail

Using `/p:BuildProjectReferences=false` instead of `--no-build` in step 3 results in the same over-publication.

The ClassLibraryPages folder comes from the referenced src\Mvc\test\WebSites\RazorPagesClassLibrary\ project. Note the many `*.cshtml` files from the RazorPagesWebSite project are **not** published in either (3) or (7).

Moving up a project dependency level and doing the same with the src\Mvc\test\Mvc.FunctionalTests\ project is worse because src\Mvc\test\WebSites\RazorPagesWebSite\ contains a lot of `*.cshtml` files and other referenced projects add to that; all are copied into artifacts\bin\Microsoft.AspNetCore.Mvc.FunctionalTests\Debug\net7.0\publish\Pages and …\Views. This breaks the functional tests because a few test sites use `false` and pick up incorrect / unexpected files.

#### Binary logging

The `AssignTargetPaths` target in the RazorPagesClassLibrary project adds`@(ContentWithTargetPath)` for `Pages\ClassLibraryPages\*.cshtml` in all cases. The items are removed before the `GetCopyToPublishDirectoryItems` target is reached but not when using `--no-build` or `/p:BuildProjectReferences=false`. It's not clear where the removals happen because item removals are not consistently logged.

#### Versions

I've only tested with the ` 7.0.100-preview.2.22102.6` SDK but this is likely a long-standing issue.

dotnet details

``` text
> dotnet --info
.NET SDK (reflecting any global.json):
Version: 7.0.100-preview.2.22102.6
Commit: 6b77f292d5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\dd\dnx\aspnetcore\.dotnet\sdk\7.0.100-preview.2.22102.6\

Host (useful for support):
Version: 7.0.0-preview.2.22103.2
Commit: 0ae04e5b33

.NET SDKs installed:
7.0.100-preview.2.22102.6 [C:\dd\dnx\aspnetcore\.dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.21 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0-dev [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0-preview.2.22080.2 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.21 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0-preview.2.22102.1 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0-preview.2.22102.17 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0-preview.2.22103.2 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 7.0.0-preview.2.22101.13 [C:\dd\dnx\aspnetcore\.dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.