feat: support building web projects with the dotnet CLI (Core MSBuild)#103
feat: support building web projects with the dotnet CLI (Core MSBuild)#103ehasis wants to merge 1 commit into
Conversation
…Build) Make `dotnet build` work for ASP.NET 4.x projects when Visual Studio or the Build Tools (web workload) is installed; the full-MSBuild path is unchanged. - Locate Microsoft.WebApplication.targets from the installed VS/Build Tools when the default path is missing (fixes MSB4019), with a clear SYSWEB001 error if none is found. - Skip MvcBuildViews under Core MSBuild (AspNetCompiler is unavailable, MSB4803) instead of failing Release. - Re-add the System.Web reference after the Microsoft.NET.Sdk import for Core MSBuild, where the earlier reference is dropped from @(Reference).
|
@ehasis Thanks so much for this PR - there is lots of good stuff in this. I like the idea of there being less friction to using this SDK with the dotnet cli, although I think that the 'automagical' fixes for some of the items should be more intentional on behalf of the user. Could you create separate issues and PRs for
<PropertyGroup>
<MvcBuildViews Condition="'$(MSBuildRuntimeType)' == 'Core'">false</MvcBuildViews>
</PropertyGroup>
For https://github.com/CZEMacLeod/C3D.Extensions.Aspire/blob/main/Set-WebApplicationTargetsPath.ps1 Putting the auto-resolver under a mode flag - e.g. default to no action, option to skip if not found, option to resolve automatically might be a good addtion though. |
Support building with
dotnet(Core MSBuild) when Visual Studio / Build Tools is installedSummary
The SDK could only build projects with the full Visual Studio MSBuild. Running
dotnet buildon a project that usesMSBuild.SDK.SystemWebfailed early with a cryptic error. This PR makes thedotnetCLI tooling work for ASP.NET 4.x web projects whenever Visual Studio or the Visual Studio Build Tools (with the web workload) is installed, while leaving the full-MSBuild / VS path completely unchanged.All changes are in a single file:
src/MSBuild.SDK.SystemWeb/Sdk/Sdk.targets.Problem
Under
dotnet(Core MSBuild), three independent things broke:Microsoft.WebApplication.targetsnot found —$(MSBuildExtensionsPath32)points at the .NET SDK instead of a VS install, so the import resolved to a non‑existent path:MvcBuildViews(Release) — theAspNetCompilertask does not exist in Core MSBuild (error MSB4803), so Release builds failed.System.Webnot resolved — the framework<Reference>contributed byCommon.DefaultPackages.targetsis added before theMicrosoft.NET.Sdktargets and is silently dropped from@(Reference)under Core MSBuild, soSystem.Webtypes didn't compile (other framework refs likeSystem.dllresolved fine).Changes (
Sdk.targets)Microsoft.WebApplication.targetsautomatically. When the default path doesn't exist (thedotnetcase), resolve an installed Visual Studio / Build Tools copy (any edition, incl.BuildTools) and use it.<Import>is processed before items are evaluated, so an item glob can't influence it.<root>\Microsoft Visual Studio\<year>\<edition>). No recursive search — a recursiveGetDirectories(..., AllDirectories)throws on an ACL‑restricted subfolder and would break evaluation for every build.!Exists(...), so an explicit$(VSToolsPath)/$(WebApplicationsTargetPath)and normal VS / full‑MSBuild builds are untouched and pay no lookup cost.MSB4019. The web-targets import is now conditional; if no VS/Build Tools is found, a clearSYSWEB001error explains how to fix it (install the web workload, use a Developer prompt, or setVSToolsPath).MvcBuildViewsunder Core MSBuild. Precompile views only under full MSBuild; underdotnetemit an informational message (not a warning, so it never tripsTreatWarningsAsErrors) instead of failing —dotnet build -c Releasestill produces output.System.Webafter theMicrosoft.NET.Sdkimport, for Core MSBuild only. This makes the reference survive into@(Reference)underdotnet. Gated on'$(MSBuildRuntimeType)' == 'Core'so full MSBuild keeps using the existingCommon.DefaultPackages.targetsreference and there is no duplicate (MSB3105).No changes to
Common.DefaultPackages.targetsor the RazorLibrary SDK.Behavior matrix
dotnet buildSystem.Web(Debug)MSB3105)SYSWEB001error-p:VSToolsPath=...Testing
Validated against the sample web apps on Windows with VS 18 + .NET SDK 10, building via both the
dotnetCLI and the full Visual StudioMSBuild.exe:samples/ExampleEmptyWebApplication—dotnet buildDebug & Release; full MSBuild Release.samples/ExampleFullWebApplication(VB, usesSystem.Web) —dotnet buildDebug; full MSBuild Release (confirmsSystem.Webresolves and noMSB3105duplicate).SYSWEB001.Out of scope
The RazorLibrary SDK is not changed here. It has separate, pre-existing incompatibilities with Core MSBuild (e.g.
MSB4113onEnableDefaultItems,MSB4022onMicrosoftNETBuildTasksAssembly) rooted in its conditionalMicrosoft.NET.Sdkimport structure; making it build underdotnetis a larger change. Its full-MSBuild behavior is unaffected by this PR.Notes for reviewers
System.Webdeliberately stays inCommon.DefaultPackages.targets(used by full MSBuild and by RazorLibrary) and is only re-declared in the main SDK for Core MSBuild — see the inline comment explaining the duplicate-avoidance gate.$(VSToolsPath)(or$(WebApplicationsTargetPath)) explicitly.