Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions util/SeederUtility/Configuration/GlobalSettingsFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Settings;
using System.Reflection;
using Bit.Core.Settings;
using Microsoft.Extensions.Configuration;

namespace Bit.SeederUtility.Configuration;
Expand All @@ -14,10 +15,15 @@ public static GlobalSettings GlobalSettings

private static GlobalSettings LoadGlobalSettings()
{
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Directory.GetCurrentDirectory();
Comment on lines +18 to +19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 SUGGESTED: AppContext.BaseDirectory resolves the same directory without reflection or a null fallback

Details and rationale
Suggested change
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Directory.GetCurrentDirectory();
var directory = AppContext.BaseDirectory;

Assembly.GetExecutingAssembly().Location returns an empty string under single-file publish, so the ?? Directory.GetCurrentDirectory() branch silently falls back to the CWD-based behavior this PR is fixing. This project sets IncludeAllContentForSelfExtract, which suggests self-extract publishing is at least contemplated; AppContext.BaseDirectory points at the extraction/app directory in that mode and never returns null, so the fallback becomes unnecessary.

It also matches the existing pattern in the repo — see test/IntegrationTestCommon/Factories/WebApplicationFactoryBase.cs:183, which pairs SetBasePath(AppContext.BaseDirectory) for this same purpose.


var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.SetBasePath(directory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json",
optional: true, reloadOnChange: true)
.AddUserSecrets("bitwarden-seeder-utility")
.AddEnvironmentVariables();

Expand Down
9 changes: 9 additions & 0 deletions util/SeederUtility/SeederUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
<PackageReference Include="Spectre.Console" Version="[0.55.2]" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading