Skip to content

Commit 6be85c6

Browse files
committed
fix(cli): install missing fonts during browser setup
1 parent aa8c666 commit 6be85c6

3 files changed

Lines changed: 211 additions & 4 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { describe, expect, it } from "@effect/vitest";
3+
import {
4+
HostProcessArguments,
5+
HostProcessEnvironment,
6+
HostProcessExecutablePath,
7+
HostProcessIsExecutable,
8+
HostProcessPlatform,
9+
HostProcessUserId,
10+
} from "@t3tools/shared/hostProcess";
11+
import * as Effect from "effect/Effect";
12+
import * as FileSystem from "effect/FileSystem";
13+
import * as Layer from "effect/Layer";
14+
import * as PlatformError from "effect/PlatformError";
15+
import * as Sink from "effect/Sink";
16+
import * as Stream from "effect/Stream";
17+
import * as TestConsole from "effect/testing/TestConsole";
18+
import { Command } from "effect/cli";
19+
import * as ChildProcess from "effect/process/ChildProcess";
20+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
21+
22+
import { browserCommand } from "./browser.ts";
23+
24+
const runSetup = (input: {
25+
readonly font: string | undefined;
26+
readonly root?: boolean;
27+
readonly apt?: boolean;
28+
readonly installedBrowser?: boolean;
29+
readonly missingLibraries?: boolean;
30+
readonly platform?: NodeJS.Platform;
31+
}) => {
32+
const commands: Array<ReadonlyArray<string>> = [];
33+
const browser = "/t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell";
34+
const spawner = ChildProcessSpawner.make((command) =>
35+
Effect.gen(function* () {
36+
if (!ChildProcess.isStandardCommand(command)) return yield* Effect.die("Unexpected pipeline");
37+
commands.push([command.command, ...command.args]);
38+
if (command.command === "fc-match" && input.font === undefined) {
39+
return yield* PlatformError.systemError({
40+
_tag: "NotFound",
41+
module: "ChildProcess",
42+
method: "spawn",
43+
});
44+
}
45+
const output =
46+
command.command === "fc-match"
47+
? (input.font ?? "")
48+
: command.command === browser && input.missingLibraries
49+
? "error while loading shared libraries: libnss3.so: cannot open shared object file"
50+
: command.command === "ldd"
51+
? "libnss3.so => not found\n"
52+
: "Chrome 154\n";
53+
return ChildProcessSpawner.makeHandle({
54+
pid: ChildProcessSpawner.ProcessId(1),
55+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
56+
isRunning: Effect.succeed(false),
57+
kill: () => Effect.void,
58+
unref: Effect.succeed(Effect.void),
59+
stdin: Sink.drain,
60+
stdout: Stream.make(new TextEncoder().encode(output)),
61+
stderr: Stream.empty,
62+
all: Stream.make(new TextEncoder().encode(output)),
63+
getInputFd: () => Sink.drain,
64+
getOutputFd: () => Stream.empty,
65+
});
66+
}),
67+
);
68+
const fs = FileSystem.makeNoop({
69+
exists: (path) =>
70+
Effect.succeed(path === "/usr/bin/apt-get" ? (input.apt ?? true) : path === browser),
71+
readFileString: () => Effect.succeed("0"),
72+
readDirectory: (path) =>
73+
Effect.succeed(
74+
input.installedBrowser === false
75+
? []
76+
: path.endsWith("chrome-headless-shell")
77+
? ["linux64"]
78+
: ["154"],
79+
),
80+
});
81+
return Effect.gen(function* () {
82+
yield* Command.runWith(browserCommand, { version: "0.0.0" })(["setup", "--base-dir", "/t3"]);
83+
return { commands, output: (yield* TestConsole.logLines).join("\n") };
84+
}).pipe(
85+
Effect.provideService(FileSystem.FileSystem, fs),
86+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
87+
Effect.provideService(HostProcessPlatform, input.platform ?? "linux"),
88+
Effect.provideService(HostProcessUserId, input.root ? 0 : 1000),
89+
Effect.provideService(HostProcessEnvironment, { PATH: "" }),
90+
Effect.provideService(HostProcessArguments, ["/usr/bin/node", "/t3/bin.mjs"]),
91+
Effect.provideService(HostProcessExecutablePath, "/usr/bin/node"),
92+
Effect.provideService(HostProcessIsExecutable, false),
93+
Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer)),
94+
);
95+
};
96+
97+
describe("t3 browser setup fonts", () => {
98+
it.effect.each([undefined, ""])(
99+
"reports missing fonts even when Chrome starts (fc-match: %s)",
100+
(font) =>
101+
Effect.gen(function* () {
102+
const { commands, output } = yield* runSetup({ font });
103+
expect(output).toContain("needs fontconfig and system fonts");
104+
expect(output).toContain("sudo t3 browser setup");
105+
expect(output).not.toContain("This host is ready");
106+
expect(commands.some(([name]) => name === "apt-get")).toBe(false);
107+
}),
108+
);
109+
110+
it.effect("installs fonts before the browser's first use", () =>
111+
Effect.gen(function* () {
112+
const { commands, output } = yield* runSetup({
113+
font: undefined,
114+
root: true,
115+
installedBrowser: false,
116+
});
117+
expect(commands.filter(([name]) => name === "apt-get")).toEqual([
118+
["apt-get", "update"],
119+
["apt-get", "install", "-y", "--no-install-recommends", "fontconfig", "fonts-liberation"],
120+
]);
121+
expect(output).toContain("Restart the T3 server");
122+
expect(output).toContain("This host is ready");
123+
}),
124+
);
125+
126+
it.effect("leaves hosts with working fonts alone", () =>
127+
Effect.gen(function* () {
128+
const { commands, output } = yield* runSetup({
129+
font: "/usr/share/fonts/sans.ttf",
130+
root: true,
131+
});
132+
expect(commands.some(([name]) => name === "apt-get")).toBe(false);
133+
expect(output).toContain("This host is ready");
134+
}),
135+
);
136+
137+
it.effect("installs missing libraries and fonts with one package-list refresh", () =>
138+
Effect.gen(function* () {
139+
const { commands } = yield* runSetup({ font: "", root: true, missingLibraries: true });
140+
const packages = commands
141+
.filter(([name, action]) => name === "apt-get" && action === "install")
142+
.flatMap((command) => command.slice(2));
143+
expect(packages).toContain("libnss3");
144+
expect(packages).toContain("fonts-liberation");
145+
expect(
146+
commands.filter(([name, action]) => name === "apt-get" && action === "update"),
147+
).toHaveLength(1);
148+
}),
149+
);
150+
151+
it.effect("gives package-manager guidance without apt instead of reporting ready", () =>
152+
Effect.gen(function* () {
153+
const { commands, output } = yield* runSetup({ font: "", root: true, apt: false });
154+
expect(output).toContain("fontconfig and system fonts");
155+
expect(output).toContain("Install them with your package manager");
156+
expect(output).not.toContain("This host is ready");
157+
expect(commands.some(([name]) => name === "apt-get")).toBe(false);
158+
}),
159+
);
160+
161+
it.effect("does not check fonts on macOS", () =>
162+
Effect.gen(function* () {
163+
const { commands, output } = yield* runSetup({ font: undefined, platform: "darwin" });
164+
expect(commands).toEqual([]);
165+
expect(output).toContain("Nothing to set up");
166+
}),
167+
);
168+
});

