Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Fiber from "effect/Fiber";
7import * as Path from "effect/Path";
8import * as PlatformError from "effect/PlatformError";
9import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
10
11import * as ProcessRunner from "../processRunner.ts";
12import {
13 ensurePinnedRuntimeInstalled,
14 pinnedRuntimePaths,
15 PinnedRuntimeInstallError,
16} from "./pinnedRuntime.ts";
17
18const successfulRunner = (fs: FileSystem.FileSystem, path: Path.Path) =>
19 ProcessRunner.ProcessRunner.of({
20 run: (input) =>
21 Effect.gen(function* () {
22 const prefixIndex = input.args.indexOf("--prefix");
23 const stagingDir = input.args[prefixIndex + 1];
24 if (stagingDir === undefined) return yield* Effect.die("missing npm --prefix");
25 const entry = path.join(stagingDir, "node_modules", "t3", "dist", "bin.mjs");
26 yield* fs.makeDirectory(path.dirname(entry), { recursive: true }).pipe(Effect.orDie);
27 yield* fs.writeFileString(entry, "export {};\n").pipe(Effect.orDie);
28 return {
29 stdout: "",
30 stderr: "",
31 code: ChildProcessSpawner.ExitCode(0),
32 timedOut: false,
33 stdoutTruncated: false,
34 stderrTruncated: false,
35 stdoutInvalidUtf8: false,
36 stderrInvalidUtf8: false,
37 };
38 }),
39 });
40
41it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
42 it.effect("installs through pnpm when its Node runtime has no npm executable", () =>
43 Effect.gen(function* () {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-pnpm-" });
47 const commands: Array<ProcessRunner.ProcessRunInput> = [];
48 const install = successfulRunner(fs, path);
49 const paths = yield* ensurePinnedRuntimeInstalled({
50 baseDir,
51 version: "1.2.3",
52 fs,
53 path,
54 runner: ProcessRunner.ProcessRunner.of({
55 run: (input) => {
56 commands.push(input);
57 return input.command === "npm"
58 ? Effect.fail(
59 new ProcessRunner.ProcessSpawnError({
60 command: "npm",
61 argumentCount: input.args.length,
62 cause: PlatformError.systemError({
63 _tag: "NotFound",
64 module: "ChildProcess",
65 method: "spawn",
66 }),
67 }),
68 )
69 : install.run(input);
70 },
71 }),
72 validate: (staging) =>
73 fs.exists(staging.entryPath).pipe(
74 Effect.flatMap((exists) => (exists ? Effect.void : Effect.die("missing runtime"))),
75 Effect.orDie,
76 ),
77 });
78 assert.deepEqual(
79 commands.map((command) => command.command),
80 ["npm", "pnpm"],
81 );
82 assert.deepEqual(commands[1]!.args, ["--package=npm@11", "dlx", "npm", ...commands[0]!.args]);
83 assert.equal(yield* fs.readFileString(paths.sentinelPath), "1.2.3\n");
84 }),
85 );
86
87 it.effect("does not try a different installer for npm permission failures", () =>
88 Effect.gen(function* () {
89 const fs = yield* FileSystem.FileSystem;
90 const path = yield* Path.Path;
91 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-permission-" });
92 const commands: string[] = [];
93 yield* ensurePinnedRuntimeInstalled({
94 baseDir,
95 version: "1.2.3",
96 fs,
97 path,
98 runner: ProcessRunner.ProcessRunner.of({
99 run: (input) => {
100 commands.push(input.command);
101 return Effect.fail(
102 new ProcessRunner.ProcessSpawnError({
103 command: input.command,
104 argumentCount: input.args.length,
105 cause: PlatformError.systemError({
106 _tag: "PermissionDenied",
107 module: "ChildProcess",
108 method: "spawn",
109 }),
110 }),
111 );
112 },
113 }),
114 validate: () => Effect.die("must not validate a failed install"),
115 }).pipe(Effect.flip);
116 assert.deepEqual(commands, ["npm"]);
117 }),
118 );
119
120 it.effect("validates a staging tree before atomically publishing it", () =>
121 Effect.gen(function* () {
122 const fs = yield* FileSystem.FileSystem;
123 const path = yield* Path.Path;
124 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
125 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
126 let validatedDirectory = "";
127
128 const installed = yield* ensurePinnedRuntimeInstalled({
129 baseDir,
130 version: "1.2.3",
131 fs,
132 path,
133 runner: successfulRunner(fs, path),
134 validate: (staging) =>
135 Effect.gen(function* () {
136 validatedDirectory = staging.versionDir;
137 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
138 assert.isTrue(yield* fs.exists(staging.entryPath));
139 }).pipe(Effect.orDie),
140 });
141
142 assert.notEqual(validatedDirectory, finalPaths.versionDir);
143 assert.deepEqual(installed, finalPaths);
144 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
145 assert.equal(yield* fs.readFileString(finalPaths.sentinelPath), "1.2.3\n");
146 }),
147 );
148
149 it.effect("removes staging and leaves no final runtime when validation fails", () =>
150 Effect.gen(function* () {
151 const fs = yield* FileSystem.FileSystem;
152 const path = yield* Path.Path;
153 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
154 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
155
156 yield* ensurePinnedRuntimeInstalled({
157 baseDir,
158 version: "1.2.3",
159 fs,
160 path,
161 runner: successfulRunner(fs, path),
162 validate: () =>
163 Effect.fail(new PinnedRuntimeInstallError({ step: "validating the staged runtime" })),
164 }).pipe(Effect.flip);
165
166 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
167 assert.deepEqual(
168 (yield* fs.readDirectory(path.dirname(finalPaths.versionDir))).filter((entry) =>
169 entry.startsWith(".staging-"),
170 ),
171 [],
172 );
173 }),
174 );
175
176 it.effect("replaces an incomplete pinned runtime", () =>
177 Effect.gen(function* () {
178 const fs = yield* FileSystem.FileSystem;
179 const path = yield* Path.Path;
180 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
181 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
182 yield* fs.makeDirectory(finalPaths.versionDir, { recursive: true });
183 yield* fs.writeFileString(path.join(finalPaths.versionDir, "partial"), "incomplete\n");
184
185 yield* ensurePinnedRuntimeInstalled({
186 baseDir,
187 version: "1.2.3",
188 fs,
189 path,
190 runner: successfulRunner(fs, path),
191 validate: () => Effect.void,
192 });
193
194 assert.isFalse(yield* fs.exists(path.join(finalPaths.versionDir, "partial")));
195 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
196 }),
197 );
198
199 it.effect("preserves a completed runtime when validation fails", () =>
200 Effect.gen(function* () {
201 const fs = yield* FileSystem.FileSystem;
202 const path = yield* Path.Path;
203 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
204 const finalPaths = pinnedRuntimePaths(path, baseDir, "1.2.3");
205 yield* fs.makeDirectory(path.dirname(finalPaths.entryPath), { recursive: true });
206 yield* fs.writeFileString(finalPaths.entryPath, "broken\n");
207 yield* fs.writeFileString(finalPaths.sentinelPath, "1.2.3\n");
208
209 let validations = 0;
210 yield* ensurePinnedRuntimeInstalled({
211 baseDir,
212 version: "1.2.3",
213 fs,
214 path,
215 runner: successfulRunner(fs, path),
216 validate: (paths) =>
217 Effect.gen(function* () {
218 validations += 1;
219 const source = yield* fs.readFileString(paths.entryPath).pipe(Effect.orDie);
220 if (source === "broken\n") {
221 return yield* new PinnedRuntimeInstallError({ step: "validating the runtime" });
222 }
223 }),
224 }).pipe(Effect.flip);
225
226 assert.equal(validations, 1);
227 assert.equal(yield* fs.readFileString(finalPaths.entryPath), "broken\n");
228 }),
229 );
230
231 it.effect("removes staging when installation is interrupted", () =>
232 Effect.gen(function* () {
233 const fs = yield* FileSystem.FileSystem;
234 const path = yield* Path.Path;
235 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-interrupt-" });
236 const started = yield* Deferred.make<void>();
237 const runner = ProcessRunner.ProcessRunner.of({
238 run: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
239 });
240 const install = yield* ensurePinnedRuntimeInstalled({
241 baseDir,
242 version: "1.2.3",
243 fs,
244 path,
245 runner,
246 validate: () => Effect.void,
247 }).pipe(Effect.forkScoped);
248
249 yield* Deferred.await(started);
250 yield* Fiber.interrupt(install);
251 const versionsDir = path.join(baseDir, "runtime", "versions");
252 assert.deepEqual(yield* fs.readDirectory(versionsDir), []);
253 }),
254 );
255});