Skip to content

Commit 0c51a38

Browse files
fix(cli): browser setup installs missing fonts
Chromium starts on a host without fontconfig, but pages render no text and `t3 browser setup` still said the host was ready. Setup now probes for a usable font with fc-match and, as root, installs fontconfig and fonts-liberation alongside any missing libraries with one package-list refresh. Without root it names the gap next to the sudo line. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 32d2fed commit 0c51a38

3 files changed

Lines changed: 178 additions & 9 deletions

File tree

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,94 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
44
import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
66
import * as Layer from "effect/Layer";
7+
import * as PlatformError from "effect/PlatformError";
8+
import * as Sink from "effect/Sink";
9+
import * as Stream from "effect/Stream";
710
import * as TestConsole from "effect/testing/TestConsole";
811
import { Command } from "effect/cli";
12+
import * as ChildProcess from "effect/process/ChildProcess";
13+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
914

1015
import { browserCommand } from "./browser.ts";
1116

17+
const BROWSER = "/t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell";
18+
const FONT = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
19+
20+
/**
21+
* Records every command. `fc-match` is missing until fonts are installed, unless
22+
* the host starts with a font; the browser fails to load libnss3 when asked to.
23+
*/
24+
const fakeSpawner = (host: { readonly font?: string; readonly missingLibrary?: boolean }) => {
25+
const commands: Array<ReadonlyArray<string>> = [];
26+
let font = host.font;
27+
const spawner = ChildProcessSpawner.make((command) =>
28+
Effect.gen(function* () {
29+
if (!ChildProcess.isStandardCommand(command)) return yield* Effect.die("Unexpected pipeline");
30+
commands.push([command.command, ...command.args]);
31+
if (command.command === "apt-get" && command.args.includes("fonts-liberation")) font = FONT;
32+
if (command.command === "fc-match" && font === undefined) {
33+
return yield* PlatformError.systemError({
34+
_tag: "NotFound",
35+
module: "ChildProcess",
36+
method: "spawn",
37+
});
38+
}
39+
const output =
40+
command.command === "fc-match"
41+
? font!
42+
: command.command === BROWSER && host.missingLibrary
43+
? "error while loading shared libraries: libnss3.so: cannot open shared object file"
44+
: command.command === "ldd"
45+
? "\tlibnss3.so => not found\n"
46+
: "";
47+
const bytes = Stream.make(new TextEncoder().encode(output));
48+
return ChildProcessSpawner.makeHandle({
49+
pid: ChildProcessSpawner.ProcessId(1),
50+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
51+
isRunning: Effect.succeed(false),
52+
kill: () => Effect.void,
53+
unref: Effect.succeed(Effect.void),
54+
stdin: Sink.drain,
55+
stdout: bytes,
56+
stderr: Stream.empty,
57+
all: bytes,
58+
getInputFd: () => Sink.drain,
59+
getOutputFd: () => Stream.empty,
60+
});
61+
}),
62+
);
63+
return { spawner, commands };
64+
};
65+
66+
/** A Linux host with apt and T3's browser installed under `/t3`. */
67+
const runSetup = (host: {
68+
readonly root: boolean;
69+
readonly font?: string;
70+
readonly missingLibrary?: boolean;
71+
}) => {
72+
const { spawner, commands } = fakeSpawner(host);
73+
const fs = FileSystem.makeNoop({
74+
exists: (path) => Effect.succeed(path === "/usr/bin/apt-get" || path === BROWSER),
75+
readDirectory: (path) =>
76+
Effect.succeed(path.endsWith("chrome-headless-shell") ? ["linux64"] : ["154"]),
77+
});
78+
return Effect.gen(function* () {
79+
yield* Command.runWith(browserCommand, { version: "0.0.0" })(["setup", "--base-dir", "/t3"]);
80+
return { commands, output: (yield* TestConsole.logLines).join("\n") };
81+
}).pipe(
82+
Effect.provideService(FileSystem.FileSystem, fs),
83+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
84+
Effect.provideService(HostProcess.Platform, "linux"),
85+
Effect.provideService(HostProcess.UserId, host.root ? 0 : 1000),
86+
// Root through sudo from a regular user, so setup reports ready instead of the root warning.
87+
Effect.provideService(HostProcess.Environment, host.root ? { SUDO_USER: "alice" } : {}),
88+
Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer)),
89+
);
90+
};
91+
92+
const aptGet = (commands: ReadonlyArray<ReadonlyArray<string>>) =>
93+
commands.filter(([name]) => name === "apt-get");
94+
1295
it.effect("setup in a root shell warns that the server cannot run as root", () =>
1396
Effect.gen(function* () {
1497
// Every file read fails, so setup finds nothing to install and changes nothing.
@@ -18,6 +101,10 @@ it.effect("setup in a root shell warns that the server cannot run as root", () =
18101
"/nonexistent",
19102
]).pipe(
20103
Effect.provide(FileSystem.layerNoop({})),
104+
Effect.provideService(
105+
ChildProcessSpawner.ChildProcessSpawner,
106+
fakeSpawner({ font: FONT }).spawner,
107+
),
21108
Effect.provideService(HostProcess.Platform, "linux"),
22109
Effect.provideService(HostProcess.UserId, 0),
23110
// A root user who follows the old advice runs setup through sudo.
@@ -28,3 +115,32 @@ it.effect("setup in a root shell warns that the server cannot run as root", () =
28115
expect(output).not.toContain("ready");
29116
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer))),
30117
);
118+
119+
it.effect("setup without root does not report a host without fonts as ready", () =>
120+
Effect.gen(function* () {
121+
const { commands, output } = yield* runSetup({ root: false });
122+
expect(output).toContain("T3's browser needs fontconfig and system fonts; setup installs them.");
123+
expect(output).toContain("This needs root. Run:");
124+
expect(output).not.toContain("ready");
125+
expect(aptGet(commands)).toEqual([]);
126+
}),
127+
);
128+
129+
it.effect("setup as root installs missing libraries and fonts after one package refresh", () =>
130+
Effect.gen(function* () {
131+
const { commands, output } = yield* runSetup({ root: true, missingLibrary: true });
132+
const apt = aptGet(commands);
133+
expect(apt.filter(([, action]) => action === "update")).toHaveLength(1);
134+
expect(apt.find((args) => args.includes("libnss3"))).toBeDefined();
135+
expect(apt).toContainEqual([
136+
"apt-get",
137+
"install",
138+
"-y",
139+
"--no-install-recommends",
140+
"fontconfig",
141+
"fonts-liberation",
142+
]);
143+
expect(output).toContain("Installed the browser's fonts.");
144+
expect(output).toContain("This host is ready for T3's browser.");
145+
}),
146+
);

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,31 @@ const installedBrowser = Effect.fn("browserSetup.installedBrowser")(function* (b
8484
return Option.none<string>();
8585
});
8686

