Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/desktop/src/fixPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readPathFromLoginShell } from "@t3tools/shared/shell";
import { ensureCommonMacPaths, readPathFromLoginShell } from "@t3tools/shared/shell";

export function fixPath(): void {
if (process.platform !== "darwin") return;
Expand All @@ -12,4 +12,8 @@ export function fixPath(): void {
} catch {
// Keep inherited PATH if shell lookup fails.
}

// Ensure well-known macOS binary directories (e.g. Homebrew) are on PATH
// even when the login-shell probe fails or returns a partial result.
ensureCommonMacPaths();
}
6 changes: 5 additions & 1 deletion apps/server/src/os-jank.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as OS from "node:os";
import { Effect, Path } from "effect";
import { readPathFromLoginShell } from "@t3tools/shared/shell";
import { ensureCommonMacPaths, readPathFromLoginShell } from "@t3tools/shared/shell";

export function fixPath(): void {
if (process.platform !== "darwin") return;
Expand All @@ -14,6 +14,10 @@ export function fixPath(): void {
} catch {
// Silently ignore — keep default PATH
}

// Ensure well-known macOS binary directories (e.g. Homebrew) are on PATH
// even when the login-shell probe fails or returns a partial result.
ensureCommonMacPaths();
}

export const expandHomePath = Effect.fn(function* (input: string) {
Expand Down
62 changes: 60 additions & 2 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";

import { extractPathFromShellOutput, readPathFromLoginShell } from "./shell";
import { COMMON_MACOS_PATHS, ensureCommonMacPaths, extractPathFromShellOutput, readPathFromLoginShell } from "./shell";

describe("extractPathFromShellOutput", () => {
it("extracts the path between capture markers", () => {
Expand Down Expand Up @@ -53,3 +53,61 @@ describe("readPathFromLoginShell", () => {
expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
});
});

describe("ensureCommonMacPaths", () => {
const originalPlatform = process.platform;
const originalPath = process.env.PATH;

afterEach(() => {
Object.defineProperty(process, "platform", { value: originalPlatform });
process.env.PATH = originalPath;
});

it("appends missing Homebrew paths on darwin", () => {
Object.defineProperty(process, "platform", { value: "darwin" });
process.env.PATH = "/usr/bin:/bin";

ensureCommonMacPaths();

const dirs = process.env.PATH!.split(":");
for (const p of COMMON_MACOS_PATHS) {
expect(dirs).toContain(p);
}
// Original paths are still present at the start
expect(dirs[0]).toBe("/usr/bin");
expect(dirs[1]).toBe("/bin");
});

it("does not duplicate paths already present", () => {
Object.defineProperty(process, "platform", { value: "darwin" });
process.env.PATH = `/usr/bin:/bin:${COMMON_MACOS_PATHS.join(":")}`;

ensureCommonMacPaths();

const dirs = process.env.PATH!.split(":");
for (const p of COMMON_MACOS_PATHS) {
expect(dirs.filter((d) => d === p)).toHaveLength(1);
}
});

it("handles empty PATH", () => {
Object.defineProperty(process, "platform", { value: "darwin" });
process.env.PATH = "";

ensureCommonMacPaths();

const dirs = process.env.PATH!.split(":");
for (const p of COMMON_MACOS_PATHS) {
expect(dirs).toContain(p);
}
});

it("is a no-op on non-darwin platforms", () => {
Object.defineProperty(process, "platform", { value: "linux" });
process.env.PATH = "/usr/bin:/bin";

ensureCommonMacPaths();

expect(process.env.PATH).toBe("/usr/bin:/bin");
});
});
28 changes: 28 additions & 0 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ export function readPathFromLoginShell(
});
return extractPathFromShellOutput(output) ?? undefined;
}

/**
* Well-known macOS binary directories that should always be on PATH
* so that tools installed via Homebrew are discoverable even when the
* app is launched from the Dock / Finder (which inherits a minimal PATH).
*/
export const COMMON_MACOS_PATHS = [
"/opt/homebrew/bin", // Apple Silicon Homebrew
"/opt/homebrew/sbin",
"/usr/local/bin", // Intel Homebrew / user binaries
"/usr/local/sbin",
] as const;

/**
* Append any missing well-known macOS binary directories to
* `process.env.PATH`. This is a no-op on non-darwin platforms.
*/
export function ensureCommonMacPaths(): void {
if (process.platform !== "darwin") return;

const currentPath = process.env.PATH ?? "";
const currentDirs = new Set(currentPath.split(":").filter(Boolean));
const missing = COMMON_MACOS_PATHS.filter((p) => !currentDirs.has(p));

if (missing.length > 0) {
process.env.PATH = [currentPath, ...missing].filter(Boolean).join(":");
}
}