From fd6ea53731b75c95ca246a006ad26477bd5cf846 Mon Sep 17 00:00:00 2001
From: Stephen Halter <halter73@gmail.com>
Date: Wed, 4 May 2022 13:22:39 -0700
Subject: [PATCH] [release/6.0] Fix diff checks for ContentRoot and WebRoot
 (#41182)

---
 .../src/ConfigureHostBuilder.cs               |  4 ++-
 .../src/ConfigureWebHostBuilder.cs            |  8 +++--
 .../WebApplicationTests.cs                    | 30 +++++++++++++++++++
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/DefaultBuilder/src/ConfigureHostBuilder.cs b/src/DefaultBuilder/src/ConfigureHostBuilder.cs
index fdf8b2d0bc0..87e24d84889 100644
--- a/src/DefaultBuilder/src/ConfigureHostBuilder.cs
+++ b/src/DefaultBuilder/src/ConfigureHostBuilder.cs
@@ -62,6 +62,7 @@ namespace Microsoft.AspNetCore.Builder
             var previousApplicationName = _configuration[HostDefaults.ApplicationKey];
             // Use the real content root so we can compare paths
             var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
+            var previousContentRootConfig = _configuration[HostDefaults.ContentRootKey];
             var previousEnvironment = _configuration[HostDefaults.EnvironmentKey];
 
             // Run these immediately so that they are observable by the imperative code
@@ -74,7 +75,8 @@ namespace Microsoft.AspNetCore.Builder
                 throw new NotSupportedException($"The application name changed from \"{previousApplicationName}\" to \"{_configuration[HostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             }
 
-            if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
+            if (!string.Equals(previousContentRootConfig, _configuration[HostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
+                && !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
             {
                 throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             }
diff --git a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs
index 0a2c482f53f..d0ed6a822ab 100644
--- a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs
+++ b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs
@@ -39,7 +39,9 @@ namespace Microsoft.AspNetCore.Builder
         public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
         {
             var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
+            var previousContentRootConfig = _configuration[WebHostDefaults.ContentRootKey];
             var previousWebRoot = _context.HostingEnvironment.WebRootPath;
+            var previousWebRootConfig = _configuration[WebHostDefaults.WebRootKey];
             var previousApplication = _configuration[WebHostDefaults.ApplicationKey];
             var previousEnvironment = _configuration[WebHostDefaults.EnvironmentKey];
             var previousHostingStartupAssemblies = _configuration[WebHostDefaults.HostingStartupAssembliesKey];
@@ -48,7 +50,8 @@ namespace Microsoft.AspNetCore.Builder
             // Run these immediately so that they are observable by the imperative code
             configureDelegate(_context, _configuration);
 
-            if (!string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
+            if (!string.Equals(previousWebRootConfig, _configuration[WebHostDefaults.WebRootKey], StringComparison.OrdinalIgnoreCase)
+                && !string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
             {
                 // Diasllow changing the web root for consistency with other types.
                 throw new NotSupportedException($"The web root changed from \"{HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot)}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot)}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
@@ -58,7 +61,8 @@ namespace Microsoft.AspNetCore.Builder
                 // Disallow changing any host configuration
                 throw new NotSupportedException($"The application name changed from \"{previousApplication}\" to \"{_configuration[WebHostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             }
-            else if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
+            else if (!string.Equals(previousContentRootConfig, _configuration[WebHostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
+                && !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
             {
                 // Disallow changing any host configuration
                 throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
index 8f724498dde..d487d259493 100644
--- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
+++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
@@ -1686,6 +1686,36 @@ namespace Microsoft.AspNetCore.Tests
             Assert.Equal(string.Empty, responseBody);
         }
 
+        [Fact]
+        public void EmptyAppConfiguration()
+        {
+            var wwwroot = Path.Combine(AppContext.BaseDirectory, "wwwroot");
+            bool createdDirectory = false;
+            if (!Directory.Exists(wwwroot))
+            {
+                createdDirectory = true;
+                Directory.CreateDirectory(wwwroot);
+            }
+    
+            try
+            {
+                var builder = WebApplication.CreateBuilder();
+    
+                builder.WebHost.ConfigureAppConfiguration((ctx, config) => { });
+    
+                using var app = builder.Build();
+                var hostEnv = app.Services.GetRequiredService<Hosting.IWebHostEnvironment>();
+                Assert.Equal(wwwroot, hostEnv.WebRootPath);
+            }
+            finally
+            {
+                if (createdDirectory)
+                {
+                    Directory.Delete(wwwroot);
+                }
+            }
+        }
+
         [Fact]
         public void HostConfigurationNotAffectedByConfiguration()
         {
-- 
GitLab