‎apps/server/src/cli/browser.ts‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* - installs the AppArmor profile that lets Chrome's sandbox run where the host
77
* restricts unprivileged user namespaces (Ubuntu 23.10+), and
8-
* - installs the Debian packages for any libraries the browser cannot load.
8+
* - installs the Debian packages for missing browser libraries and fonts.
99
*
1010
* Both need root. Without it, the command prints what it would change and the
1111
* `sudo` line to run. It is safe to run again; it skips what is already done.
@@ -105,7 +105,7 @@ const installedBrowser = Effect.fn("browserSetup.installedBrowser")(function* (b
105105

106106
const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe(
107107
Command.withDescription(
108-
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.",
108+
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries and fonts.",
109109
),
110110
Command.withHandler(({ baseDir }) =>
111111
Effect.gen(function* () {
@@ -121,9 +121,20 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
121121
const missing = Option.isSome(browser)
122122
? yield* PreviewBrowserHost.missingLibraries(browser.value)
123123
: [];
124+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
125+
// Chrome can start without fonts, but pages then render without text.
126+
const font = yield* spawner
127+
.string(
128+
ChildProcess.make("fc-match", ["--format=%{file}", "sans-serif"], {
129+
stdin: "ignore",
130+
stderr: "ignore",
131+
}),
132+
)
133+
.pipe(Effect.orElseSucceed(() => ""));
134+
const needsFonts = font.trim() === "";
124135
const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false));
125136

126-
if (!needsProfile && missing.length === 0) {
137+
if (!needsProfile && missing.length === 0 && !needsFonts) {
127138
return yield* Console.log(
128139
Option.isSome(browser)
129140
? "This host is ready for T3's browser."
@@ -140,6 +151,11 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
140151
if (missing.length > 0) {
141152
yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`);
142153
}
154+
if (needsFonts) {
155+
yield* Console.log(
156+
"T3's browser needs fontconfig and system fonts; setup installs them.",
157+
);
158+
}
143159
return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`);
144160
}
145161

@@ -184,6 +200,27 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
184200
yield* Console.log("Installed the browser's libraries.");
185201
}
186202

203+
if (needsFonts) {
204+
if (!hasApt) {
205+
return yield* Console.log(
206+
"T3's browser needs fontconfig and system fonts. Install them with your package manager, then run this again.",
207+
);
208+
}
209+
if (missing.length === 0) {
210+
yield* runStep("refresh the package lists", "apt-get", ["update"]);
211+
}
212+
yield* runStep("install the browser's fonts", "apt-get", [
213+
"install",
214+
"-y",
215+
"--no-install-recommends",
216+
"fontconfig",
217+
"fonts-liberation",
218+
]);
219+
yield* Console.log(
220+
"Installed the browser's fonts. Restart the T3 server if browser tabs were already open.",
221+
);
222+
}
223+
187224
yield* Console.log("This host is ready for T3's browser.");
188225
}),
189226
),

‎docs/user/remote-access.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ The server shows the exact line for how you started it, such as
225225
`sudo npx t3 browser setup`, and keeps your `PATH` when Node is installed only
226226
for your user. Where `t3` is not on your `PATH`, such as with only the
227227
desktop app installed, it names the full path of the app's own `t3` instead. It allows Chrome's sandbox with an AppArmor profile and installs
228-
any missing libraries with apt. It is safe to run again. Without `sudo`, it
228+
any missing libraries and system fonts with apt. On minimal hosts such as Ubuntu
229+
Server, run setup if pages load without text. Restart the T3 server after
230+
installing fonts if browser tabs were already open. It is safe to run again. Without `sudo`, it
229231
only reports what it would change.
230232

231233
The browser always runs in Chrome's sandbox. Where you cannot change the host,

0 commit comments

Comments
 (0)