Skip to content

Commit 7968113

Browse files
refactor: Effect code reads env through HostProcess.Environment
Enable the Effect language-service rule processEnvInEffect as an error and route every hit through the HostProcess.Environment reference. Tests provide the reference instead of mutating the global process.env. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4b87a45 commit 7968113

39 files changed

Lines changed: 296 additions & 446 deletions

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,13 @@ const startup = Effect.gen(function* () {
307307
yield* desktopSettings.load;
308308

309309
if (linuxElectronOptions !== null) {
310+
const env = yield* HostProcess.Environment;
310311
yield* logStartupInfo("linux password store configured", {
311312
passwordStore: hasCommandLinePasswordStore
312313
? "command-line"
313314
: (linuxElectronOptions.passwordStore ?? "electron-default"),
314-
xdgCurrentDesktop: process.env.XDG_CURRENT_DESKTOP ?? null,
315-
xdgSessionDesktop: process.env.XDG_SESSION_DESKTOP ?? null,
315+
xdgCurrentDesktop: env.XDG_CURRENT_DESKTOP ?? null,
316+
xdgSessionDesktop: env.XDG_SESSION_DESKTOP ?? null,
316317
});
317318
}
318319

‎apps/desktop/src/app/DesktopCliCommand.test.ts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2-
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
2+
import { expect, it } from "@effect/vitest";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
44
import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
@@ -29,7 +29,13 @@ const environmentFor = (
2929
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
3030

3131
const commandIn = (
32-
input: { home: string; baseDir?: string; platform?: NodeJS.Platform; isPackaged?: boolean },
32+
input: {
33+
home: string;
34+
baseDir?: string;
35+
platform?: NodeJS.Platform;
36+
isPackaged?: boolean;
37+
env?: NodeJS.ProcessEnv;
38+
},
3339
spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"],
3440
) =>
3541
Effect.gen(function* () {
@@ -42,6 +48,7 @@ const commandIn = (
4248
DesktopEnvironment.DesktopEnvironment,
4349
environmentFor(path, { ...input, baseDir }),
4450
),
51+
Effect.provideService(HostProcess.Environment, input.env ?? { PATH: "" }),
4552
);
4653
return yield* spawner
4754
? make.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner))
@@ -84,14 +91,6 @@ const fakePowerShell = (initial: string) => {
8491
return { registry, spawner };
8592
};
8693

87-
const machinePath = process.env.PATH;
88-
beforeEach(() => {
89-
process.env.PATH = "";
90-
});
91-
afterEach(() => {
92-
process.env.PATH = machinePath;
93-
});
94-
9594
it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
9695
it.effect("links the launcher onto PATH and removes only that link", () =>
9796
Effect.gen(function* () {
@@ -124,7 +123,8 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
124123
const fs = yield* FileSystem.FileSystem;
125124
const path = yield* Path.Path;
126125
const home = yield* fs.makeTempDirectoryScoped();
127-
const command = yield* commandIn({ home });
126+
const env: NodeJS.ProcessEnv = { PATH: "" };
127+
const command = yield* commandIn({ home, env });
128128
const theirs = path.join(home, ".local", "bin", "t3");
129129
yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
130130
yield* fs.writeFileString(theirs, "npm's t3\n");
@@ -139,7 +139,7 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
139139

140140
const launcher = path.join(home, ".t3", "bin", "t3");
141141
yield* fs.writeFileString(launcher, "npm's t3\n");
142-
process.env.PATH = path.dirname(launcher);
142+
env.PATH = path.dirname(launcher);
143143
expect(yield* command.state).toMatchObject({ onPath: false, shadowedBy: launcher });
144144
expect((yield* Effect.flip(command.install)).message).toContain("Could not set up");
145145
yield* command.uninstall;
@@ -173,9 +173,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
173173
const shadow = path.join(home, "shadow");
174174
yield* fs.makeDirectory(shadow);
175175
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
176-
process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
176+
const env = { PATH: [shadow, path.join(home, ".local", "bin")].join(":") };
177177

178-
const command = yield* commandIn({ home });
178+
const command = yield* commandIn({ home, env });
179179
const theirs = path.join(shadow, "t3");
180180
expect((yield* command.state).shadowedBy).toBe(theirs);
181181
const error = yield* Effect.flip(command.install);
@@ -209,9 +209,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
209209
yield* fs.chmod(path.join(plain, "t3"), process.getuid?.() === 0 ? 0o600 : 0o601);
210210
yield* fs.makeDirectory(shadow);
211211
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
212-
process.env.PATH = [folder, plain, shadow, path.join(home, ".local", "bin")].join(":");
212+
const env = { PATH: [folder, plain, shadow, path.join(home, ".local", "bin")].join(":") };
213213

214-
const command = yield* commandIn({ home });
214+
const command = yield* commandIn({ home, env });
215215
expect((yield* command.state).shadowedBy).toBe(path.join(shadow, "t3"));
216216

217217
const error = yield* Effect.flip(command.install);
@@ -231,9 +231,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
231231
const path = yield* Path.Path;
232232
const home = yield* fs.makeTempDirectoryScoped();
233233
const launcher = path.join(home, ".t3", "bin", "t3");
234-
process.env.PATH = [path.dirname(launcher), path.join(home, ".local", "bin")].join(":");
234+
const env = { PATH: [path.dirname(launcher), path.join(home, ".local", "bin")].join(":") };
235235

236-
const command = yield* commandIn({ home });
236+
const command = yield* commandIn({ home, env });
237237
const installed = yield* command.install;
238238

239239
expect(yield* fs.exists(launcher)).toBe(true);

‎apps/desktop/src/app/DesktopCliCommand.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as NodeFSP from "node:fs/promises";
33

44
import type { DesktopCliCommandState } from "@t3tools/contracts";
5+
import * as HostProcess from "@t3tools/shared/HostProcess";
56
import * as Context from "effect/Context";
67
import * as Effect from "effect/Effect";
78
import * as FileSystem from "effect/FileSystem";
@@ -66,6 +67,7 @@ export const make = Effect.gen(function* () {
6667
const environment = yield* DesktopEnvironment.DesktopEnvironment;
6768
const fs = yield* FileSystem.FileSystem;
6869
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
70+
const env = yield* HostProcess.Environment;
6971
const path = environment.path;
7072
const windows = environment.platform === "win32";
7173
const launcher = DesktopCliShim.launcherPath(environment);
@@ -108,7 +110,7 @@ export const make = Effect.gen(function* () {
108110
const powershell = (env: Record<string, string>) =>
109111
Effect.scoped(
110112
Effect.gen(function* () {
111-
const root = process.env.SystemRoot ?? process.env.WINDIR;
113+
const root = env.SystemRoot ?? env.WINDIR;
112114
const handle = yield* spawner.spawn(
113115
ChildProcess.make(
114116
root ? `${root}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe` : "powershell.exe",
@@ -144,7 +146,7 @@ export const make = Effect.gen(function* () {
144146
);
145147

146148
const firstOnPath = Effect.gen(function* () {
147-
for (const directory of pathEntries(process.env.PATH, ":")) {
149+
for (const directory of pathEntries(env.PATH, ":")) {
148150
const candidate = path.join(directory, "t3");
149151
if (yield* isExecutableFile(candidate)) return Option.some(candidate);
150152
}
@@ -239,7 +241,7 @@ export const make = Effect.gen(function* () {
239241
`Another t3 at ${shadowedBy.value} runs first in a new terminal. Remove it, or run the launcher directly at ${launcher}.`,
240242
);
241243
}
242-
const onPath = pathEntries(process.env.PATH, ":");
244+
const onPath = pathEntries(env.PATH, ":");
243245
const candidates = unixCandidates(environment.homeDirectory, environment.platform);
244246
for (const directory of [
245247
...candidates.filter((candidate) => onPath.includes(candidate)),

‎apps/desktop/src/app/DesktopPreReadyPlatform.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class DesktopPreReadyElectronOptions extends Context.Service<
5353
export const make = Effect.gen(function* () {
5454
const platform = yield* HostProcess.Platform;
5555
const homeDirectory = yield* HostProcess.HomeDirectory;
56+
const env = yield* HostProcess.Environment;
5657
return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
5758
const linuxPasswordStoreCommandLine =
5859
platform === "linux"
@@ -63,7 +64,7 @@ export const make = Effect.gen(function* () {
6364

6465
if (linux !== null) {
6566
DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
66-
env: process.env,
67+
env,
6768
homeDirectory,
6869
joinPath: NodePath.posix.join,
6970
commandLine: Electron.app.commandLine,
@@ -77,8 +78,7 @@ export const make = Effect.gen(function* () {
7778
// have removed the executable referenced by the previous launch's entry.
7879
try {
7980
const applicationsDir = NodePath.posix.join(
80-
process.env.XDG_DATA_HOME?.trim() ||
81-
NodePath.posix.join(homeDirectory, ".local", "share"),
81+
env.XDG_DATA_HOME?.trim() || NodePath.posix.join(homeDirectory, ".local", "share"),
8282
"applications",
8383
);
8484
NodeFS.mkdirSync(applicationsDir, { recursive: true });
@@ -110,9 +110,7 @@ export const make = Effect.gen(function* () {
110110
appVersion: Electron.app.getVersion(),
111111
}).displayName,
112112
execTarget:
113-
process.env.T3CODE_DESKTOP_LAUNCHER_PATH?.trim() ||
114-
process.env.APPIMAGE?.trim() ||
115-
process.execPath,
113+
env.T3CODE_DESKTOP_LAUNCHER_PATH?.trim() || env.APPIMAGE?.trim() || process.execPath,
116114
scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
117115
...(iconPath === undefined ? {} : { iconPath }),
118116
}),
@@ -126,7 +124,7 @@ export const make = Effect.gen(function* () {
126124
}
127125
// Chromium keeps only the last --disable-features value, so pass the merged list.
128126
const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
129-
env: process.env,
127+
env,
130128
ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
131129
enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
132130
disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),

0 commit comments

Comments
 (0)