diff --git a/src/UniGetUI.PackageEngine.Managers.Bun/Bun.cs b/src/UniGetUI.PackageEngine.Managers.Bun/Bun.cs index bbc885a602..7daedb1b23 100644 --- a/src/UniGetUI.PackageEngine.Managers.Bun/Bun.cs +++ b/src/UniGetUI.PackageEngine.Managers.Bun/Bun.cs @@ -207,7 +207,24 @@ public override IReadOnlyList FindCandidateExecutableFiles() } internal static string GetGlobalPackagesDirectory(string userProfile) - => Path.Combine(userProfile, ".bun", "install", "global"); + => GetGlobalPackagesDirectory(userProfile, Environment.GetEnvironmentVariable); + + // Mirrors Bun's own lookup order: BUN_INSTALL_GLOBAL_DIR, then $BUN_INSTALL/install/global, + // then ~/.bun/install/global. + internal static string GetGlobalPackagesDirectory( + string userProfile, + Func getEnvironmentVariable) + { + string? globalDir = getEnvironmentVariable("BUN_INSTALL_GLOBAL_DIR"); + if (!string.IsNullOrEmpty(globalDir)) + return globalDir; + + string? bunInstall = getEnvironmentVariable("BUN_INSTALL"); + if (!string.IsNullOrEmpty(bunInstall)) + return Path.Combine(bunInstall, "install", "global"); + + return Path.Combine(userProfile, ".bun", "install", "global"); + } internal static bool HasGlobalPackageManifest(string globalDir) => Directory.Exists(globalDir) && File.Exists(Path.Combine(globalDir, "package.json")); diff --git a/src/UniGetUI.PackageEngine.Tests/BunManagerTests.cs b/src/UniGetUI.PackageEngine.Tests/BunManagerTests.cs index 913b231cef..5592c5a2ce 100644 --- a/src/UniGetUI.PackageEngine.Tests/BunManagerTests.cs +++ b/src/UniGetUI.PackageEngine.Tests/BunManagerTests.cs @@ -757,6 +757,42 @@ public void GetAvailableUpdatesReturnsEmptyWhenGlobalManifestIsMissing() } } + /// + /// Tests the global packages directory follows Bun's lookup order for its environment variables. + /// + [Fact] + public void GetGlobalPackagesDirectoryHonorsBunEnvironmentVariables() + { + string userProfile = Path.Combine("home", "user"); + string globalDir = Path.Combine("custom", "global"); + string bunInstall = Path.Combine("scoop", "persist", "bun"); + + Assert.Equal( + globalDir, + Bun.GetGlobalPackagesDirectory( + userProfile, + name => name switch + { + "BUN_INSTALL_GLOBAL_DIR" => globalDir, + "BUN_INSTALL" => bunInstall, + _ => null, + })); + + Assert.Equal( + Path.Combine(bunInstall, "install", "global"), + Bun.GetGlobalPackagesDirectory( + userProfile, + name => name == "BUN_INSTALL" ? bunInstall : null)); + + Assert.Equal( + Path.Combine(userProfile, ".bun", "install", "global"), + Bun.GetGlobalPackagesDirectory(userProfile, _ => null)); + + Assert.Equal( + Path.Combine(userProfile, ".bun", "install", "global"), + Bun.GetGlobalPackagesDirectory(userProfile, _ => "")); + } + /// /// Tests Bun install location defaults to the global Bun node_modules directory. ///