87+
/**
88+
* Chrome starts without fonts, but pages then render no text. Without
89+
* fontconfig there is no `fc-match` at all, which counts as no font.
90+
*/
91+
const fontAvailable = Effect.gen(function* () {
92+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
93+
const font = yield* spawner
94+
.string(
95+
ChildProcess.make("fc-match", ["--format=%{file}", "sans-serif"], {
96+
stdin: "ignore",
97+
stderr: "ignore",
98+
}),
99+
)
100+
.pipe(
101+
Effect.timeout("5 seconds"),
102+
Effect.orElseSucceed(() => ""),
103+
);
104+
return font.trim() !== "";
105+
}).pipe(Effect.withSpan("browserSetup.fontAvailable"));
106+
107+
const FONT_PACKAGES = ["fontconfig", "fonts-liberation"];
108+
87109
const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe(
88110
Command.withDescription(
89-
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.",
111+
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries and fonts.",
90112
),
91113
Command.withHandler(({ baseDir }) =>
92114
Effect.gen(function* () {
@@ -108,9 +130,10 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
108130
const missing = Option.isSome(browser)
109131
? yield* PreviewBrowserHost.missingLibraries(browser.value)
110132
: [];
133+
const needsFonts = !(yield* fontAvailable);
111134
const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false));
112135

113-
if (!needsProfile && missing.length === 0) {
136+
if (!needsProfile && missing.length === 0 && !needsFonts) {
114137
return yield* Console.log(
115138
rootWarning ??
116139
(Option.isSome(browser)
@@ -128,6 +151,9 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
128151
if (missing.length > 0) {
129152
yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`);
130153
}
154+
if (needsFonts) {
155+
yield* Console.log("T3's browser needs fontconfig and system fonts; setup installs them.");
156+
}
131157
return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`);
132158
}
133159

@@ -153,13 +179,24 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
153179
yield* Console.log("Allowed Chrome's sandbox for T3's browser.");
154180
}
155181

156-
if (missing.length > 0) {
157-
if (!hasApt) {
158-
return yield* Console.log(
182+
if ((missing.length > 0 || needsFonts) && !hasApt) {
183+
if (missing.length > 0) {
184+
yield* Console.log(
159185
`T3's browser is missing ${missing.join(", ")}. Install them with your package manager, then run this again.`,
160186
);
161187
}
188+
if (needsFonts) {
189+
yield* Console.log(
190+
"T3's browser needs fontconfig and system fonts. Install them with your package manager, then run this again.",
191+
);
192+
}
193+
return;
194+
}
195+
if (missing.length > 0 || needsFonts) {
162196
yield* runStep("refresh the package lists", "apt-get", ["update"]);
197+
}
198+
199+
if (missing.length > 0) {
163200
const packages = yield* Effect.forEach(PreviewBrowserHost.DEBIAN_PACKAGES, (names) =>
164201
Effect.findFirst(names, aptOffers).pipe(Effect.map(Option.getOrElse(() => names[0]!))),
165202
);
@@ -172,6 +209,23 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
172209
yield* Console.log("Installed the browser's libraries.");
173210
}
174211

212+
if (needsFonts) {
213+
yield* runStep("install the browser's fonts", "apt-get", [
214+
"install",
215+
"-y",
216+
"--no-install-recommends",
217+
...FONT_PACKAGES,
218+
]);
219+
if (!(yield* fontAvailable)) {
220+
return yield* Console.log(
221+
"Font packages are installed, but Fontconfig still finds no system font. Check its configuration and run setup again.",
222+
);
223+
}
224+
yield* Console.log(
225+
"Installed the browser's fonts. Restart the T3 server if browser tabs were already open.",
226+
);
227+
}
228+
175229
yield* Console.log(rootWarning ?? "This host is ready for T3's browser.");
176230
}),
177231
),

‎docs/user/remote-access.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ The server shows the exact line for how you started it, such as
231231
`sudo npx t3 browser setup`, and keeps your `PATH` when Node is installed only
232232
for your user. Where `t3` is not on your `PATH`, such as with only the
233233
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
234-
any missing libraries and system fonts with apt. On minimal hosts such as Ubuntu
235-
Server, run setup if pages load without text. Restart the T3 server after
236-
installing fonts if browser tabs were already open. It is safe to run again. Without `sudo`, it
237-
only reports what it would change.
234+
any missing libraries and system fonts with apt; pages that load without text
235+
can mean the host has no fonts. It is safe to run again. Without `sudo`, it only
236+
reports what it would change.
238237

239238
The browser always runs in Chrome's sandbox. Where you cannot change the host,
240239
set `T3CODE_SERVER_BROWSER_SANDBOX=0` for the environment to run without it.

0 commit comments

Comments
 (0)