You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by wy...@apache.org on 2026/09/04 05:20:52 UTC

(maka) 02/02: fix(desktop): launch Windows local setup safely

This is an automated email from the ASF dual-hosted git repository.

M4n5ter pushed a commit to branch fix/windows-local-remote-access
in repository https://gitbox.apache.org/repos/asf/maka.git

commit 29b98005441151ac5612aa9336f25186b502b9e9
Author: M4n5ter <m4...@gmail.com>
AuthorDate: Fri Sep 4 13:04:26 2026 +0800

    fix(desktop): launch Windows local setup safely
    
    Generated-by: Codex
---
 .../src/main/runtime-host-local-operator.ts        | 46 ++++++++++++++++++++--
 .../src/renderer/locales/settings-projects-copy.ts |  3 ++
 .../settings/runtime-host-profiles-section.tsx     | 12 +++++-
 3 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/apps/desktop/src/main/runtime-host-local-operator.ts b/apps/desktop/src/main/runtime-host-local-operator.ts
index 3bda1aef6b..c19f67d66e 100644
--- a/apps/desktop/src/main/runtime-host-local-operator.ts
+++ b/apps/desktop/src/main/runtime-host-local-operator.ts
@@ -20,7 +20,7 @@
 import { spawn, type ChildProcess } from 'node:child_process';
 import { mkdtemp, rm, rmdir, stat } from 'node:fs/promises';
 import { tmpdir } from 'node:os';
