Skip to content
Merged
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
169 changes: 169 additions & 0 deletions apps/desktop/src/main/fs/workspace-file-watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import type { FileChangeEvent } from "@pi-desktop/shared";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
createWorkspaceFileWatcherService,
type FileWatchSender,
type WatchDirectoryFn,
} from "./workspace-file-watcher";

function createFakeSender() {
const sent: Array<{ channel: string; payload: FileChangeEvent }> = [];
const destroyedListeners: Array<() => void> = [];
const sender: FileWatchSender & {
sent: typeof sent;
triggerDestroyed: () => void;
} = {
sent,
send: (channel, payload) => {
sent.push({ channel, payload });
},
once: (_event, listener) => {
destroyedListeners.push(listener);
},
triggerDestroyed: () => {
for (const listener of destroyedListeners) listener();
},
};
return sender;
}

function createFakeWatchDirectory() {
const listeners = new Map<
string,
(eventType: "rename" | "change", filename: string | null) => void
>();
const closed: string[] = [];
const watchDirectory: WatchDirectoryFn = (rootPath, listener) => {
listeners.set(rootPath, listener);
return {
close: () => {
closed.push(rootPath);
listeners.delete(rootPath);
},
};
};
return { watchDirectory, listeners, closed };
}

describe("createWorkspaceFileWatcherService", () => {
it("forwards classified events for nested paths on the fs:changed channel", () => {
const fake = createFakeWatchDirectory();
const service = createWorkspaceFileWatcherService(
fake.watchDirectory,
() => 42,
);
const sender = createFakeSender();

service.subscribe(sender, "/root");
const listener = fake.listeners.get("/root");
expect(listener).toBeDefined();

// "change" events are content modifications.
listener?.("change", path.join("src", "deep", "file.ts"));
expect(sender.sent).toEqual([
{
channel: "fs:changed",
payload: {
type: "modify",
path: path.join("/root", "src", "deep", "file.ts"),
timestamp: 42,
},
},
]);
});

it("classifies rename events by existence and ignores dependency/VCS trees", () => {
const root = mkdtempSync(path.join(tmpdir(), "pi-watch-"));
try {
mkdirSync(path.join(root, "src"), { recursive: true });
writeFileSync(path.join(root, "src", "kept.ts"), "x");
const fake = createFakeWatchDirectory();
const service = createWorkspaceFileWatcherService(fake.watchDirectory);
const sender = createFakeSender();
service.subscribe(sender, root);
const listener = fake.listeners.get(root);

listener?.("rename", path.join("src", "kept.ts"));
listener?.("rename", path.join("src", "gone.ts"));
listener?.("change", path.join("node_modules", "pkg", "index.js"));
listener?.("rename", path.join(".git", "index"));

const types = sender.sent.map((entry) => entry.payload.type);
expect(types).toEqual(["create", "delete"]);
service.unsubscribeAll(sender);
} finally {
rmSync(root, { recursive: true, force: true });
}
});

it("shares one watcher per root and closes it when the last sender leaves", () => {
const fake = createFakeWatchDirectory();
const service = createWorkspaceFileWatcherService(fake.watchDirectory);
const first = createFakeSender();
const second = createFakeSender();

service.subscribe(first, "/root");
service.subscribe(second, "/root");
expect(fake.listeners.size).toBe(1);
expect(service.watcherCountFor(first)).toBe(1);

service.unsubscribe(first, "/root");
expect(fake.closed).toEqual([]);
fake.listeners.get("/root")?.("change", "a.ts");
expect(first.sent).toHaveLength(0);
expect(second.sent).toHaveLength(1);

service.unsubscribe(second, "/root");
expect(fake.closed).toEqual(["/root"]);
expect(service.watcherCountFor(second)).toBe(0);
});

it("tears down a sender's watches when the sender is destroyed", () => {
const fake = createFakeWatchDirectory();
const service = createWorkspaceFileWatcherService(fake.watchDirectory);
const sender = createFakeSender();

service.subscribe(sender, "/root");
sender.triggerDestroyed();

expect(service.watcherCountFor(sender)).toBe(0);
expect(fake.closed).toEqual(["/root"]);
});

describe("with the real filesystem", () => {
let root: string;

afterEach(() => {
if (root) {
rmSync(root, { recursive: true, force: true });
}
});

it("reports a file created in a nested directory", async () => {
root = mkdtempSync(path.join(tmpdir(), "pi-watch-real-"));
mkdirSync(path.join(root, "src", "deep"), { recursive: true });
const service = createWorkspaceFileWatcherService();
const sender = createFakeSender();
service.subscribe(sender, root);

const nestedPath = path.join(root, "src", "deep", "new-file.ts");
writeFileSync(nestedPath, "export {};\n");

await vi.waitFor(() => {
expect(
sender.sent.some(
(entry) =>
entry.payload.path === nestedPath &&
entry.payload.type === "create",
),
).toBe(true);
});

service.unsubscribeAll(sender);
expect(service.watcherCountFor(sender)).toBe(0);
});
});
});
202 changes: 202 additions & 0 deletions apps/desktop/src/main/fs/workspace-file-watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { existsSync, type FSWatcher, watch as fsWatch } from "node:fs";
import path from "node:path";
import { type FileChangeEvent, IPC_CHANNELS } from "@pi-desktop/shared";

