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
19 changes: 18 additions & 1 deletion src/UniGetUI.PackageEngine.Managers.Bun/Bun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,24 @@ public override IReadOnlyList<string> 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<string, string?> 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"));
Expand Down
36 changes: 36 additions & 0 deletions src/UniGetUI.PackageEngine.Tests/BunManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,42 @@ public void GetAvailableUpdatesReturnsEmptyWhenGlobalManifestIsMissing()
}
}

/// <summary>
/// Tests the global packages directory follows Bun's lookup order for its environment variables.
/// </summary>
[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, _ => ""));
}

/// <summary>
/// Tests Bun install location defaults to the global Bun node_modules directory.
/// </summary>
Expand Down