@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 1,396 lines 50.1 KB
Raw
1 import { spawnSync } from "node:child_process";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7 import { strFromU8, unzipSync } from "fflate";
8 import {
9 hashPackageInstallIdentity,
10 installPackages,
11 installResolvedPackages,
12 matchesPackageRoot,
13 normalizeInstallTargets,
14 npmInstallArgs,
15 npmLockfileArgs,
16 npmPlainInstallArgs,
17 parsePackageRoot,
18 parsePackageRootFromSpecifier,
19 resolveInstallTargets,
20 resolvePackageInstallIdentity,
21 } from "@/Bundle/InstalledPackages";
22 import { zipCode } from "@/Util/zip";
23
24 const integrationEnabled =
25 process.env.ALCHEMY_TEST_LAMBDA_EXTERNAL_PACKAGES === "1" &&
26 spawnSync("npm", ["--version"], { stdio: "ignore" }).status === 0;
27
28 const withLockfileFixture = <A, E, R>(
29 options: {
30 readonly prefix: string;
31 readonly packageJson: unknown;
32 readonly lockfileName: string;
33 readonly lockfileContent: string;
34 readonly workspace?: string;
35 },
36 use: (context: {
37 readonly root: string;
38 readonly cwd: string;
39 readonly fs: FileSystem.FileSystem;
40 readonly path: Path.Path;
41 }) => Effect.Effect<A, E, R>,
42 ) =>
43 Effect.gen(function* () {
44 const fs = yield* FileSystem.FileSystem;
45 const path = yield* Path.Path;
46 const root = yield* fs.makeTempDirectory({ prefix: options.prefix });
47 const cwd = options.workspace === undefined ? root : path.join(root, options.workspace);
48 try {
49 if (cwd !== root) yield* fs.makeDirectory(cwd, { recursive: true });
50 yield* fs.writeFileString(
51 path.join(cwd, "package.json"),
52 JSON.stringify(options.packageJson),
53 );
54 yield* fs.writeFileString(path.join(root, options.lockfileName), options.lockfileContent);
55 return yield* use({ root, cwd, fs, path });
56 } finally {
57 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
58 }
59 });
60
61 describe("Lambda external packages", { tags: ["unit", "local"] }, () => {
62 it("accepts only package roots, not subpaths", () => {
63 expect(parsePackageRoot("sharp")).toBe("sharp");
64 expect(parsePackageRoot("@img/tool")).toBe("@img/tool");
65 expect(parsePackageRoot("sharp/lib/index.js")).toBeUndefined();
66 expect(parsePackageRoot("@img/sharp-linux-arm64/lib")).toBeUndefined();
67 expect(parsePackageRoot("node:fs")).toBeUndefined();
68 expect(parsePackageRoot("./local.js")).toBeUndefined();
69 });
70
71 it("extracts package roots from externalized module ids", () => {
72 expect(parsePackageRootFromSpecifier("heic-convert")).toBe("heic-convert");
73 expect(parsePackageRootFromSpecifier("heic-convert/lib")).toBe("heic-convert");
74 expect(parsePackageRootFromSpecifier("@scope/pkg/subpath")).toBe("@scope/pkg");
75 expect(parsePackageRootFromSpecifier("node:fs")).toBeUndefined();
76 expect(parsePackageRootFromSpecifier("./local.js")).toBeUndefined();
77 expect(parsePackageRootFromSpecifier("")).toBeUndefined();
78 expect(parsePackageRootFromSpecifier("@scope")).toBeUndefined();
79 });
80
81 it("matches package roots and subpath imports", () => {
82 expect(matchesPackageRoot("sharp", "sharp")).toBe(true);
83 expect(matchesPackageRoot("sharp/lib/index.js", "sharp")).toBe(true);
84 expect(matchesPackageRoot("sharpish", "sharp")).toBe(false);
85 expect(matchesPackageRoot("@scope/pkg", "@scope/pkg")).toBe(true);
86 expect(matchesPackageRoot("@scope/pkg/sub", "@scope/pkg")).toBe(true);
87 });
88
89 it.effect("rejects subpaths in build.install", () =>
90 Effect.gen(function* () {
91 const error = yield* Effect.flip(normalizeInstallTargets(["sharp/lib"]));
92 expect(error.message).toMatch(/Invalid package name/);
93 }),
94 );
95
96 it.effect("normalizes and validates install targets", () =>
97 Effect.gen(function* () {
98 expect(yield* normalizeInstallTargets(["sharp", "pg-native"])).toEqual({
99 sharp: "*",
100 "pg-native": "*",
101 });
102 expect(yield* normalizeInstallTargets({ sharp: "^0.33.5" })).toEqual({ sharp: "^0.33.5" });
103 }),
104 );
105
106 it("targets Linux and the Lambda architecture", () => {
107 expect(npmInstallArgs("arm64")).toEqual([
108 "ci",
109 "--force",
110 "--platform=linux",
111 "--os=linux",
112 "--arch=arm64",
113 "--cpu=arm64",
114 "--libc=glibc",
115 ]);
116 expect(npmInstallArgs("x86_64")).toEqual([
117 "ci",
118 "--force",
119 "--platform=linux",
120 "--os=linux",
121 "--arch=x64",
122 "--cpu=x64",
123 "--libc=glibc",
124 ]);
125 expect(npmLockfileArgs("arm64")).toEqual([
126 "install",
127 "--force",
128 "--platform=linux",
129 "--os=linux",
130 "--arch=arm64",
131 "--cpu=arm64",
132 "--libc=glibc",
133 "--package-lock-only",
134 "--ignore-scripts",
135 ]);
136 expect(npmPlainInstallArgs("arm64")).toEqual([
137 "install",
138 "--force",
139 "--platform=linux",
140 "--os=linux",
141 "--arch=arm64",
142 "--cpu=arm64",
143 "--libc=glibc",
144 ]);
145 });
146
147 it.effect("resolves catalog versions from pnpm-workspace.yaml", () =>
148 Effect.gen(function* () {
149 const fs = yield* FileSystem.FileSystem;
150 const path = yield* Path.Path;
151 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-catalog-" });
152
153 try {
154 yield* fs.writeFileString(
155 path.join(root, "pnpm-workspace.yaml"),
156 ["packages:", " - packages/*", "catalog:", " sharp: ^0.33.5"].join("\n"),
157 );
158 yield* fs.writeFileString(
159 path.join(root, "package.json"),
160 JSON.stringify({ dependencies: { sharp: "catalog:" } }),
161 );
162
163 const files = yield* installPackages({
164 cwd: root,
165 install: ["sharp"],
166 architecture: "arm64",
167 runNpmInstall: (directory) =>
168 Effect.gen(function* () {
169 const packageJson = JSON.parse(
170 yield* fs.readFileString(path.join(directory, "package.json")),
171 );
172 expect(packageJson.dependencies.sharp).toBe("^0.33.5");
173 }),
174 });
175
176 expect(files.map((file) => file.path)).toContain("package.json");
177 } finally {
178 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
179 }
180 }).pipe(Effect.provide(NodeServices.layer)),
181 );
182
183 it.effect("resolves named catalog versions from pnpm-workspace.yaml", () =>
184 Effect.gen(function* () {
185 const fs = yield* FileSystem.FileSystem;
186 const path = yield* Path.Path;
187 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-pnpm-named-catalog-" });
188
189 try {
190 yield* fs.writeFileString(
191 path.join(root, "pnpm-workspace.yaml"),
192 ["packages:", " - packages/*", "catalogs:", " native:", " sharp: ^0.34.5"].join(
193 "\n",
194 ),
195 );
196 yield* fs.writeFileString(
197 path.join(root, "package.json"),
198 JSON.stringify({ dependencies: { sharp: "catalog:native" } }),
199 );
200
201 const files = yield* installPackages({
202 cwd: root,
203 install: ["sharp"],
204 architecture: "arm64",
205 runNpmInstall: (directory) =>
206 Effect.gen(function* () {
207 const packageJson = JSON.parse(
208 yield* fs.readFileString(path.join(directory, "package.json")),
209 );
210 expect(packageJson.dependencies.sharp).toBe("^0.34.5");
211 }),
212 });
213
214 expect(files.map((file) => file.path)).toContain("package.json");
215 } finally {
216 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
217 }
218 }).pipe(Effect.provide(NodeServices.layer)),
219 );
220
221 it.effect("resolves catalog versions from Bun workspace metadata", () =>
222 Effect.gen(function* () {
223 const fs = yield* FileSystem.FileSystem;
224 const path = yield* Path.Path;
225 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-bun-catalog-" });
226
227 try {
228 yield* fs.writeFileString(
229 path.join(root, "package.json"),
230 JSON.stringify({
231 workspaces: { packages: ["packages/*"], catalogs: { native: { sharp: "^0.34.5" } } },
232 dependencies: { sharp: "catalog:native" },
233 }),
234 );
235
236 const files = yield* installPackages({
237 cwd: root,
238 install: ["sharp"],
239 architecture: "arm64",
240 runNpmInstall: (directory) =>
241 Effect.gen(function* () {
242 const packageJson = JSON.parse(
243 yield* fs.readFileString(path.join(directory, "package.json")),
244 );
245 expect(packageJson.dependencies.sharp).toBe("^0.34.5");
246 }),
247 });
248
249 expect(files.map((file) => file.path)).toContain("package.json");
250 } finally {
251 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
252 }
253 }).pipe(Effect.provide(NodeServices.layer)),
254 );
255
256 it.effect("falls back to optional and dev dependency versions", () =>
257 Effect.gen(function* () {
258 const fs = yield* FileSystem.FileSystem;
259 const path = yield* Path.Path;
260 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-dependency-sections-" });
261
262 try {
263 yield* fs.writeFileString(
264 path.join(root, "package.json"),
265 JSON.stringify({
266 optionalDependencies: { sharp: "^0.34.5" },
267 devDependencies: { "pg-native": "^3.2.0" },
268 }),
269 );
270
271 const files = yield* installPackages({
272 cwd: root,
273 install: ["sharp", "pg-native"],
274 architecture: "x86_64",
275 runNpmInstall: (directory) =>
276 Effect.gen(function* () {
277 const packageJson = JSON.parse(
278 yield* fs.readFileString(path.join(directory, "package.json")),
279 );
280 expect(packageJson.dependencies).toEqual({ "pg-native": "^3.2.0", sharp: "^0.34.5" });
281 }),
282 });
283
284 expect(files.map((file) => file.path)).toContain("package.json");
285 } finally {
286 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
287 }
288 }).pipe(Effect.provide(NodeServices.layer)),
289 );
290
291 it.effect("rejects workspace and file dependency protocols", () =>
292 Effect.gen(function* () {
293 const fs = yield* FileSystem.FileSystem;
294 const path = yield* Path.Path;
295 const root = yield* fs.makeTempDirectory({
296 prefix: "alchemy-external-incompatible-protocol-",
297 });
298
299 try {
300 yield* fs.writeFileString(
301 path.join(root, "package.json"),
302 JSON.stringify({
303 dependencies: { sharp: "workspace:*", "pg-native": "file:../pg-native" },
304 }),
305 );
306
307 const workspaceError = yield* installPackages({
308 cwd: root,
309 install: ["sharp"],
310 architecture: "arm64",
311 runNpmInstall: () => Effect.void,
312 }).pipe(Effect.flip);
313 expect(workspaceError.message).toContain("workspace:*");
314
315 const fileError = yield* installPackages({
316 cwd: root,
317 install: ["pg-native"],
318 architecture: "arm64",
319 runNpmInstall: () => Effect.void,
320 }).pipe(Effect.flip);
321 expect(fileError.message).toContain("file:../pg-native");
322 } finally {
323 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
324 }
325 }).pipe(Effect.provide(NodeServices.layer)),
326 );
327
328 it.effect("installs into an isolated artifact and returns its files", () =>
329 Effect.gen(function* () {
330 const fs = yield* FileSystem.FileSystem;
331 const path = yield* Path.Path;
332 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-source-" });
333 let installDirectory: string | undefined;
334 const installArgs: Array<ReadonlyArray<string>> = [];
335 let artifactPackageJson: unknown;
336
337 try {
338 yield* fs.writeFileString(
339 path.join(root, "package.json"),
340 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
341 );
342
343 const files = yield* installPackages({
344 cwd: root,
345 install: ["sharp"],
346 architecture: "arm64",
347 runNpmInstall: (directory, args) =>
348 Effect.gen(function* () {
349 installDirectory = directory;
350 installArgs.push(args);
351 const manifest = yield* fs.readFileString(path.join(directory, "package.json"));
352 artifactPackageJson = yield* Effect.try(() => JSON.parse(manifest));
353
354 const sharpRoot = path.join(directory, "node_modules", "sharp");
355 const sharpBinRoot = path.join(sharpRoot, "bin");
356 const binRoot = path.join(directory, "node_modules", ".bin");
357 const binaryRoot = path.join(
358 directory,
359 "node_modules",
360 "@img",
361 "sharp-linux-arm64",
362 "lib",
363 );
364 const libvipsRoot = path.join(
365 directory,
366 "node_modules",
367 "@img",
368 "sharp-libvips-linux-arm64",
369 "lib",
370 );
371 yield* fs.makeDirectory(sharpRoot, { recursive: true });
372 yield* fs.makeDirectory(sharpBinRoot, { recursive: true });
373 yield* fs.makeDirectory(binRoot, { recursive: true });
374 yield* fs.makeDirectory(binaryRoot, { recursive: true });
375 yield* fs.makeDirectory(libvipsRoot, { recursive: true });
376 yield* fs.writeFileString(
377 path.join(sharpRoot, "package.json"),
378 JSON.stringify({ name: "sharp", version: "0.34.5" }),
379 );
380 const executablePath = path.join(sharpBinRoot, "sharp-tool");
381 yield* fs.writeFileString(executablePath, "#!/bin/sh\n");
382 yield* fs.chmod(executablePath, 0o755);
383 yield* fs.symlink("../sharp/bin/sharp-tool", path.join(binRoot, "sharp-tool"));
384 yield* fs.writeFile(
385 path.join(binaryRoot, "sharp.node"),
386 new Uint8Array([0, 1, 2, 3]),
387 );
388 yield* fs.writeFile(
389 path.join(libvipsRoot, "libvips.so"),
390 new Uint8Array([4, 5, 6, 7]),
391 );
392 yield* fs.writeFileString(path.join(directory, "package-lock.json"), "{}");
393 }),
394 });
395
396 expect(artifactPackageJson).toEqual({ private: true, dependencies: { sharp: "^0.34.5" } });
397 // No lockfile in the fixture means nothing to pin, so the install is a
398 // single plain `npm install` instead of the lockfile-generate + ci pair.
399 expect(installArgs).toEqual([npmPlainInstallArgs("arm64")]);
400 expect(files.map((file) => file.path)).toEqual(
401 expect.arrayContaining([
402 "package.json",
403 "package-lock.json",
404 "node_modules/sharp/package.json",
405 "node_modules/sharp/bin/sharp-tool",
406 "node_modules/.bin/sharp-tool",
407 "node_modules/@img/sharp-linux-arm64/lib/sharp.node",
408 "node_modules/@img/sharp-libvips-linux-arm64/lib/libvips.so",
409 ]),
410 );
411 const executable = files.find((file) => file.path === "node_modules/sharp/bin/sharp-tool");
412 const symlink = files.find((file) => file.path === "node_modules/.bin/sharp-tool");
413 expect(executable?.mode && executable.mode & 0o111).toBe(0o111);
414 expect(symlink?.mode && symlink.mode & 0o170000).toBe(0o120000);
415 const archive = yield* zipCode("export const handler = () => {};", files);
416 const zip = unzipSync(archive);
417 expect(zip["node_modules/@img/sharp-linux-arm64/lib/sharp.node"]).toBeDefined();
418 expect(zip["node_modules/@img/sharp-libvips-linux-arm64/lib/libvips.so"]).toBeDefined();
419 expect(strFromU8(zip["node_modules/.bin/sharp-tool"]!)).toBe("../sharp/bin/sharp-tool");
420 expect(installDirectory).toBeDefined();
421 expect(yield* fs.exists(installDirectory!)).toBe(false);
422 } finally {
423 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
424 }
425 }).pipe(Effect.provide(NodeServices.layer)),
426 );
427
428 it.effect("does not install package.json dependencies unless requested", () =>
429 Effect.gen(function* () {
430 const fs = yield* FileSystem.FileSystem;
431 const path = yield* Path.Path;
432 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-unrequested-" });
433 let installed = false;
434
435 try {
436 yield* fs.writeFileString(
437 path.join(root, "package.json"),
438 JSON.stringify({ dependencies: { "heic-convert": "^2.1.0" } }),
439 );
440
441 const files = yield* installPackages({
442 cwd: root,
443 architecture: "arm64",
444 runNpmInstall: () =>
445 Effect.sync(() => {
446 installed = true;
447 }),
448 });
449
450 expect(installed).toBe(false);
451 expect(files).toEqual([]);
452 } finally {
453 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
454 }
455 }).pipe(Effect.provide(NodeServices.layer)),
456 );
457
458 it.effect("returns an empty install identity when nothing is requested", () =>
459 Effect.gen(function* () {
460 const fs = yield* FileSystem.FileSystem;
461 const path = yield* Path.Path;
462 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-empty-identity-" });
463
464 try {
465 yield* fs.writeFileString(
466 path.join(root, "package.json"),
467 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
468 );
469 yield* fs.writeFileString(path.join(root, "package-lock.json"), "sharp@0.34.5");
470
471 expect(yield* resolvePackageInstallIdentity({ cwd: root, requested: {} })).toEqual({
472 resolved: {},
473 overrides: {},
474 });
475 } finally {
476 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
477 }
478 }).pipe(Effect.provide(NodeServices.layer)),
479 );
480
481 it.effect("fails when the source package.json is missing", () =>
482 Effect.gen(function* () {
483 const fs = yield* FileSystem.FileSystem;
484 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-missing-manifest-" });
485
486 try {
487 const error = yield* installPackages({
488 cwd: root,
489 install: ["sharp"],
490 architecture: "arm64",
491 runNpmInstall: () => Effect.void,
492 }).pipe(Effect.flip);
493 expect(error.message).toContain(
494 `Failed to read package.json for Lambda externals from '${root}'`,
495 );
496 } finally {
497 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
498 }
499 }).pipe(Effect.provide(NodeServices.layer)),
500 );
501
502 it.effect("fails when the nearest lockfile cannot be read", () =>
503 Effect.gen(function* () {
504 const fs = yield* FileSystem.FileSystem;
505 const path = yield* Path.Path;
506 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-lockfile-read-error-" });
507
508 try {
509 yield* fs.writeFileString(
510 path.join(root, "package.json"),
511 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
512 );
513 yield* fs.makeDirectory(path.join(root, "package-lock.json"));
514
515 const error = yield* resolvePackageInstallIdentity({
516 cwd: root,
517 requested: { sharp: "*" },
518 }).pipe(Effect.flip);
519 expect(error.message).toContain(
520 `Failed to read package-manager lockfile for Lambda externals from '${root}'`,
521 );
522 } finally {
523 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
524 }
525 }).pipe(Effect.provide(NodeServices.layer)),
526 );
527
528 it.effect("resolves catalog versions from a top-level Bun catalog", () =>
529 Effect.gen(function* () {
530 const fs = yield* FileSystem.FileSystem;
531 const path = yield* Path.Path;
532 const root = yield* fs.makeTempDirectory({
533 prefix: "alchemy-external-bun-top-level-catalog-",
534 });
535
536 try {
537 yield* fs.writeFileString(
538 path.join(root, "package.json"),
539 JSON.stringify({
540 workspaces: ["packages/*"],
541 catalog: { sharp: "^0.33.5" },
542 dependencies: { sharp: "catalog:" },
543 }),
544 );
545
546 const files = yield* installPackages({
547 cwd: root,
548 install: ["sharp"],
549 architecture: "arm64",
550 runNpmInstall: (directory) =>
551 Effect.gen(function* () {
552 const packageJson = JSON.parse(
553 yield* fs.readFileString(path.join(directory, "package.json")),
554 );
555 expect(packageJson.dependencies.sharp).toBe("^0.33.5");
556 }),
557 });
558
559 expect(files.map((file) => file.path)).toContain("package.json");
560 } finally {
561 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
562 }
563 }).pipe(Effect.provide(NodeServices.layer)),
564 );
565
566 it.effect("resolves catalog versions from manifest catalogs with package-only workspaces", () =>
567 Effect.gen(function* () {
568 const fs = yield* FileSystem.FileSystem;
569 const path = yield* Path.Path;
570 const root = yield* fs.makeTempDirectory({
571 prefix: "alchemy-external-bun-workspace-packages-only-",
572 });
573
574 try {
575 yield* fs.writeFileString(
576 path.join(root, "package.json"),
577 JSON.stringify({
578 workspaces: { packages: ["packages/*"] },
579 catalog: { sharp: "^0.34.5" },
580 dependencies: { sharp: "catalog:" },
581 }),
582 );
583
584 const files = yield* installPackages({
585 cwd: root,
586 install: ["sharp"],
587 architecture: "arm64",
588 runNpmInstall: (directory) =>
589 Effect.gen(function* () {
590 const packageJson = JSON.parse(
591 yield* fs.readFileString(path.join(directory, "package.json")),
592 );
593 expect(packageJson.dependencies.sharp).toBe("^0.34.5");
594 }),
595 });
596
597 expect(files.map((file) => file.path)).toContain("package.json");
598 } finally {
599 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
600 }
601 }).pipe(Effect.provide(NodeServices.layer)),
602 );
603
604 it.effect("fails when a catalog reference cannot be resolved", () =>
605 Effect.gen(function* () {
606 const fs = yield* FileSystem.FileSystem;
607 const path = yield* Path.Path;
608 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-unresolved-catalog-" });
609
610 try {
611 yield* fs.writeFileString(
612 path.join(root, "package.json"),
613 JSON.stringify({ dependencies: { sharp: "catalog:" } }),
614 );
615
616 const error = yield* resolveInstallTargets({ cwd: root, requested: { sharp: "*" } }).pipe(
617 Effect.flip,
618 );
619 expect(error.message).toContain("Could not resolve catalog version for 'sharp'");
620 } finally {
621 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
622 }
623 }).pipe(Effect.provide(NodeServices.layer)),
624 );
625
626 it.effect("fails when a pnpm catalog entry is missing", () =>
627 Effect.gen(function* () {
628 const fs = yield* FileSystem.FileSystem;
629 const path = yield* Path.Path;
630 const root = yield* fs.makeTempDirectory({
631 prefix: "alchemy-external-missing-pnpm-catalog-entry-",
632 });
633
634 try {
635 yield* fs.writeFileString(
636 path.join(root, "pnpm-workspace.yaml"),
637 ["packages:", " - packages/*", "catalog:", " other: ^1.0.0"].join("\n"),
638 );
639 yield* fs.writeFileString(
640 path.join(root, "package.json"),
641 JSON.stringify({ dependencies: { sharp: "catalog:" } }),
642 );
643
644 const error = yield* resolveInstallTargets({ cwd: root, requested: { sharp: "*" } }).pipe(
645 Effect.flip,
646 );
647 expect(error.message).toContain("Could not resolve catalog version for 'sharp' (catalog:)");
648 expect(error.message).toContain("pnpm-workspace.yaml");
649 } finally {
650 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
651 }
652 }).pipe(Effect.provide(NodeServices.layer)),
653 );
654
655 it.effect("omits lockfile fingerprints when no lockfile exists", () =>
656 Effect.gen(function* () {
657 const fs = yield* FileSystem.FileSystem;
658 const path = yield* Path.Path;
659 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-no-lockfile-" });
660
661 try {
662 yield* fs.writeFileString(
663 path.join(root, "package.json"),
664 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
665 );
666
667 expect(
668 yield* resolvePackageInstallIdentity({ cwd: root, requested: { sharp: "*" } }),
669 ).toEqual({ resolved: { sharp: "^0.34.5" }, overrides: {}, lockfile: undefined });
670 } finally {
671 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
672 }
673 }).pipe(Effect.provide(NodeServices.layer)),
674 );
675
676 it.effect("fails when a Bun workspace catalog entry is missing", () =>
677 Effect.gen(function* () {
678 const fs = yield* FileSystem.FileSystem;
679 const path = yield* Path.Path;
680 const root = yield* fs.makeTempDirectory({
681 prefix: "alchemy-external-missing-bun-catalog-entry-",
682 });
683
684 try {
685 yield* fs.writeFileString(
686 path.join(root, "package.json"),
687 JSON.stringify({
688 workspaces: { packages: ["packages/*"], catalogs: { native: { other: "^1.0.0" } } },
689 dependencies: { sharp: "catalog:native" },
690 }),
691 );
692
693 const error = yield* resolveInstallTargets({ cwd: root, requested: { sharp: "*" } }).pipe(
694 Effect.flip,
695 );
696 expect(error.message).toContain(
697 "Could not resolve catalog version for 'sharp' (catalog:native)",
698 );
699 expect(error.message).toContain("package.json");
700 } finally {
701 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
702 }
703 }).pipe(Effect.provide(NodeServices.layer)),
704 );
705
706 describe.sequential("npm install failures", () => {
707 it.effect(
708 "fails when npm is missing from PATH",
709 () =>
710 Effect.gen(function* () {
711 const fs = yield* FileSystem.FileSystem;
712 const path = yield* Path.Path;
713 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-external-missing-npm-" });
714 const originalPath = process.env.PATH;
715
716 try {
717 process.env.PATH = "";
718 yield* fs.writeFileString(
719 path.join(root, "package.json"),
720 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
721 );
722
723 const error = yield* installResolvedPackages({
724 resolved: { sharp: "^0.34.5" },
725 architecture: "arm64",
726 }).pipe(Effect.flip);
727 expect(error.message).toContain("Failed to run 'npm install' for build.install:");
728 expect(error.message).toMatch(/NotFound|ENOENT/);
729 } finally {
730 process.env.PATH = originalPath;
731 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
732 }
733 }).pipe(Effect.provide(NodeServices.layer)),
734 // Blanking process.env.PATH is process-global: every concurrently
735 // running test that spawns a child process would fail with ENOENT.
736 // `exclusive` takes the runner's whole-process write lock.
737 { exclusive: true },
738 );
739
740 it.effect(
741 "fails when npm install exits non-zero",
742 () =>
743 Effect.gen(function* () {
744 const error = yield* installResolvedPackages({
745 resolved: { "alchemy-nonexistent-external-package-xyz": "1.0.0" },
746 architecture: "arm64",
747 }).pipe(Effect.flip);
748 expect(error.message).toMatch(
749 /npm install for build\.install failed with exit code \d+:/,
750 );
751 }).pipe(Effect.provide(NodeServices.layer)),
752 { timeout: 120_000 },
753 );
754 });
755
756 // `chmod 0o000` cannot make a directory unreadable on Windows, so the
757 // failure under test is unreproducible there.
758 it.effect.skipIf(process.platform === "win32")(
759 "fails when installed package files cannot be read",
760 () =>
761 Effect.gen(function* () {
762 const fs = yield* FileSystem.FileSystem;
763 const path = yield* Path.Path;
764 const root = yield* fs.makeTempDirectory({
765 prefix: "alchemy-external-artifact-read-error-",
766 });
767
768 try {
769 yield* fs.writeFileString(
770 path.join(root, "package.json"),
771 JSON.stringify({ dependencies: { sharp: "^0.34.5" } }),
772 );
773
774 const error = yield* installPackages({
775 cwd: root,
776 install: ["sharp"],
777 architecture: "arm64",
778 runNpmInstall: (directory, args) =>
779 Effect.gen(function* () {
780 if (args[0] === "install" && args.includes("--package-lock-only")) return;
781 yield* fs.writeFileString(
782 path.join(directory, "package.json"),
783 JSON.stringify({ private: true, dependencies: { sharp: "^0.34.5" } }),
784 );
785 yield* fs.writeFileString(path.join(directory, "package-lock.json"), "{}");
786 yield* fs.chmod(directory, 0o000);
787 }),
788 }).pipe(Effect.flip);
789 expect(error.message).toBe("Failed to read installed Lambda external packages");
790 } finally {
791 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
792 }
793 }).pipe(Effect.provide(NodeServices.layer)),
794 );
795
796 it.effect("pins requested packages to the versions in supported lockfiles", () =>
797 Effect.gen(function* () {
798 const fixtures = [
799 {
800 name: "package-lock.json",
801 content: JSON.stringify({
802 name: "fixture",
803 lockfileVersion: 3,
804 packages: {
805 "": { dependencies: { sharp: "^0.34.0" } },
806 "node_modules/sharp": { version: "0.34.5", dependencies: { semver: "^7.0.0" } },
807 "node_modules/semver": { version: "7.7.3" },
808 },
809 }),
810 },
811 {
812 name: "package-lock.json",
813 content: JSON.stringify({
814 lockfileVersion: 1,
815 dependencies: {
816 sharp: { version: "0.34.5", requires: { semver: "^7.0.0" } },
817 semver: { version: "7.7.3" },
818 },
819 }),
820 },
821 {
822 name: "pnpm-lock.yaml",
823 content: [
824 "lockfileVersion: '9.0'",
825 "importers:",
826 " .:",
827 " dependencies:",
828 " sharp:",
829 " specifier: ^0.34.0",
830 " version: 0.34.5",
831 "snapshots:",
832 " sharp@0.34.5:",
833 " dependencies:",
834 " semver: 7.7.3",
835 " semver@7.7.3: {}",
836 ].join("\n"),
837 },
838 {
839 // pnpm 12: pnpm's own environment lock document, then the project
840 // lockfile as a second YAML document.
841 name: "pnpm-lock.yaml",
842 content: [
843 "---",
844 "lockfileVersion: '9.0'",
845 "importers:",
846 " .:",
847 " packageManagerDependencies:",
848 " pnpm:",
849 " specifier: 12.9.1",
850 " version: 12.9.1",
851 "",
852 "---",
853 "lockfileVersion: '9.0'",
854 "importers:",
855 " .:",
856 " dependencies:",
857 " sharp:",
858 " specifier: ^0.34.0",
859 " version: 0.34.5",
860 "snapshots:",
861 " sharp@0.34.5:",
862 " dependencies:",
863 " semver: 7.7.3",
864 " semver@7.7.3: {}",
865 ].join("\n"),
866 },
867 {
868 name: "bun.lock",
869 content: [
870 "{",
871 " // Bun lockfiles use JSONC.",
872 ' "lockfileVersion": 1,',
873 ' "workspaces": {',
874 ' "": { "dependencies": { "sharp": "^0.34.0" } },',
875 " },",
876 ' "packages": {',
877 ' "sharp": ["sharp@0.34.5", "", { "dependencies": { "semver": "^7.0.0" } }],',
878 ' "semver": ["semver@7.7.3", "", {}],',
879 " },",
880 "}",
881 ].join("\n"),
882 },
883 {
884 name: "yarn.lock",
885 content: [
886 "# yarn lockfile v1",
887 "",
888 "sharp@^0.34.0:",
889 ' version "0.34.5"',
890 ' resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.34.5.tgz"',
891 " dependencies:",
892 ' semver "^7.0.0"',
893 "semver@^7.0.0:",
894 ' version "7.7.3"',
895 ].join("\n"),
896 },
897 {
898 name: "yarn.lock",
899 content: [
900 '"sharp@npm:^0.34.0":',
901 " version: 0.34.5",
902 ' resolution: "sharp@npm:0.34.5"',
903 " dependencies:",
904 ' semver: "npm:^7.0.0"',
905 '"semver@npm:^7.0.0":',
906 " version: 7.7.3",
907 ' resolution: "semver@npm:7.7.3"',
908 ].join("\n"),
909 },
910 {
911 // v1 with quoted multi-selector entry keys and quoted pair keys.
912 name: "yarn.lock",
913 content: [
914 "# yarn lockfile v1",
915 "",
916 '"sharp@^0.34.0", "sharp@~0.34.5":',
917 ' version "0.34.5"',
918 ' resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.34.5.tgz#abc123"',
919 " dependencies:",
920 ' "semver" "^7.0.0"',
921 '"semver@^7.0.0", semver@^7.5.0:',
922 ' version "7.7.3"',
923 ].join("\n"),
924 },
925 ] as const;
926
927 for (const fixture of fixtures) {
928 yield* withLockfileFixture(
929 {
930 prefix: `alchemy-external-pinned-${fixture.name}-`,
931 packageJson: { dependencies: { sharp: "^0.34.0" } },
932 lockfileName: fixture.name,
933 lockfileContent: fixture.content,
934 },
935 ({ cwd }) =>
936 Effect.gen(function* () {
937 const identity = yield* resolvePackageInstallIdentity({
938 cwd,
939 requested: { sharp: "*" },
940 });
941 expect(identity.resolved, fixture.name).toEqual({ sharp: "0.34.5" });
942 expect(identity.overrides, fixture.name).toEqual({
943 "sharp@0.34.5": { semver: "7.7.3" },
944 });
945 }),
946 );
947 }
948 }).pipe(Effect.provide(NodeServices.layer)),
949 );
950
951 it.effect("does not replace an explicit install version with a different lock entry", () =>
952 withLockfileFixture(
953 {
954 prefix: "alchemy-external-explicit-version-",
955 packageJson: { dependencies: { sharp: "^0.34.0" } },
956 lockfileName: "package-lock.json",
957 lockfileContent: JSON.stringify({
958 name: "fixture",
959 lockfileVersion: 3,
960 packages: {
961 "": { dependencies: { sharp: "^0.34.0" } },
962 "node_modules/sharp": { version: "0.34.5" },
963 },
964 }),
965 },
966 ({ cwd }) =>
967 Effect.gen(function* () {
968 expect(yield* resolveInstallTargets({ cwd, requested: { sharp: "0.33.5" } })).toEqual({
969 sharp: "0.33.5",
970 });
971 }),
972 ).pipe(Effect.provide(NodeServices.layer)),
973 );
974
975 it.effect("pins aliases and transitive dependency edges from package-lock.json", () =>
976 withLockfileFixture(
977 {
978 prefix: "alchemy-external-package-lock-plan-",
979 packageJson: { dependencies: { alias: "npm:real@^1.0.0", parent: "^1.0.0" } },
980 lockfileName: "package-lock.json",
981 lockfileContent: JSON.stringify({
982 lockfileVersion: 3,
983 packages: {
984 "": { dependencies: { alias: "npm:real@^1.0.0", parent: "^1.0.0" } },
985 "node_modules/alias": {
986 name: "real",
987 version: "1.2.3",
988 dependencies: { leaf: "^2.0.0" },
989 },
990 "node_modules/parent": { version: "1.0.0", dependencies: { leaf: "^2.0.0" } },
991 "node_modules/leaf": { version: "2.4.0" },
992 },
993 }),
994 },
995 ({ cwd }) =>
996 Effect.gen(function* () {
997 const identity = yield* resolvePackageInstallIdentity({
998 cwd,
999 requested: { alias: "*", parent: "*" },
1000 });
1001 expect(identity.resolved).toEqual({ alias: "npm:real@1.2.3", parent: "1.0.0" });
1002 expect(identity.overrides).toEqual({
1003 "real@1.2.3": { leaf: "2.4.0" },
1004 "parent@1.0.0": { leaf: "2.4.0" },
1005 });
1006 }),
1007 ).pipe(Effect.provide(NodeServices.layer)),
1008 );
1009
1010 it.effect("rejects a stale Bun importer entry", () =>
1011 withLockfileFixture(
1012 {
1013 prefix: "alchemy-external-stale-bun-lock-",
1014 packageJson: { dependencies: { sharp: "^0.35.0" } },
1015 lockfileName: "bun.lock",
1016 lockfileContent: JSON.stringify({
1017 lockfileVersion: 1,
1018 workspaces: { "": { dependencies: { sharp: "^0.34.0" } } },
1019 packages: { sharp: ["sharp@0.34.5", "", {}] },
1020 }),
1021 },
1022 ({ cwd }) =>
1023 Effect.gen(function* () {
1024 const error = yield* resolveInstallTargets({ cwd, requested: { sharp: "*" } }).pipe(
1025 Effect.flip,
1026 );
1027 expect(error.message).toContain("Could not resolve a locked version");
1028 }),
1029 ).pipe(Effect.provide(NodeServices.layer)),
1030 );
1031
1032 it.effect("resolves Bun packages for the current workspace importer", () =>
1033 withLockfileFixture(
1034 {
1035 prefix: "alchemy-external-bun-importer-",
1036 workspace: "packages/app",
1037 packageJson: { dependencies: { sharp: "^0.35.0" } },
1038 lockfileName: "bun.lock",
1039 lockfileContent: JSON.stringify({
1040 lockfileVersion: 1,
1041 workspaces: { "packages/app": { name: "app", dependencies: { sharp: "^0.35.0" } } },
1042 packages: { sharp: ["sharp@0.34.5", "", {}], "app/sharp": ["sharp@0.35.1", "", {}] },
1043 }),
1044 },
1045 ({ cwd }) =>
1046 Effect.gen(function* () {
1047 expect(yield* resolveInstallTargets({ cwd, requested: { sharp: "*" } })).toEqual({
1048 sharp: "0.35.1",
1049 });
1050 }),
1051 ).pipe(Effect.provide(NodeServices.layer)),
1052 );
1053
1054 it.effect("preserves context-specific Bun dependency resolutions", () =>
1055 withLockfileFixture(
1056 {
1057 prefix: "alchemy-external-bun-context-",
1058 packageJson: { dependencies: { root: "^1.0.0" } },
1059 lockfileName: "bun.lock",
1060 lockfileContent: JSON.stringify({
1061 lockfileVersion: 1,
1062 workspaces: { "": { dependencies: { root: "^1.0.0" } } },
1063 packages: {
1064 root: ["root@1.0.0", "", { dependencies: { left: "^1", right: "^1" } }],
1065 "root/left": ["left@1.0.0", "", { dependencies: { shared: "^1" } }],
1066 "root/right": ["right@1.0.0", "", { dependencies: { shared: "^1" } }],
1067 "root/left/shared": ["shared@1.0.0", "", { dependencies: { leaf: "^1" } }],
1068 "root/right/shared": ["shared@1.0.0", "", { dependencies: { leaf: "^1" } }],
1069 "root/left/shared/leaf": ["leaf@1.0.0", "", {}],
1070 "root/right/shared/leaf": ["leaf@2.0.0", "", {}],
1071 },
1072 }),
1073 },
1074 ({ cwd }) =>
1075 Effect.gen(function* () {
1076 const identity = yield* resolvePackageInstallIdentity({ cwd, requested: { root: "*" } });
1077 // `shared` resolves uniquely under the root so one flat rule covers
1078 // it; only the divergent `leaf` needs per-branch nesting.
1079 expect(identity.overrides).toEqual({
1080 "root@1.0.0": {
1081 left: { ".": "1.0.0", leaf: "1.0.0" },
1082 right: { ".": "1.0.0", leaf: "2.0.0" },
1083 shared: "1.0.0",
1084 },
1085 });
1086 }),
1087 ).pipe(Effect.provide(NodeServices.layer)),
1088 );
1089
1090 it.effect("keeps overrides flat and compact for diamond-shaped graphs", () => {
1091 // 16 levels of diamonds = 2^16 distinct paths. A path-enumerating walk
1092 // would take minutes and emit megabytes; the deduplicated walk must emit
1093 // one flat rule per package.
1094 const levels = 16;
1095 const packages: Record<string, unknown> = {
1096 "": { dependencies: { root: "^1.0.0" } },
1097 "node_modules/root": { version: "1.0.0", dependencies: { a1: "^1.0.0", b1: "^1.0.0" } },
1098 };
1099 for (let i = 1; i <= levels; i++) {
1100 const dependencies = i === levels ? {} : { [`a${i + 1}`]: "^1.0.0", [`b${i + 1}`]: "^1.0.0" };
1101 packages[`node_modules/a${i}`] = { version: "1.0.0", dependencies };
1102 packages[`node_modules/b${i}`] = { version: "1.0.0", dependencies };
1103 }
1104 return withLockfileFixture(
1105 {
1106 prefix: "alchemy-external-diamond-",
1107 packageJson: { dependencies: { root: "^1.0.0" } },
1108 lockfileName: "package-lock.json",
1109 lockfileContent: JSON.stringify({ lockfileVersion: 3, packages }),
1110 },
1111 ({ cwd }) =>
1112 Effect.gen(function* () {
1113 const identity = yield* resolvePackageInstallIdentity({ cwd, requested: { root: "*" } });
1114 const rootOverrides = identity.overrides["root@1.0.0"];
1115 expect(typeof rootOverrides).toBe("object");
1116 expect(Object.keys(rootOverrides as object)).toHaveLength(levels * 2);
1117 expect(
1118 Object.values(rootOverrides as Record<string, unknown>).every(
1119 (spec) => spec === "1.0.0",
1120 ),
1121 ).toBe(true);
1122 }),
1123 ).pipe(Effect.provide(NodeServices.layer));
1124 });
1125
1126 it.effect("disambiguates pnpm peer-variant resolutions without failing", () =>
1127 withLockfileFixture(
1128 {
1129 prefix: "alchemy-external-pnpm-peer-variants-",
1130 packageJson: { dependencies: { root: "^1.0.0" } },
1131 lockfileName: "pnpm-lock.yaml",
1132 lockfileContent: [
1133 "lockfileVersion: '9.0'",
1134 "importers:",
1135 " .:",
1136 " dependencies:",
1137 " root:",
1138 " specifier: ^1.0.0",
1139 " version: 1.0.0",
1140 "snapshots:",
1141 " root@1.0.0:",
1142 " dependencies:",
1143 " m1: 1.0.0",
1144 " m2: 1.0.0",
1145 " m1@1.0.0:",
1146 " dependencies:",
1147 " p: 1.0.0(x@1.0.0)",
1148 " m2@1.0.0:",
1149 " dependencies:",
1150 " p: 1.0.0(x@2.0.0)",
1151 " p@1.0.0(x@1.0.0):",
1152 " dependencies:",
1153 " x: 1.0.0",
1154 " p@1.0.0(x@2.0.0):",
1155 " dependencies:",
1156 " x: 2.0.0",
1157 " x@1.0.0: {}",
1158 " x@2.0.0: {}",
1159 ].join("\n"),
1160 },
1161 ({ cwd }) =>
1162 Effect.gen(function* () {
1163 const identity = yield* resolvePackageInstallIdentity({ cwd, requested: { root: "*" } });
1164 expect(identity.overrides).toEqual({
1165 "root@1.0.0": {
1166 m1: { ".": "1.0.0", x: "1.0.0" },
1167 m2: { ".": "1.0.0", x: "2.0.0" },
1168 p: "1.0.0",
1169 },
1170 });
1171 }),
1172 ).pipe(Effect.provide(NodeServices.layer)),
1173 );
1174
1175 it.effect("parses bun.lock JSONC with comments, trailing commas, and tricky strings", () =>
1176 withLockfileFixture(
1177 {
1178 prefix: "alchemy-external-bun-jsonc-",
1179 packageJson: { dependencies: { weird: "^1.0.0" } },
1180 lockfileName: "bun.lock",
1181 lockfileContent: [
1182 "{",
1183 " /* block",
1184 " comment */",
1185 ' "lockfileVersion": 1,',
1186 ' "workspaces": {',
1187 ' "": { "dependencies": { "weird": "^1.0.0" } }, // line comment',
1188 " },",
1189 ' "packages": {',
1190 ' "weird": ["weird@1.2.3", "", { "bin": { "weird,]": "cli,}.js" } }],',
1191 " },",
1192 "}",
1193 ].join("\n"),
1194 },
1195 ({ cwd }) =>
1196 Effect.gen(function* () {
1197 expect(yield* resolveInstallTargets({ cwd, requested: { weird: "*" } })).toEqual({
1198 weird: "1.2.3",
1199 });
1200 }),
1201 ).pipe(Effect.provide(NodeServices.layer)),
1202 );
1203
1204 it.effect("includes package-manager lockfiles in the external package identity", () =>
1205 Effect.gen(function* () {
1206 const lockfileContent = (
1207 lockfileName: "bun.lock" | "package-lock.json" | "pnpm-lock.yaml" | "yarn.lock",
1208 version: string,
1209 ) => {
1210 switch (lockfileName) {
1211 case "bun.lock":
1212 return JSON.stringify({
1213 lockfileVersion: 1,
1214 workspaces: { "": { dependencies: { sharp: "^0.34.5" } } },
1215 packages: { sharp: [`sharp@${version}`, "", {}] },
1216 });
1217 case "package-lock.json":
1218 return JSON.stringify({
1219 lockfileVersion: 3,
1220 packages: {
1221 "": { dependencies: { sharp: "^0.34.5" } },
1222 "node_modules/sharp": { version },
1223 },
1224 });
1225 case "pnpm-lock.yaml":
1226 return [
1227 "lockfileVersion: '9.0'",
1228 "importers:",
1229 " .:",
1230 " dependencies:",
1231 " sharp:",
1232 " specifier: ^0.34.5",
1233 ` version: ${version}`,
1234 ].join("\n");
1235 case "yarn.lock":
1236 return ["# yarn lockfile v1", "", `sharp@^0.34.5:`, ` version "${version}"`].join(
1237 "\n",
1238 );
1239 }
1240 };
1241
1242 for (const lockfileName of [
1243 "bun.lock",
1244 "package-lock.json",
1245 "pnpm-lock.yaml",
1246 "yarn.lock",
1247 ] as const) {
1248 yield* withLockfileFixture(
1249 {
1250 prefix: "alchemy-external-lockfile-",
1251 packageJson: { dependencies: { sharp: "^0.34.5" } },
1252 lockfileName,
1253 lockfileContent: lockfileContent(lockfileName, "0.34.5"),
1254 },
1255 ({ cwd, fs, path }) =>
1256 Effect.gen(function* () {
1257 const first = yield* resolvePackageInstallIdentity({
1258 cwd,
1259 requested: { sharp: "*" },
1260 });
1261 const firstHash = yield* hashPackageInstallIdentity({
1262 bundleHash: "bundle",
1263 identity: first,
1264 architecture: "arm64",
1265 });
1266
1267 yield* fs.writeFileString(
1268 path.join(cwd, lockfileName),
1269 lockfileContent(lockfileName, "0.34.6"),
1270 );
1271
1272 const second = yield* resolvePackageInstallIdentity({
1273 cwd,
1274 requested: { sharp: "*" },
1275 });
1276 const secondHash = yield* hashPackageInstallIdentity({
1277 bundleHash: "bundle",
1278 identity: second,
1279 architecture: "arm64",
1280 });
1281
1282 expect(first.resolved).not.toEqual(second.resolved);
1283 expect(first.lockfile?.name).toBe(lockfileName);
1284 expect(second.lockfile?.name).toBe(lockfileName);
1285 expect(first.lockfile?.hash).not.toBe(second.lockfile?.hash);
1286 expect(firstHash).not.toBe(secondHash);
1287 }),
1288 );
1289 }
1290 }).pipe(Effect.provide(NodeServices.layer)),
1291 );
1292
1293 it.effect("does not fall back to node_modules for an invalid bun.lockb", () =>
1294 withLockfileFixture(
1295 {
1296 prefix: "alchemy-external-invalid-lockb-",
1297 packageJson: { dependencies: { sharp: "^0.34.5" } },
1298 lockfileName: "bun.lockb",
1299 lockfileContent: "invalid",
1300 },
1301 ({ cwd, fs, path }) =>
1302 Effect.gen(function* () {
1303 const installedPackage = path.join(cwd, "node_modules", "sharp");
1304 yield* fs.makeDirectory(installedPackage, { recursive: true });
1305 yield* fs.writeFileString(
1306 path.join(installedPackage, "package.json"),
1307 JSON.stringify({ name: "sharp", version: "999.0.0" }),
1308 );
1309
1310 const error = yield* resolveInstallTargets({ cwd, requested: { sharp: "*" } }).pipe(
1311 Effect.flip,
1312 );
1313 expect(error.message).toContain("Failed to inspect legacy Bun lockfile");
1314 }),
1315 ).pipe(Effect.provide(NodeServices.layer)),
1316 );
1317 });
1318
1319 describe.skipIf(!integrationEnabled)(
1320 "Lambda external packages integration",
1321 { tags: ["live"] },
1322 () => {
1323 it.effect(
1324 "npm-installs aliases with pinned transitive dependencies",
1325 () =>
1326 Effect.gen(function* () {
1327 const files = yield* installResolvedPackages({
1328 resolved: { odd: "npm:is-odd@3.0.1" },
1329 overrides: { "is-odd@3.0.1": { "is-number": "6.0.0" } },
1330 architecture: "arm64",
1331 });
1332 const aliasManifest = files.find((file) => file.path === "node_modules/odd/package.json");
1333 const transitiveManifest = files.find(
1334 (file) =>
1335 file.path === "node_modules/is-number/package.json" ||
1336 file.path === "node_modules/odd/node_modules/is-number/package.json",
1337 );
1338 expect(aliasManifest).toBeDefined();
1339 expect(transitiveManifest).toBeDefined();
1340 const decoder = new TextDecoder();
1341 const alias = yield* Effect.try(() => JSON.parse(decoder.decode(aliasManifest?.content)));
1342 const transitive = yield* Effect.try(() =>
1343 JSON.parse(decoder.decode(transitiveManifest?.content)),
1344 );
1345 expect(alias.name).toBe("is-odd");
1346 expect(alias.version).toBe("3.0.1");
1347 expect(transitive.version).toBe("6.0.0");
1348 }).pipe(Effect.provide(NodeServices.layer)),
1349 { timeout: 120_000 },
1350 );
1351
1352 it.effect(
1353 "npm-installs sharp with linux arm64 native binaries",
1354 () =>
1355 withLockfileFixture(
1356 {
1357 prefix: "alchemy-external-sharp-",
1358 packageJson: { dependencies: { sharp: "^0.33.5" } },
1359 lockfileName: "package-lock.json",
1360 lockfileContent: JSON.stringify({
1361 lockfileVersion: 3,
1362 packages: {
1363 "": { dependencies: { sharp: "^0.33.5" } },
1364 "node_modules/sharp": { version: "0.33.5", dependencies: { semver: "^7.5.4" } },
1365 "node_modules/semver": { version: "7.7.3" },
1366 },
1367 }),
1368 },
1369 ({ cwd }) =>
1370 Effect.gen(function* () {
1371 const files = yield* installPackages({
1372 cwd,
1373 install: ["sharp"],
1374 architecture: "arm64",
1375 });
1376
1377 const paths = files.map((file) => file.path);
1378 expect(paths).toContain("node_modules/sharp/package.json");
1379 expect(
1380 paths.some((filePath) =>
1381 filePath.includes(
1382 "node_modules/@img/sharp-linux-arm64/lib/sharp-linux-arm64.node",
1383 ),
1384 ),
1385 ).toBe(true);
1386 expect(
1387 paths.some((filePath) =>
1388 filePath.includes("node_modules/@img/sharp-libvips-linux-arm64/lib/libvips"),
1389 ),
1390 ).toBe(true);
1391 }),
1392 ).pipe(Effect.provide(NodeServices.layer)),
1393 { timeout: 120_000 },
1394 );
1395 },
1396 );