-import { dirname, join } from 'node:path';
+import { delimiter, dirname, isAbsolute, join } from 'node:path';
 import { redactSecrets } from '@maka/core/redaction';
 import {
   DEFAULT_PROCESS_TERMINATION_GRACE_MS,
@@ -662,6 +662,45 @@ function resolveLocalSetupPackage(
   return { specifier: setupPackage.path, integrity: setupPackage.integrity };
 }
 
+async function resolveLocalNpmCommand(
+  command: DesktopRuntimeHostLocalSetupCommand,
+  environment: NodeJS.ProcessEnv,
+): Promise<DesktopRuntimeHostLocalSetupCommand> {
+  if (process.platform !== 'win32' || command.executable !== 'npm') return command;
+
+  const configuredNode = environment.npm_node_execpath;
+  const configuredCli = environment.npm_execpath;
+  const candidates: Array<readonly [string, string]> = [];
+  if (configuredNode && configuredCli) candidates.push([configuredNode, configuredCli]);
+
+  const path = Object.entries(environment).find(([key]) => key.toUpperCase() === 'PATH')?.[1];
+  for (const entry of path?.split(delimiter) ?? []) {
+    const directory = entry.replace(/^"|"$/gu, '').trim();
+    if (!directory) continue;
+    candidates.push([
+      join(directory, 'node.exe'),
+      join(directory, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
+    ]);
+  }
+
+  for (const [nodePath, cliPath] of candidates) {
+    if (!isAbsolute(nodePath) || !isAbsolute(cliPath)) continue;
+    if ((await regularFile(nodePath)) && (await regularFile(cliPath))) {
+      return { executable: nodePath, args: [cliPath, ...command.args] };
+    }
+  }
+  throw new Error('Local Runtime Host management requires a complete Node.js and npm installation');
+}
+
+async function regularFile(path: string): Promise<boolean> {
+  try {
+    return (await stat(path)).isFile();
+  } catch (error) {
+    if (isNodeError(error, 'ENOENT')) return false;
+    throw error;
+  }
+}
+
 function managedTargetArgs(target: DesktopRuntimeHostLocalServiceTarget): string[] {
   return [
     '--expected-service-id',
@@ -752,7 +791,7 @@ function runSetupProcess(input: {
   });
 }
 
-function runFramedProcess<Frame, Result>(input: {
+async function runFramedProcess<Frame, Result>(input: {
   readonly command: DesktopRuntimeHostLocalSetupCommand;
   readonly cwd?: string;
   readonly prefix: string;
@@ -772,8 +811,9 @@ function runFramedProcess<Frame, Result>(input: {
   readonly pendingMaxBytes?: number;
 }): Promise<Result> {
   input.signal?.throwIfAborted();
+  const command = await resolveLocalNpmCommand(input.command, input.environment);
   return new Promise((resolve, reject) => {
-    const child = input.spawnProcess(input.command.executable, [...input.command.args], {
+    const child = input.spawnProcess(command.executable, [...command.args], {
       ...(input.cwd ? { cwd: input.cwd } : {}),
       detached: process.platform !== 'win32',
       env: input.environment,
diff --git a/apps/desktop/src/renderer/locales/settings-projects-copy.ts b/apps/desktop/src/renderer/locales/settings-projects-copy.ts
index 3666a18ddd..fa2aee3c2c 100644
--- a/apps/desktop/src/renderer/locales/settings-projects-copy.ts
+++ b/apps/desktop/src/renderer/locales/settings-projects-copy.ts
@@ -38,6 +38,7 @@ export type SettingsProjectsCopy = {
     configureManuallyDescription: string;
     thisComputerRemoteAccess: string;
     thisComputerRemoteAccessHelp: string;
+    remoteAccessEnabling: string;
     remoteAccessOn: string;
     remoteAccessOff: string;
     enableRemoteAccess: string;
@@ -329,6 +330,7 @@ const SETTINGS_PROJECTS_COPY_BY_LOCALE = {
       configureManuallyDescription: '为已有 Host 填写 TLS、SSH 或 Direct peer 参数',
       thisComputerRemoteAccess: '远程访问',
       thisComputerRemoteAccessHelp: '通过实验性端到端直连访问此 Host;可自动发现公共协调节点来辅助打洞',
+      remoteAccessEnabling: '正在准备并开启远程访问;首次可能需要一点时间。',
       remoteAccessOn: '已开启',
       remoteAccessOff: '未开启',
       enableRemoteAccess: '开启',
@@ -645,6 +647,7 @@ const SETTINGS_PROJECTS_COPY_BY_LOCALE = {
       configureManuallyDescription: 'Enter TLS, SSH, or Direct peer details for an existing Host',
       thisComputerRemoteAccess: 'Remote access',
       thisComputerRemoteAccessHelp: 'Reach this Host through experimental end-to-end direct connections, with automatic public coordination discovery',
+      remoteAccessEnabling: 'Preparing and enabling remote access. The first setup may take a moment.',
       remoteAccessOn: 'On',
       remoteAccessOff: 'Off',
       enableRemoteAccess: 'Enable',
diff --git a/apps/desktop/src/renderer/settings/runtime-host-profiles-section.tsx b/apps/desktop/src/renderer/settings/runtime-host-profiles-section.tsx
index 3e39df880f..6d60724cda 100644
--- a/apps/desktop/src/renderer/settings/runtime-host-profiles-section.tsx
+++ b/apps/desktop/src/renderer/settings/runtime-host-profiles-section.tsx
@@ -112,6 +112,7 @@ export function RuntimeHostProfilesSection(props: {
   const [showJoinSharedSession, setShowJoinSharedSession] = useState(false);
   const [managedTarget, setManagedTarget] = useState<RuntimeHostManagementTarget>();
   const [localAccess, setLocalAccess] = useState<DesktopLocalRuntimeHostRemoteAccessSnapshot>();
+  const [localAccessEnabling, setLocalAccessEnabling] = useState(false);
   const [connectionCodeDialog, setConnectionCodeDialog] = useState<
     | { readonly mode: 'import' }
     | {
@@ -263,6 +264,7 @@ export function RuntimeHostProfilesSection(props: {
 
   async function enableLocalRemoteAccess(allowInterruptActiveTasks = false): Promise<void> {
     setSwitching(true);
+    setLocalAccessEnabling(true);
     try {
       const result = await window.maka.localRuntimeHostRemoteAccess.enable({
         allowInterruptActiveTasks,
@@ -298,7 +300,10 @@ export function RuntimeHostProfilesSection(props: {
         );
       }
     } finally {
-      if (mountedRef.current) setSwitching(false);
+      if (mountedRef.current) {
+        setSwitching(false);
+        setLocalAccessEnabling(false);
+      }
     }
   }
 
@@ -399,7 +404,9 @@ export function RuntimeHostProfilesSection(props: {
         <SettingsRow
           label={copy.thisComputerRemoteAccess}
           description={
-            localAccess?.state === 'unsupported'
+            localAccessEnabling
+              ? copy.remoteAccessEnabling
+              : localAccess?.state === 'unsupported'
               ? localAccess.message
               : localAccess?.state === 'unavailable'
                 ? localAccess.message
@@ -450,6 +457,7 @@ export function RuntimeHostProfilesSection(props: {
                     variant="secondary"
                     size="sm"
                     label={copy.enableRemoteAccess}
+                    isLoading={localAccessEnabling}
                     isDisabled={
                       switching ||
                       !localAccess ||


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org