@slack/radar-mcp 1.18.1 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,14 +57,105 @@ function webBinPath() {
57
57
  function mcpBinPath() {
58
58
  return path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "mcp", "index.js");
59
59
  }
60
+ /**
61
+ * A keg-only formula (`brew install node@22`) is not linked into `<prefix>/bin`, so it has no entry
62
+ * under that name. Homebrew still gives it one version-free path,
63
+ * `<prefix>/opt/node@22/bin/node`, which is exactly the kind of name this file is looking for.
64
+ * Returns nothing when the directory is absent, which is every non-Homebrew machine.
65
+ *
66
+ * Newest formula first. Plain `node` leads: it is the unpinned formula, so it is the one brew keeps
67
+ * current. Major only in the comparator, since a directory may be `node@22.1.2` and Number("22.1.2")
68
+ * is NaN, which would make the comparison meaningless rather than merely wrong.
69
+ */
70
+ function kegOnlyNodePaths(prefix) {
71
+ try {
72
+ return fs
73
+ .readdirSync(path.join(prefix, "opt"))
74
+ .filter((entry) => /^node(@[\d.]+)?$/.test(entry))
75
+ .sort((a, b) => {
76
+ const version = (name) => Number(name.split("@")[1]?.split(".")[0] ?? Infinity);
77
+ return version(b) - version(a);
78
+ })
79
+ .map((entry) => path.join(prefix, "opt", entry, "bin", "node"));
80
+ }
81
+ catch {
82
+ return []; // no <prefix>/opt to read, so no keg-only formulas to find
83
+ }
84
+ }
85
+ /**
86
+ * Where a stable, version-free name for a node interpreter could live. Homebrew keeps one at
87
+ * `<prefix>/bin/node` (`/opt/homebrew` on Apple silicon, `/usr/local` on Intel) pointing into the
88
+ * versioned Cellar directory, plus a `<prefix>/opt/...` name per keg-only formula. HOMEBREW_PREFIX
89
+ * covers a custom prefix when the install runs from a brew-aware shell.
90
+ *
91
+ * Only used to find a stable NAME for the interpreter that is already running this install, never to
92
+ * pick a different one: stableNodePath substitutes a candidate only when realpath proves it is the
93
+ * same binary. Order therefore does not decide anything here.
94
+ */
95
+ export function stableNodeCandidates(env = process.env) {
96
+ // A relative prefix would produce a relative path, and the app resolves its interpreter against
97
+ // whatever directory Finder launched it from, so only an absolute prefix is usable here.
98
+ const brewPrefixes = ["/opt/homebrew"];
99
+ const configured = env.HOMEBREW_PREFIX;
100
+ if (configured && path.isAbsolute(configured))
101
+ brewPrefixes.unshift(configured);
102
+ const namesUnder = (prefix) => [path.join(prefix, "bin", "node"), ...kegOnlyNodePaths(prefix)];
103
+ return [...new Set([...brewPrefixes.flatMap(namesUnder), "/usr/bin/node", ...namesUnder("/usr/local")])];
104
+ }
105
+ /**
106
+ * A stable path for the node running this install, for baking into the app.
107
+ *
108
+ * WHY NOT process.execPath DIRECTLY. execPath reports the interpreter with symlinks resolved, so a
109
+ * Homebrew node comes back as `/opt/homebrew/Cellar/node/26.8.1/bin/node`. The next `brew upgrade
110
+ * node` deletes that directory, and an app holding it can no longer start the dashboard -- nor
111
+ * repair itself, because the self-heal check runs under the same dead interpreter -- so the window
112
+ * shows its failure page on every launch until someone re-runs install-app by hand.
113
+ * `/opt/homebrew/bin/node` names the same binary today and the upgraded one after that.
114
+ *
115
+ * The trade this makes: `brew unlink node`, or pinning to a keg-only formula later, invalidates the
116
+ * symlink in a case where the versioned Cellar path would still have worked. Worth it, since an
117
+ * upgrade is far more common than an unlink and the failure page now names the exact repair either
118
+ * way, but it is a new way to end up on that page.
119
+ *
120
+ * Substitutes ONLY when a candidate resolves to the exact same executable, so this never bakes in a
121
+ * different node than the one that ran the install: an nvm or system interpreter keeps its own path
122
+ * because no candidate points at it. When nothing matches, the answer is execPath, unchanged.
123
+ *
124
+ * Exported for testing.
125
+ */
126
+ export function stableNodePath(execPath = process.execPath, candidates = stableNodeCandidates()) {
127
+ let real;
128
+ try {
129
+ real = fs.realpathSync(execPath);
130
+ }
131
+ catch {
132
+ return execPath; // cannot resolve it, so cannot prove a candidate is the same binary
133
+ }
134
+ for (const candidate of candidates) {
135
+ if (candidate === real)
136
+ continue; // already the stable name; an indirection would add nothing
137
+ try {
138
+ if (fs.realpathSync(candidate) === real)
139
+ return candidate;
140
+ }
141
+ catch {
142
+ // This candidate could not be resolved: usually absent, sometimes an unreadable parent
143
+ // directory or a symlink cycle. Either way it cannot be proven to be the same binary.
144
+ }
145
+ }
146
+ return execPath;
147
+ }
60
148
  /**
61
149
  * Resolve npx's absolute path, same reasoning as the baked-in node path: a Finder-launched dock
62
150
  * app gets a minimal PATH with no shell config, so a bare `npx` would not resolve. npm ships npx
63
151
  * alongside node in most installs, so check there first; `which` covers the rest. Returns null
64
152
  * (never throws) so a machine with no npx just does not get the self-heal capability.
153
+ *
154
+ * Looks alongside the STABLE node path rather than execPath for the same upgrade reason: the npx next
155
+ * to a Cellar interpreter disappears with it.
65
156
  */
