diff --git a/packages/cli/src/index.test.ts b/packages/cli/src/index.test.ts index cca3ef1..8a6d6eb 100644 --- a/packages/cli/src/index.test.ts +++ b/packages/cli/src/index.test.ts @@ -3,6 +3,9 @@ */ import { describe, it, expect, vi, beforeEach } from 'vitest'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; // Mock all command modules vi.mock('./commands/send.js', () => ({ sendCommand: vi.fn() })); @@ -195,6 +198,16 @@ describe('direct execution detection', () => { expect(isDirectCliExecution(new URL(`file://${entry}`).href, ['node', entry])).toBe(true) }) + it('returns true when argv entry is a symlink to the import meta URL', () => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wanman-cli-')) + const entry = path.join(tempDir, 'index.js') + const linkedEntry = path.join(tempDir, 'wanman') + fs.writeFileSync(entry, '') + fs.symlinkSync(entry, linkedEntry) + + expect(isDirectCliExecution(new URL(`file://${entry}`).href, ['node', linkedEntry])).toBe(true) + }) + it('returns false without an argv entry', () => { expect(isDirectCliExecution('file:///tmp/wanman-entry.js', ['node'])).toBe(false) }) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 9dbf4e1..a2efbbf 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -9,6 +9,7 @@ * wanman escalate */ +import * as fs from 'node:fs'; import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import { sendCommand } from './commands/send.js'; @@ -142,7 +143,11 @@ export function isDirectCliExecution(metaUrl = import.meta.url, argv = process.a if (!entry) return false try { - return path.resolve(entry) === path.resolve(fileURLToPath(metaUrl)) + const entryPath = path.resolve(entry) + const metaPath = path.resolve(fileURLToPath(metaUrl)) + if (entryPath === metaPath) return true + + return fs.realpathSync(entryPath) === fs.realpathSync(metaPath) } catch { return false }