Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: count a symlinked path to the entry script as main, so guarded scripts run in a vault under a symlinked folder (#393)

Brenno Ferrari7724dba3ca20
diff --git a/.claude/scripts/lib/main-guard.ts b/.claude/scripts/lib/main-guard.ts
index 6df7c58..f471cf5 100644
--- a/.claude/scripts/lib/main-guard.ts
+++ b/.claude/scripts/lib/main-guard.ts
@@ -6,16 +6,29 @@
* if (isMainModule(import.meta.url)) main();
*
* Equivalent of CommonJS's `require.main === module`. `fileURLToPath` strips
- * the `file://` prefix and URL-encoded segments so the comparison against
- * `process.argv[1]` (which is already an absolute resolved path) is
- * apples-to-apples on every platform.
+ * the `file://` prefix and URL-encoded segments. Node gives the entry module
+ * its real path, while `process.argv[1]` keeps the path as typed, so a
+ * symlinked folder on the way (macOS's /var, a link to /private/var, or a
+ * Windows junction) is resolved on the argv side too.
*/
+import { realpathSync } from "node:fs";
import { fileURLToPath } from "node:url";
+/** The real path, or the path itself when it can't be resolved. */
+const real = (path: string): string => {
+ try {
+ return realpathSync(path);
+ } catch {
+ return path;
+ }
+};
+
export function isMainModule(importMetaUrl: string): boolean {
- return (
- process.argv[1] !== undefined &&
- fileURLToPath(importMetaUrl) === process.argv[1]
- );
+ const argv1 = process.argv[1];
+ if (argv1 === undefined) return false;
+ const modulePath = fileURLToPath(importMetaUrl);
+ // The module's path is already real (the loader resolved it), so only
+ // argv[1] needs resolving.
+ return modulePath === argv1 || modulePath === real(argv1);
}
diff --git a/.claude/scripts/tests/main-guard.test.ts b/.claude/scripts/tests/main-guard.test.ts
index d46b6be..17c1a98 100644
--- a/.claude/scripts/tests/main-guard.test.ts
+++ b/.claude/scripts/tests/main-guard.test.ts
@@ -1,17 +1,21 @@
/**
* Unit tests for lib/main-guard — detect direct-script invocation vs import.
*
- * The function compares `fileURLToPath(importMetaUrl)` against `process.argv[1]`.
+ * The function compares `fileURLToPath(importMetaUrl)` against `process.argv[1]`,
+ * resolving argv[1]'s real path too (obsidian-mind#391).
* We can't mutate `import.meta.url`, but we can pass a synthetic value and
* stage `process.argv[1]` to cover the matrix.
*/
-import { test, describe, afterEach } from "node:test";
+import { test, describe, afterEach, before, after } from "node:test";
import assert from "node:assert/strict";
+import { mkdirSync, mkdtempSync, realpathSync, symlinkSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { isMainModule } from "../lib/main-guard.ts";
-import { hostPath } from "./_helpers.ts";
+import { hostPath, rmTemp } from "./_helpers.ts";
const ORIGINAL_ARGV1: string | undefined = process.argv[1];
@@ -56,4 +60,41 @@ describe("isMainModule", () => {
// before comparing against the raw argv path.
assert.equal(isMainModule(pathToFileURL(path).href), true);
});
+
+ describe("through a symlinked folder (obsidian-mind#391)", () => {
+ // Node gives the entry module its real path; argv[1] keeps the path as typed,
+ // as for a vault under macOS's /var, a symlink to /private/var.
+ let root = "";
+ let linked = true;
+ before(() => {
+ root = mkdtempSync(join(tmpdir(), "main-guard-"));
+ mkdirSync(join(root, "real"));
+ writeFileSync(join(root, "real", "foo.ts"), "");
+ writeFileSync(join(root, "real", "bar.ts"), "");
+ try {
+ symlinkSync(join(root, "real"), join(root, "link"), "junction");
+ } catch {
+ linked = false;
+ }
+ });
+ after(() => rmTemp(root));
+ const realUrl = (name: string) => pathToFileURL(realpathSync(join(root, "real", name))).href;
+
+ test("true when argv[1] reaches the module through the link", (t) => {
+ if (!linked) return t.skip("no symlink rights here");
+ process.argv[1] = join(root, "link", "foo.ts");
+ assert.equal(isMainModule(realUrl("foo.ts")), true);
+ });
+
+ test("false when argv[1] reaches another file through the link", (t) => {
+ if (!linked) return t.skip("no symlink rights here");
+ process.argv[1] = join(root, "link", "bar.ts");
+ assert.equal(isMainModule(realUrl("foo.ts")), false);
+ });
+
+ test("false when argv[1] names no file, so it can't be resolved", () => {
+ process.argv[1] = join(root, "link", "gone.ts");
+ assert.equal(isMainModule(realUrl("foo.ts")), false);
+ });
+ });
});