Skip to content

Commit 9dedea4

Browse files
committed
fix: add codesign hook for esbuild on Darwin platforms
1 parent f82bd7e commit 9dedea4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

packages/plugin-vite/tests/test_utils.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,58 @@ import { walk } from "@std/fs/walk";
44
import { withTmpDir } from "../../fresh/src/test_utils.ts";
55
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
66

7+
let ensureEsbuildSignedPromise: Promise<void> | null = null;
8+
9+
async function ensureEsbuildSigned() {
10+
if (Deno.build.os !== "darwin") {
11+
return;
12+
}
13+
14+
if (ensureEsbuildSignedPromise) {
15+
return await ensureEsbuildSignedPromise;
16+
}
17+
18+
ensureEsbuildSignedPromise = (async () => {
19+
let esbuildPath: string;
20+
try {
21+
esbuildPath = path.fromFileUrl(
22+
import.meta.resolve("npm:esbuild/bin/esbuild"),
23+
);
24+
} catch (err) {
25+
throw new Error(
26+
`Failed to resolve esbuild binary location: ${
27+
err instanceof Error ? err.message : String(err)
28+
}`,
29+
);
30+
}
31+
32+
try {
33+
const command = new Deno.Command("codesign", {
34+
args: ["--force", "--sign", "-", esbuildPath],
35+
stdout: "null",
36+
stderr: "piped",
37+
});
38+
const { code, stderr } = await command.output();
39+
if (code !== 0) {
40+
const message = new TextDecoder().decode(stderr).trim();
41+
throw new Error(
42+
`codesign exited with code ${code}${message ? ": " + message : ""}`,
43+
);
44+
}
45+
} catch (err) {
46+
ensureEsbuildSignedPromise = null;
47+
if (err instanceof Deno.errors.NotFound) {
48+
throw new Error(
49+
"codesign tool was not found. Install Xcode command-line tools or codesign the esbuild binary manually.",
50+
);
51+
}
52+
throw err;
53+
}
54+
})();
55+
56+
await ensureEsbuildSignedPromise;
57+
}
58+
759
export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo");
860
export const FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures");
961

@@ -76,6 +128,7 @@ export async function launchDevServer(
76128
fn: (address: string, dir: string) => void | Promise<void>,
77129
env: Record<string, string> = {},
78130
) {
131+
await ensureEsbuildSigned();
79132
await withChildProcessServer(
80133
{
81134
cwd: dir,
@@ -90,6 +143,7 @@ export async function spawnDevServer(
90143
dir: string,
91144
env: Record<string, string> = {},
92145
) {
146+
await ensureEsbuildSigned();
93147
const boot = Promise.withResolvers<void>();
94148
const p = Promise.withResolvers<void>();
95149

@@ -135,6 +189,7 @@ export async function buildVite(
135189
fixtureDir: string,
136190
options?: { base?: string },
137191
) {
192+
await ensureEsbuildSigned();
138193
const tmp = await withTmpDir({
139194
dir: path.join(import.meta.dirname!, ".."),
140195
prefix: "tmp_vite_",

0 commit comments

Comments
 (0)