Skip to content

Commit 583b866

Browse files
fix(macos): open a running app by path only when its path is absolute
A relative or dash-led argv[0] names no bundle on this host, so such a copy leaves the open to open -b. Trims the comments the change added.
1 parent 220f363 commit 583b866

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

‎packages/platform-apple/src/core/__tests__/tool-provider.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ test('local Apple tool provider exposes macOS host operations as semantic method
177177
const calls: Array<[string, string[], string | undefined]> = [];
178178
const provider = createLocalAppleToolProvider({
179179
runCommand: async (cmd, args, options) => {
180-
// The bundle open's LaunchServices probe is the seam's own read, asserted in
181-
// launch-services-registration.test.ts; this routing test reports the bundle as listed with
182-
// no running copy, which leaves the open to the bundle id.
180+
// The bundle open's LaunchServices registration probe is the seam's own read, asserted in
181+
// launch-services-registration.test.ts; this routing test only reports it as listing the bundle.
183182
if (cmd === '/usr/bin/lsappinfo') {
184183
return { exitCode: 0, stdout: 'ASN:0x0-0x1234-"Demo":\n', stderr: '' };
185184
}

‎packages/platform-apple/src/os/macos/launch-services-registration.test.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ test('openBundle opens the listed running copy by its own path, not by bundle id
135135

136136
await openBundle(runCommand);
137137

138-
// `open -b` opens whichever registered copy LaunchServices resolves the bundle id to, which can be
139-
// a stale build at another path; the running copy's own path attaches to it. A listed copy is
140-
// never waited for.
141138
expect(transcript()).toEqual([
142139
{ cmd: LAUNCH_SERVICES_TOOL, args: ['find', `bundleid=${BUNDLE_ID}`] },
143140
{ cmd: PROCESS_TABLE_TOOL, args: PROCESS_TABLE_ARGS },
@@ -185,7 +182,6 @@ test('openBundle leaves copies running from two paths to open -b', async () => {
185182
]);
186183
const { runCommand, calls } = fakeMacHost((call) => {
187184
if (call.cmd === LAUNCH_SERVICES_TOOL) return { stdout: REGISTERED };
188-
// Both bundles carry the opened identifier, so no one path names the running app.
189185
if (call.cmd === 'plutil') return { stdout: 'com.example.demo\ncom.example.demo\n' };
190186
return {};
191187
});
@@ -195,6 +191,26 @@ test('openBundle leaves copies running from two paths to open -b', async () => {
195191
expect(calls.at(-1)).toEqual({ cmd: 'open', args: ['-b', BUNDLE_ID] });
196192
});
197193

194+
test.each([
195+
['a relative', 'Contents/Resources/FortiTray/FortiTray.app/Contents/MacOS/FortiTray'],
196+
['a dash-led', '-Demo.app/Contents/MacOS/Demo'],
197+
])('openBundle leaves a copy run from %s path to open -b', async (_shape, command) => {
198+
processTable.stdout = psRows([[4242, command]]);
199+
const { runCommand, transcript } = fakeMacHost((call) => {
200+
if (call.cmd === LAUNCH_SERVICES_TOOL) return { stdout: REGISTERED };
201+
if (call.cmd === 'plutil') return { stdout: `${BUNDLE_ID}\n` };
202+
return {};
203+
});
204+
205+
await openBundle(runCommand);
206+
207+
expect(transcript()).toEqual([
208+
{ cmd: LAUNCH_SERVICES_TOOL, args: ['find', `bundleid=${BUNDLE_ID}`] },
209+
{ cmd: PROCESS_TABLE_TOOL, args: PROCESS_TABLE_ARGS },
210+
{ cmd: 'open', args: ['-b', BUNDLE_ID] },
211+
]);
212+
});
213+
198214
test('openBundle runs open at once when no process of the opened bundle runs', async () => {
199215
noRunningApps();
200216
const { runCommand, calls, transcript } = fakeMacHost();

‎packages/platform-apple/src/os/macos/launch-services-registration.ts‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,17 @@ const APP_BUNDLE_INFO_PLIST = 'Contents/Info.plist';
3535
const LSAPPINFO_ASN = /ASN:/;
3636

3737
/**
38-
* The bundle path of the one running copy of `bundleId` that LaunchServices lists, which `open`
39-
* must name instead of the bundle id, after waiting out the registration race (#3406). Undefined
40-
* leaves the open to `open -b`.
41-
*
42-
* `open -b` opens the copy LaunchServices resolves the bundle id to, which is not always the one
43-
* running: when several copies of the bundle are registered, such as successive dev builds at
44-
* different paths, it can launch a stale build with no arguments next to the running one. Naming
45-
* the running copy's own path attaches to it.
38+
* The bundle path of the one running copy of `bundleId` that LaunchServices lists, or undefined to
39+
* leave the open to `open -b`. `open -b` opens the copy LaunchServices resolves the bundle id to,
40+
* which can be a stale build registered at another path rather than the running copy.
4641
*
4742
* A copy started directly — by a supervisor or a dev script — is only listed by LaunchServices
48-
* about half a second after it spawns. While it is unlisted, `open` sees no instance and launches
49-
* a second one with no arguments, which the session then drives instead of the copy the caller
50-
* owns. So the copy is only named once LaunchServices lists it. An open with no running copy pays
51-
* one `lsappinfo find` and one process-table read (plus a bundle-identifier read only when
52-
* app-shaped candidates exist) to learn that; an open of a running copy pays the bundle-identifier
53-
* read as well; only a copy that runs while unlisted is waited for. Every read shares one budget,
54-
* and the wait itself stays well inside it:
43+
* about half a second after it spawns (#3406). While it is unlisted, `open` sees no instance and
44+
* launches a second one with no arguments, which the session then drives instead of the copy the
45+
* caller owns. An open with no running copy pays one `lsappinfo find` and one process-table read
46+
* (plus a bundle-identifier read only when app-shaped candidates exist) to learn that; an open of a
47+
* running copy pays the bundle-identifier read as well; only a copy that runs while unlisted is
48+
* waited for. Every read shares one budget, and the wait itself stays well inside it:
5549
*
5650
* - `LAUNCH_SERVICES_PROBE_BUDGET_MS` bounds every read below, each read also taking its own
5751
* smaller cap so a single hung tool cannot consume the budget alone;
@@ -78,7 +72,6 @@ export async function resolveListedRunningBundle(
7872
) {
7973
return undefined;
8074
}
81-
// Copies at several paths leave the choice to LaunchServices, as `open -b` always did.
8275
return running.size === 1 ? [...running.keys()][0] : undefined;
8376
} catch (error) {
8477
// The probe is an extra read on someone else's app launch, so a host that cannot answer it at
@@ -96,7 +89,6 @@ export async function resolveListedRunningBundle(
9689
}
9790
}
9891

99-
/** Whether LaunchServices came to list one of `pids` before the wait's bounds ran out. */
10092
async function waitForListing(
10193
runCommand: AppleToolCommandExecutor,
10294
bundleId: string,
@@ -154,10 +146,6 @@ async function readsLaunchServicesInstance(
154146
return LSAPPINFO_ASN.test(result.stdout);
155147
}
156148

157-
/**
158-
* The processes running `bundleId`, by the app bundle path each one runs from. Identifiers compare
159-
* without regard to case, as LaunchServices compares them.
160-
*/
161149
async function findRunningInstances(
162150
runCommand: AppleToolCommandExecutor,
163151
bundleId: string,
@@ -198,6 +186,7 @@ async function findRunningInstances(
198186
return new Map(
199187
bundlePaths
200188
.filter(
189+
// LaunchServices compares bundle identifiers without regard to case.
201190
(bundlePath) =>
202191
bundleIds.get(path.join(bundlePath, APP_BUNDLE_INFO_PLIST))?.trim().toLowerCase() ===
203192
bundleId.toLowerCase(),
@@ -213,6 +202,7 @@ async function findRunningInstances(
213202
* XPC services, app extensions, and driver bundles, whose identifiers match no app anyone opens.
214203
*/
215204
function macAppBundlePath(command: string): string | undefined {
205+
if (!path.isAbsolute(command)) return undefined;
216206
const marker = command.indexOf(APP_EXECUTABLE_MARKER);
217207
if (marker <= 0) return undefined;
218208
const bundlePath = command.slice(0, marker);

0 commit comments

Comments
 (0)