Skip to content

Commit 77095ec

Browse files
committed
Fixed loading of appsettings.Development.json during check-deps runs
1 parent 6a23de4 commit 77095ec

5 files changed

Lines changed: 33 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- Fixed loading of `appsettings.Development.json` during `check-deps` runs by forcing the Development environment for dependency validation.
1617
- Updated Scrutor to v6.1.0.
1718
- Updated Microsoft.Extensions packages to latest patch versions.
1819

Neolution.DotNet.Console.UnitTests/DotNetConsoleBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class DotNetConsoleBuilderTests
1717
/// <summary>
1818
/// The argument string for the internal check-deps command
1919
/// </summary>
20-
private const string CheckDependenciesArgumentString = "check-deps";
20+
private const string CheckDependenciesArgumentString = DotNetConsoleDefaults.CheckDependenciesCommand;
2121

2222
/// <summary>
2323
/// Given a mistyped verb, when a default verb is defined, then should throw on console building.

Neolution.DotNet.Console/DotNetConsoleBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public IDotNetConsole Build()
8585

8686
if (this.checkDependencies)
8787
{
88-
// Use development environment before building because that's where ValidateScopes and ValidateOnBuild are enabled.
88+
// Ensure development environment is used for dependency checking
89+
// Note: The environment should already be set to Development during CreateConsoleEnvironment
90+
// but we explicitly set it here as well to ensure ValidateScopes and ValidateOnBuild are enabled.
8991
this.hostBuilder.UseEnvironment("Development");
9092
this.hostBuilder.Build();
9193

@@ -144,7 +146,7 @@ internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Ty
144146
var consoleBuilder = new DotNetConsoleBuilder(builder, parsedArguments, environment, configuration);
145147

146148
// Determine if this is a check-deps run: only DI validation should run
147-
if (args.Length == 1 && string.Equals(args[0], "check-deps", StringComparison.OrdinalIgnoreCase))
149+
if (DotNetConsoleDefaults.IsCheckDependenciesRun(args))
148150
{
149151
consoleBuilder.checkDependencies = true;
150152
return consoleBuilder;

Neolution.DotNet.Console/DotNetConsoleDefaults.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
/// </summary>
1313
internal static class DotNetConsoleDefaults
1414
{
15+
/// <summary>
16+
/// The command argument used to trigger dependency validation.
17+
/// </summary>
18+
internal const string CheckDependenciesCommand = "check-deps";
19+
20+
/// <summary>
21+
/// Determines if the given arguments represent a check-deps run.
22+
/// </summary>
23+
/// <param name="args">The command line arguments.</param>
24+
/// <returns>True if this is a check-deps run, false otherwise.</returns>
25+
public static bool IsCheckDependenciesRun(string[] args)
26+
{
27+
return args.Length == 1 && string.Equals(args[0], CheckDependenciesCommand, StringComparison.OrdinalIgnoreCase);
28+
}
29+
1530
/// <summary>
1631
/// Creates the console environment.
1732
/// </summary>
@@ -27,9 +42,17 @@ internal static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args)
2742
// The apps root directory is where the appsettings.json are located
2843
var appRootDirectory = AppContext.BaseDirectory;
2944

45+
// Check if this is a check-deps run - if so, always use Development environment
46+
var isCheckDepsRun = IsCheckDependenciesRun(args);
47+
48+
// Default to Production for normal runs, matching ASP.NET Core behavior
49+
// For check-deps, always use Development to ensure appsettings.Development.json is loaded
50+
// Environment can be overridden via DOTNET_ENVIRONMENT or command line arguments
51+
var defaultEnvironment = isCheckDepsRun ? Environments.Development : Environments.Production;
52+
3053
return new DotNetConsoleEnvironment
3154
{
32-
EnvironmentName = configuration[HostDefaults.EnvironmentKey] ?? Environments.Production,
55+
EnvironmentName = isCheckDepsRun ? Environments.Development : (configuration[HostDefaults.EnvironmentKey] ?? defaultEnvironment),
3356
ApplicationName = AppDomain.CurrentDomain.FriendlyName,
3457
ContentRootPath = appRootDirectory,
3558
ContentRootFileProvider = new PhysicalFileProvider(appRootDirectory),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Neolution.DotNet.Console.UnitTests")]

0 commit comments

Comments
 (0)