66
157
  export function resolveNpxPath() {
67
- const alongsideNode = path.join(path.dirname(process.execPath), "npx");
158
+ const alongsideNode = path.join(path.dirname(stableNodePath()), "npx");
68
159
  if (fs.existsSync(alongsideNode))
69
160
  return alongsideNode;
70
161
  try {
@@ -131,6 +222,28 @@ alive() {
131
222
  }
132
223
 
133
224
  if ! alive; then
225
+ # Checked here rather than at the top: a dashboard that is already serving needs no interpreter, so
226
+ # a moved node only matters on the path that has to start one.
227
+ #
228
+ # A dialog because there is nowhere else to say it. This script only exists on a Mac with no Swift
229
+ # toolchain, so there is no native window to render a failure page, and a Finder-launched script's
230
+ # stderr goes nowhere the user will ever look. Without this they get a browser connection error that
231
+ # says nothing about node. The wording matches the native window's own node-missing page.
232
+ if [ ! -x "$NODE" ]; then
233
+ MSG="Slack Radar cannot run the node it was built with.
234
+
235
+ $NODE
236
+
237
+ A node upgrade usually moves it. Run this in a terminal, then reopen this app:
238
+
239
+ npx -y @slack/radar-mcp@latest install-app"
240
+ # The message goes in as an ARGUMENT, not interpolated into the AppleScript: a path can contain
241
+ # a quote or a backslash, and building the script text around one is how that turns into a
242
+ # syntax error instead of a dialog.
243
+ osascript -e 'on run {msg}' -e 'display dialog msg with title "Slack Radar" buttons {"OK"} default button "OK" with icon caution' -e 'end run' "$MSG" >/dev/null 2>&1
244
+ exit 1
245
+ fi
246
+
134
247
  # SLACK_RADAR_NO_OPEN stops the server from racing us to open a BROWSER TAB: this app wants the
135
248
  # standalone window it opens below, not a tab. Fully detached (setsid-style via nohup + &, stdio
136
249
  # to a log) so the server outlives this launcher script -- otherwise the app "quitting" would
@@ -189,17 +302,49 @@ open "$URL"
189
302
  * Exported like launcherScript, for the same reason: a test can assert on the generated text
190
303
  * without touching ~/Applications.
191
304
  */
192
- export function swiftInstallConfig(radarUrl, node, webBin, npx, mcpBin) {
305
+ export function swiftInstallConfig(radarUrl, node, webBin, mcpBin) {
193
306
  return [
194
307
  "// --- BEGIN INSTALL CONFIG (substituted by install-app) ---",
195
308
  `let RADAR_URL = ${JSON.stringify(radarUrl)}`,
196
309
  `let RADAR_NODE = ${JSON.stringify(node)}`,
197
310
  `let RADAR_WEB_BIN = ${JSON.stringify(webBin)}`,
198
- `let RADAR_NPX = ${JSON.stringify(npx)}`,
199
311
  `let RADAR_MCP_BIN = ${JSON.stringify(mcpBin)}`,
312
+ // Not a parameter: the package doing the install IS the version the app is built from, so taking
313
+ // it from anywhere else would only create a way for the two to disagree.
314
+ `let RADAR_APP_VERSION = ${JSON.stringify(RADAR_VERSION)}`,
200
315
  "// --- END INSTALL CONFIG ---",
201
316
  ].join("\n");
202
317
  }
318
+ /**
319
+ * Everything the app needs to know about THIS machine.
320
+ *
321
+ * Takes the same overrides stableNodePath() does, for one reason: without them, a test can only assert
322
+ * against whatever the host happens to be, and on a machine with no version-free name for its node
323
+ * (a Linux CI box) the two sides of that assertion converge and the check passes against code that
324
+ * bakes execPath. With them, a synthetic Homebrew layout proves the wiring anywhere.
325
+ */
326
+ export function installConfigValues(execPath, candidates) {
327
+ return {
328
+ node: stableNodePath(execPath ?? process.execPath, candidates ?? stableNodeCandidates()),
329
+ webBin: webBinPath(),
330
+ mcpBin: mcpBinPath(),
331
+ };
332
+ }
333
+ /**
334
+ * RadarApp.swift with its install-config block replaced by this machine's real values. Returns null
335
+ * when the markers no longer match, which must never compile: an app built from the file's own
336
+ * defaults would point at nothing useful.
337
+ *
338
+ * Separate from buildSwiftApp so the values that reach the app can be asserted without a Swift
339
+ * toolchain, which is the only way a Linux CI box can see this wiring at all. Takes those values
340
+ * rather than resolving them, so it stays a pure text substitution: a caller (or a test) can hand it
341
+ * a synthetic machine layout, and nothing here executes an interpreter.
342
+ */
343
+ export function patchedSwiftSource(source, values) {
344
+ const config = swiftInstallConfig(`http://localhost:${WEB_PORT}`, values.node, values.webBin, values.mcpBin);
345
+ const patched = source.replace(/\/\/ --- BEGIN INSTALL CONFIG ---[\s\S]*?\/\/ --- END INSTALL CONFIG ---/, config);
346
+ return patched === source ? null : patched;
347
+ }
203
348
  /**
204
349
  * Compile the WKWebView app into `dest`. Returns false if the machine cannot build it, so install
205
350
  * falls back to the shell launcher rather than failing outright.
@@ -209,7 +354,7 @@ export function swiftInstallConfig(radarUrl, node, webBin, npx, mcpBin) {
209
354
  * two architectures, and would mean shipping an opaque executable through npm. Compiling locally
210
355
  * sidesteps all three: the output is a local build, which Gatekeeper treats as such.
211
356
  */
212
- function buildSwiftApp(dest) {
357
+ function buildSwiftApp(dest, values) {
213
358
  const src = path.join(path.dirname(fileURLToPath(import.meta.url)), "macapp", "RadarApp.swift");
214
359
  if (!fs.existsSync(src))
215
360
  return false;
@@ -219,13 +364,8 @@ function buildSwiftApp(dest) {
219
364
  catch {
220
365
  return false; // no Swift toolchain (no Xcode or Command Line Tools)
221
366
  }
222
- const webBin = webBinPath();
223
- // Substitute the install-time config block. Anchored to the markers so the replacement cannot
224
- // silently miss and compile an app pointing at the defaults.
225
- const source = fs.readFileSync(src, "utf8");
226
- const config = swiftInstallConfig(`http://localhost:${WEB_PORT}`, process.execPath, webBin, resolveNpxPath() ?? "", mcpBinPath());
227
- const patched = source.replace(/\/\/ --- BEGIN INSTALL CONFIG ---[\s\S]*?\/\/ --- END INSTALL CONFIG ---/, config);
228
- if (patched === source)
367
+ const patched = patchedSwiftSource(fs.readFileSync(src, "utf8"), values);
368
+ if (patched === null)
229
369
  return false; // markers moved; do not ship a misconfigured binary
230
370
  const tmpSrc = path.join(os.tmpdir(), `radar-app-${process.pid}.swift`);
231
371
  try {
@@ -352,7 +492,9 @@ export function installMacApp() {
352
492
  const app = path.join(dir, `${APP_NAME}.app`);
353
493
  const staging = `${app}.new`;
354
494
  const backup = `${app}.old`;
355
- const webBin = webBinPath();
495
+ // Resolved once, since both build paths below need the same answer.
496
+ const values = installConfigValues();
497
+ const webBin = values.webBin;
356
498
  if (!fs.existsSync(webBin)) {
357
499
  throw new Error(`cannot find the dashboard entrypoint at ${webBin}`);
358
500
  }
@@ -361,12 +503,12 @@ export function installMacApp() {
361
503
  // staging path -- for the self-heal caller that just means trying again next launch.
362
504
  fs.mkdirSync(dir, { recursive: true });
363
505
  const lockPath = path.join(dir, ".install.lock");
364
- // buildSwiftApp()'s compile step is bounded to 60s (see its execFileSync timeout) specifically
365
- // so this threshold is an actual upper bound on install duration, not an assumption -- icon
366
- // generation and everything else here is fast in comparison. A lock older than this can only be
367
- // a crash leftover, the process that made it died before reaching the finally below. Without
368
- // this, one crashed install would wedge every self-heal check forever, since nothing else ever
369
- // removes the lock file.
506
+ // buildSwiftApp()'s compile is the one step in here that can be slow for a structural reason, and it
507
+ // is bounded at 60s by its own execFileSync timeout, which leaves about 60s of slack against this
508
+ // threshold. The rest is unbounded but local and sub-second in practice (sips, iconutil, lsregister).
509
+ // A lock older than this can only be a crash leftover, the process that made it died before reaching
510
+ // the finally below. Without this, one crashed install would wedge every self-heal check forever,
511
+ // since nothing else ever removes the lock file.
370
512
  const STALE_LOCK_MS = 2 * 60 * 1000;
371
513
  let lockFd;
372
514
  try {
@@ -401,11 +543,12 @@ export function installMacApp() {
401
543
  fs.mkdirSync(resources, { recursive: true });
402
544
  // Prefer the native window; fall back to the shell launcher (opens a browser) if Swift is absent.
403
545
  const nativeExe = path.join(macos, "SlackRadar");
404
- const native = buildSwiftApp(nativeExe);
546
+ const native = buildSwiftApp(nativeExe, values);
405
547
  if (!native) {
406
- // process.execPath is the node running THIS install, the one known-good interpreter on the
407
- // machine. Under npx that is the same node the dashboard would run under anyway.
408
- fs.writeFileSync(path.join(macos, "radar-launcher"), launcherScript(process.execPath, webBin, WEB_PORT), {
548
+ // The node running THIS install, under its most stable name, is the one known-good
549
+ // interpreter on the machine. Under npx that is the same node the dashboard would run under
550
+ // anyway. See stableNodePath() for why the name matters more than the exact path.
551
+ fs.writeFileSync(path.join(macos, "radar-launcher"), launcherScript(values.node, webBin, WEB_PORT), {
409
552
  mode: 0o755,
410
553
  });
411
554
  }
@@ -441,7 +584,7 @@ export function installMacApp() {
441
584
  if (!icon) {
442
585
  console.log("Note: could not build the app icon, so macOS will show a generic one.");
443
586
  }
444
- return { app, native };
587
+ return { app, native, node: values.node };
445
588
  }
446
589
  finally {
447
590
  fs.closeSync(lockFd);
@@ -470,10 +613,17 @@ export function runInstallApp() {
470
613
  "window. Install Xcode Command Line Tools (xcode-select --install) and re-run this for\n" +
471
614
  "a real app window.";
472
615
  console.log(`Installed ${result.app}\n\n` +
616
+ `Dashboard interpreter: ${result.node}\n\n` +
473
617
  `${how}\n\n` +
474
618
  `To keep it in your dock: open it once, then right-click its dock icon and choose\n` +
475
619
  `Options > Keep in Dock.\n\n` +
476
- `Clicking it starts the dashboard if it is not already running.`);
620
+ `Clicking it starts the dashboard if it is not already running.\n\n` +
621
+ // Said outright because the obvious move does not work. This replaced the app bundle, and a
622
+ // copy that is already running neither reloads itself nor re-checks the port, so clicking its
623
+ // dock icon only brings the old window forward with the old dashboard still behind it. That is
624
+ // the whole reported failure, and one sentence closes it.
625
+ `If Radar is already running, quit it and open it again. Clicking a running app only brings\n` +
626
+ `its window forward, so it would keep serving the version you just replaced.`);
477
627
  // Open the containing folder so the user can drag it to the dock without hunting for it.
478
628
  try {
479
629
  spawn("open", [appDir()], { detached: true, stdio: "ignore" }).unref();