Skip to content

Commit 94f5fbe

Browse files
fix(server): preserve host environment in maintenance probe fallback
1 parent 4b0a493 commit 94f5fbe

2 files changed

Lines changed: 47 additions & 22 deletions

File tree

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2-
import { expect, it } from "@effect/vitest";
2+
import { expect, it, vi } from "@effect/vitest";
33
import * as NodeChildProcess from "node:child_process";
44
import * as NodeFS from "node:fs";
55
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -624,6 +624,50 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
624624
}),
625625
);
626626

627+
it.effect.skipIf(windowsHost || !symlinksSupported)(
628+
"uses the cleaned host for omitted probe environments and preserves supplied partial maps",
629+
() =>
630+
Effect.gen(function* () {
631+
const tempDir = yield* makeTempDir("t3-maintenance-environment");
632+
const link = linkIntoPackage(tempDir, "package-tool", [
633+
"pnpm",
634+
"global",
635+
"5",
636+
"node_modules",
637+
"@example",
638+
"package-tool",
639+
]);
640+
const binDir = NodePath.dirname(link);
641+
const pnpmPath = NodePath.join(binDir, "pnpm");
642+
writeExecutable(pnpmPath);
643+
NodeFS.writeFileSync(
644+
pnpmPath,
645+
'#!/bin/sh\n[ "$HOME" = "$T3_MAINTENANCE_HOME" ] && [ -n "$HOME" ] && [ -z "${APPDIR+x}${ELECTRON_RUN_AS_NODE+x}" ] || exit 1\nprintf "11.1.3\\n"\n',
646+
);
647+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
648+
vi.stubEnv("HOME", tempDir);
649+
vi.stubEnv("T3_MAINTENANCE_HOME", tempDir);
650+
vi.stubEnv("APPDIR", NodePath.join(tempDir, "appimage"));
651+
vi.stubEnv("ELECTRON_RUN_AS_NODE", "1");
652+
vi.stubEnv("PATH", `${tempDir}/appimage/bin:${binDir}`);
653+
654+
const inherited = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
655+
binaryPath: "package-tool",
656+
});
657+
expect(inherited.update?.command).toBe(
658+
"pnpm add -g --allow-build=@example/package-tool @example/package-tool@latest",
659+
);
660+
661+
const env = { PATH: binDir };
662+
const explicit = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
663+
binaryPath: "package-tool",
664+
env,
665+
});
666+
expect(explicit.update?.command).toBe("pnpm add -g @example/package-tool@latest");
667+
expect(env).toEqual({ PATH: binDir });
668+
}).pipe(Effect.scoped),
669+
);
670+
627671
it.effect.skipIf(windowsHost)(
628672
"switches to bun updates when the resolved binary lives in bun's global bin",
629673
() =>

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1616
import { causeErrorTag } from "@t3tools/shared/observability";
1717
import { resolveCommandPath, resolveSpawnCommand } from "@t3tools/shared/shell";
1818
import * as Cache from "effect/Cache";
19-
import * as Config from "effect/Config";
2019
import * as DateTime from "effect/DateTime";
2120
import * as Duration from "effect/Duration";
2221
import * as Effect from "effect/Effect";
@@ -31,6 +30,7 @@ import * as ChildProcess from "effect/process/ChildProcess";
3130
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
3231

3332
import { collectUint8StreamText } from "./collectStreamText.ts";
33+
import { mergeProviderInstanceEnvironment } from "./instanceEnvironment.ts";
3434
import * as ProviderLatestVersions from "./ProviderLatestVersions.ts";
3535

3636
const LATEST_VERSION_TIMEOUT_MS = 4_000;
@@ -41,25 +41,6 @@ const PROVIDER_UPDATE_ACTION_TOAST_MESSAGE = "Install the update now or review p
4141

4242
const MAINTENANCE_CAPABILITIES_CACHE_TTL = Duration.hours(1);
4343

44-
const compactEnv = (input: Record<string, Option.Option<string>>): NodeJS.ProcessEnv =>
45-
Object.fromEntries(
46-
Object.entries(input).flatMap(([key, value]) =>
47-
Option.match(value, {
48-
onNone: () => [],
49-
onSome: (resolved) => [[key, resolved]],
50-
}),
51-
),
52-
);
53-
54-
const CommandLookupEnvConfig = Config.all({
55-
PATH: Config.String("PATH").pipe(Config.option),
56-
Path: Config.String("Path").pipe(Config.option),
57-
path: Config.String("path").pipe(Config.option),
58-
PATHEXT: Config.String("PATHEXT").pipe(Config.option),
59-
}).pipe(Config.map(compactEnv));
60-
61-
const readCommandLookupEnv = CommandLookupEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
62-
6344
export interface ProviderMaintenanceResolutionContext {
6445
readonly binaryPath: string;
6546
readonly resolvedCommandPath: string;
@@ -665,7 +646,7 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
665646
return yield* resolver.resolve(null);
666647
}
667648

668-
const env = options?.env ?? (yield* readCommandLookupEnv);
649+
const env = options?.env ?? (yield* mergeProviderInstanceEnvironment(undefined));
669650
const resolvedCommandPath = yield* resolveCommandPath(binaryPath, { env });
670651
if (!resolvedCommandPath) {
671652
return yield* resolver.resolve(null);

0 commit comments

Comments
 (0)