Skip to content

Commit 6003273

Browse files
fix(providers): bind Bun updates to the selected installation
1 parent 34610f3 commit 6003273

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

‎packages/provider-core/src/server/maintenanceResolver.test.ts‎

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -869,29 +869,73 @@ it.layer(
869869
{ skip: !symlinksSupported || windowsHost },
870870
);
871871

872-
it.effect.skipIf(windowsHost)(
873-
"switches to bun updates when the resolved binary lives in bun's global bin",
872+
it.effect.skipIf(windowsHost || !symlinksSupported)(
873+
"binds bun updates to the selected global installation and rejects unproven ownership",
874874
() =>
875875
Effect.gen(function* () {
876876
const tempDir = yield* makeTempDir("t3-bun-capabilities");
877-
const bunBinDir = NodePath.join(tempDir, ".bun", "bin");
878-
writeExecutable(NodePath.join(bunBinDir, "package-tool"));
879-
880-
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
881-
packageToolUpdate,
882-
{
883-
binaryPath: "package-tool",
884-
env: { PATH: bunBinDir },
885-
},
886-
).pipe(
887-
Effect.provideService(HostProcess.Platform, "darwin"),
888-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
877+
const bunBinDir = NodePath.join(tempDir, "selected", ".bun", "bin");
878+
const globalDir = NodePath.join(tempDir, "selected", ".bun", "install", "global");
879+
const packagePath = NodePath.join(
880+
globalDir,
881+
"node_modules",
882+
"@example",
883+
"package-tool",
884+
"bin",
885+
"package-tool.js",
889886
);
887+
writeExecutable(packagePath);
888+
const maintenancePath = NodePath.join(bunBinDir, "package-tool");
889+
NodeFS.mkdirSync(bunBinDir, { recursive: true });
890+
NodeFS.symlinkSync(packagePath, maintenancePath);
891+
const runtimePath = NodePath.join(tempDir, "default", ".bun", "bin", "package-tool");
892+
writeExecutable(runtimePath);
893+
const env = {
894+
PATH: NodePath.dirname(runtimePath),
895+
BUN_INSTALL_GLOBAL_DIR: NodePath.join(tempDir, "default", ".bun", "install", "global"),
896+
BUN_INSTALL_BIN: NodePath.dirname(runtimePath),
897+
};
898+
899+
const resolve = () =>
900+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
901+
binaryPath: runtimePath,
902+
maintenanceBinaryPath: maintenancePath,
903+
env,
904+
}).pipe(
905+
Effect.provideService(HostProcess.Platform, "darwin"),
906+
Effect.provideService(HostProcess.Environment, env),
907+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
908+
);
890909

910+
const capabilities = yield* resolve();
891911
expect(capabilities.update).toMatchObject({
892912
command: "bun i -g @example/package-tool@latest",
893-
lockKey: "bun-global",
913+
lockKey: `bun-global:${normalizeCommandPath(globalDir)}`,
914+
env: { BUN_INSTALL_GLOBAL_DIR: globalDir, BUN_INSTALL_BIN: bunBinDir },
894915
});
916+
expect(makeTargetedProviderUpdateAction(capabilities, "2.0.0")).toMatchObject({
917+
args: ["i", "-g", "@example/[email protected]"],
918+
env: capabilities.update?.env,
919+
});
920+
921+
for (const target of [
922+
NodePath.join(globalDir, "node_modules", "@example", "other-package", "bin", "tool"),
923+
NodePath.join(
924+
tempDir,
925+
"project",
926+
"node_modules",
927+
"@example",
928+
"package-tool",
929+
"bin",
930+
"tool",
931+
),
932+
maintenancePath,
933+
]) {
934+
NodeFS.unlinkSync(maintenancePath);
935+
writeExecutable(target);
936+
if (target !== maintenancePath) NodeFS.symlinkSync(target, maintenancePath);
937+
expect(yield* resolve()).toMatchObject({ update: null, installPath: target });
938+
}
895939
}),
896940
);
897941

‎packages/provider-core/src/server/maintenanceResolver.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function makeTargetedProviderUpdateAction(
162162
const packageName = capabilities.packageName;
163163
if (!update || !packageName) return null;
164164
if (
165-
!/^(?:npm-global:|bun-global$|pnpm-global$|vite-plus-global$|yarn-global$|volta$)/.test(
165+
!/^(?:npm-global:|bun-global:|pnpm-global$|vite-plus-global$|yarn-global$|volta$)/.test(
166166
update.lockKey,
167167
)
168168
)
@@ -489,12 +489,29 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
489489
});
490490
}
491491
if (commandPaths.some(isBunGlobalCommandPath)) {
492+
const path = yield* Path.Path;
493+
const binDir = path.dirname(context.resolvedCommandPath);
494+
if (!normalizeCommandPath(binDir).endsWith("/.bun/bin")) return manual;
495+
const fileSystem = yield* FileSystem.FileSystem;
496+
const globalDir = yield* fileSystem
497+
.realPath(path.join(binDir, "..", "install", "global"))
498+
.pipe(Effect.orElseSucceed(() => null));
499+
// A bin entry alone may be a wrapper or a link into a local project.
500+
if (
501+
!globalDir ||
502+
!path
503+
.relative(globalDir, context.realCommandPath)
504+
.replaceAll("\\", "/")
505+
.startsWith(`node_modules/${packageName}/`)
506+
)
507+
return manual;
492508
return makeProviderMaintenanceCapabilities({
493509
provider: definition.provider,
494510
packageName,
495511
updateExecutable: "bun",
496512
updateArgs: ["i", "-g", `${packageName}@latest`],
497-
updateLockKey: "bun-global",
513+
updateLockKey: `bun-global:${normalizeCommandPath(globalDir)}`,
514+
env: { BUN_INSTALL_GLOBAL_DIR: globalDir, BUN_INSTALL_BIN: binDir },
498515
});
499516
}
500517
if (commandPaths.some(isPnpmGlobalCommandPath)) {

0 commit comments

Comments
 (0)