/**
* Workspace filesystem watcher (main process).
*
* Watches a workspace root recursively with `node:fs.watch` and pushes
* classified change events to each subscribed renderer over the typed
* `fs:changed` event contract (`fsContracts.changed`). Subscriptions are
* keyed by the IPC sender (a `WebContents`-shaped object), so a window only
* receives events for roots it asked to watch, and its watches are torn down
* when the sender is destroyed.
*
* Directory basenames in {@link IGNORED_DIRECTORY_NAMES} are never reported:
* they churn constantly (dependency installs, VCS internals) and are not
* shown in the file tree.
*/

export interface FileWatchSender {
send(channel: string, payload: FileChangeEvent): void;
once?(event: "destroyed", listener: () => void): void;
isDestroyed?(): boolean;
}

type RawWatchEventType = "rename" | "change";

export interface WatchDirectoryHandle {
close(): void;
}

export type WatchDirectoryFn = (
rootPath: string,
listener: (eventType: RawWatchEventType, filename: string | null) => void,
) => WatchDirectoryHandle;

export interface WorkspaceFileWatcherService {
subscribe(sender: FileWatchSender, rootPath: string): void;
unsubscribe(sender: FileWatchSender, rootPath: string): void;
unsubscribeAll(sender: FileWatchSender): void;
/** Number of live watchers held for a sender (test/diagnostic hook). */
watcherCountFor(sender: FileWatchSender): number;
}

const IGNORED_DIRECTORY_NAMES: ReadonlySet<string> = new Set([
"node_modules",
".git",
]);

interface WatchEntry {
readonly rootPath: string;
readonly handle: WatchDirectoryHandle;
readonly senders: Set<FileWatchSender>;
}

function defaultWatchDirectory(
rootPath: string,
listener: (eventType: RawWatchEventType, filename: string | null) => void,
): WatchDirectoryHandle {
const watcher: FSWatcher = fsWatch(
rootPath,
{ recursive: true },
(eventType, filename) => {
listener(
eventType === "change" ? "change" : "rename",
filename === null ? null : String(filename),
);
},
);
// A watcher error (e.g. the root was deleted) must not crash the main
// process; the subscription simply stops producing events.
watcher.on("error", () => undefined);
return watcher;
}

function isIgnoredPath(rootPath: string, changedPath: string): boolean {
const relative = path.relative(rootPath, changedPath);
if (relative === "") return false;
return relative
.split(path.sep)
.some((segment) => IGNORED_DIRECTORY_NAMES.has(segment));
}

/**
* Classify a raw `fs.watch` callback into the contract event type.
* `rename` means an entry appeared, disappeared, or moved; existence on disk
* at delivery time decides between create / delete / rename.
*/
function classifyEvent(
eventType: RawWatchEventType,
changedPath: string,
): FileChangeEvent["type"] {
if (eventType === "change") {
return "modify";
}
return existsSync(changedPath) ? "create" : "delete";
}

export function createWorkspaceFileWatcherService(
watchDirectory: WatchDirectoryFn = defaultWatchDirectory,
now: () => number = () => Date.now(),
): WorkspaceFileWatcherService {
/** Live watchers keyed by canonical root path. */
const entriesByRoot = new Map<string, WatchEntry>();
/** Senders already hooked for `destroyed` cleanup. */
const hookedSenders = new WeakSet<FileWatchSender>();

function teardownEntry(entry: WatchEntry): void {
entriesByRoot.delete(entry.rootPath);
try {
entry.handle.close();
} catch {
// Closing an already-dead watcher is harmless.
}
}

function removeSender(entry: WatchEntry, sender: FileWatchSender): void {
entry.senders.delete(sender);
if (entry.senders.size === 0) {
teardownEntry(entry);
}
}

function handleRawEvent(
entry: WatchEntry,
eventType: RawWatchEventType,
filename: string | null,
): void {
// Without a filename we cannot attribute the change to a path; the
// renderer debounces per path, so unattributed events are dropped.
if (!filename) return;
const changedPath = path.join(entry.rootPath, filename);
if (isIgnoredPath(entry.rootPath, changedPath)) return;

const event: FileChangeEvent = {
type: classifyEvent(eventType, changedPath),
path: changedPath,
timestamp: now(),
};

for (const sender of [...entry.senders]) {
if (sender.isDestroyed?.()) {
removeSender(entry, sender);
continue;
}
try {
sender.send(IPC_CHANNELS.fs.changed, event);
} catch {
removeSender(entry, sender);
}
}
}

function subscribe(sender: FileWatchSender, rootPath: string): void {
if (!hookedSenders.has(sender) && typeof sender.once === "function") {
hookedSenders.add(sender);
sender.once("destroyed", () => {
unsubscribeAll(sender);
});
}

const existing = entriesByRoot.get(rootPath);
if (existing) {
existing.senders.add(sender);
return;
}

const entry: WatchEntry = {
rootPath,
senders: new Set([sender]),
handle: watchDirectory(rootPath, (eventType, filename) => {
handleRawEvent(entry, eventType, filename);
}),
};
entriesByRoot.set(rootPath, entry);
}

function unsubscribe(sender: FileWatchSender, rootPath: string): void {
const entry = entriesByRoot.get(rootPath);
if (!entry) return;
removeSender(entry, sender);
}

function unsubscribeAll(sender: FileWatchSender): void {
for (const entry of [...entriesByRoot.values()]) {
removeSender(entry, sender);
}
}

return {
subscribe,
unsubscribe,
unsubscribeAll,
watcherCountFor(sender: FileWatchSender): number {
let count = 0;
for (const entry of entriesByRoot.values()) {
if (entry.senders.has(sender)) count += 1;
}
return count;
},
};
}
Loading
Loading