Skip to content

armgit / SOMA

Public Imported from GitHub
0

Desktop app for designing parametric 3D models in CadQuery together with the coding agent of your choice

App: Mitgeliefertes Python im Paket finden, PATH der Login-Shell unter macOS und Linux

Kai-Jungsthoefel37e3834e1aa5
diff --git a/app/src/main/main.ts b/app/src/main/main.ts
index aa98b93..13e5d57 100644
--- a/app/src/main/main.ts
+++ b/app/src/main/main.ts
@@ -11,7 +11,7 @@ import { backupText, createEntry, entryPath, importInputs, listFiles, renameEntr
import { ProjectController } from './projects';
import { terminalEnv } from './terminals';
import { readHelp, readHelpImage } from './help';
-import { appIcon, editBackupsDir, helpDir, libraryBackupsDir, skillDirs, skillsDir, thumbnailsDir, userSkillsDir } from './paths';
+import { adoptLoginShellPath, appIcon, editBackupsDir, helpDir, libraryBackupsDir, skillDirs, skillsDir, thumbnailsDir, userSkillsDir } from './paths';
import { readSettings, writeSettings } from './settings';
import { areaDir, clearPreviews, isStorageArea, storageStats } from './storage';
import { checkLibraryText, createLibrarySkill, importLibrarySkills, libraryState, ownFile, readLibrarySkill, removeLibrarySkill, renameOldSkillFiles, saveLibrarySkill } from './skilllibrary';
@@ -357,6 +357,7 @@ function createWindow(): void {
// Erst die Verbindung für `soma render` (E58), dann das Fenster: Die Terminals bekommen Adresse und Schlüssel beim Anlegen
app.whenReady().then(async () => {
+ adoptLoginShellPath(); // macOS und Linux: PATH wie im Terminal, sonst fehlen die Agenten
// Python schon jetzt vorwärmen: Bis das Fenster steht und das letzte Modell offen ist, ist cadquery geladen (build.ts)
warmUp();
sweepDiscarded(); // Reste verworfener Modelle (E69) vom letzten Mal
diff --git a/app/src/main/paths.ts b/app/src/main/paths.ts
index 51d6aca..d590925 100644
--- a/app/src/main/paths.ts
+++ b/app/src/main/paths.ts
@@ -1,4 +1,5 @@
import { app } from 'electron';
+import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import type { SkillDirs } from './skills';
@@ -48,11 +49,23 @@ export const libraryBackupsDir = () => path.join(backupsDir(), 'skills-library')
export const appIcon = () => path.join(app.getAppPath(), 'resources', process.platform === 'win32' ? 'icon.ico' : 'icon.png');
/**
- * Python der Basisumgebung. Reihenfolge: SOMA_PYTHON, Entwicklungs-Umgebung (python/scripts/setup_env.ps1), sonst `python` im PATH.
- * Die mitgelieferte Basisumgebung ersetzt das spaeter (Auslieferung ist noch offen).
+ * Mitgelieferte Basisumgebung im Installationspaket: eigenständiges Python mit soma, gebaut von
+ * python/scripts/build_runtime.py, von electron-builder nach resources/python gelegt. Nur in der installierten App.
+ */
+export function bundledPythonDir(): string | null {
+ if (!app.isPackaged) return null;
+ const dir = path.join(process.resourcesPath, 'python');
+ return fs.existsSync(path.join(dir, process.platform === 'win32' ? 'python.exe' : path.join('bin', 'python3'))) ? dir : null;
+}
+
+/**
+ * Python der Basisumgebung. Reihenfolge: SOMA_PYTHON, mitgelieferte Basisumgebung, Entwicklungs-Umgebung
+ * (python/scripts/setup_env.ps1), sonst `python` im PATH.
*/
export function pythonExe(): string {
if (process.env.SOMA_PYTHON) return process.env.SOMA_PYTHON;
+ const bundled = bundledPythonDir();
+ if (bundled) return path.join(bundled, process.platform === 'win32' ? 'python.exe' : path.join('bin', 'python3'));
if (process.platform === 'win32' && process.env.LOCALAPPDATA) {
const dev = path.join(process.env.LOCALAPPDATA, 'soma-dev', 'venv', 'Scripts', 'python.exe');
if (fs.existsSync(dev)) return dev;
@@ -68,8 +81,32 @@ export function gitCmdDir(): string | null {
} catch { return null; }
}
-/** Ordner mit python und soma-Startprogramm, kommt bei Terminals an den Anfang von PATH. */
-export function pythonBinDir(): string | null {
+/**
+ * Ordner mit python und den Startprogrammen (soma, pip), kommen bei Terminals an den Anfang von PATH. In der venv der
+ * Entwicklung liegt alles in einem Ordner, im eigenständigen Python unter Windows die Startprogramme in Scripts\.
+ */
+export function pythonBinDirs(): string[] {
const exe = pythonExe();
- return path.isAbsolute(exe) ? path.dirname(exe) : null;
+ if (!path.isAbsolute(exe)) return [];
+ const dir = path.dirname(exe);
+ return process.platform === 'win32' && !process.env.SOMA_PYTHON && dir === bundledPythonDir() ? [path.join(dir, 'Scripts'), dir] : [dir];
+}
+
+/**
+ * Programme aus der App heraus sehen unter macOS (und je nach Desktop unter Linux) nur einen knappen PATH ohne
+ * /opt/homebrew/bin, ~/.local/bin oder die Ordner von npm. Agenten würden weder erkannt noch in den Terminals gefunden.
+ * Deshalb einmal beim Start den PATH einer Login-Shell übernehmen. Nur in der installierten App: Aus einem Terminal
+ * gestartet stimmt der PATH schon. Scheitert die Shell (Zeitlimit, ungewohnte Ausgabe), bleibt der PATH, wie er ist.
+ */
+export function adoptLoginShellPath(): void {
+ if (process.platform === 'win32' || !app.isPackaged) return;
+ const shell = process.env.SHELL || '/bin/bash';
+ try {
+ const r = spawnSync(shell, ['-ilc', 'printf "\\n__SOMA_PATH__=%s\\n" "$PATH"'], { encoding: 'utf8', timeout: 5000, stdio: ['ignore', 'pipe', 'ignore'] });
+ const found = /__SOMA_PATH__=(.*)/.exec(r.stdout ?? '')?.[1]?.trim();
+ // fish gibt PATH als Liste mit Leerzeichen aus, das passt nicht ins Format: dann lieber nichts ändern
+ if (!found || !found.includes('/') || (found.includes(' ') && !found.includes(':'))) return;
+ const merged = [...found.split(':'), ...(process.env.PATH ?? '').split(':')].filter((p) => p);
+ process.env.PATH = [...new Set(merged)].join(':');
+ } catch { /* PATH bleibt */ }
}
diff --git a/app/src/main/terminals.ts b/app/src/main/terminals.ts
index 3e51da2..1ef4b8b 100644
--- a/app/src/main/terminals.ts
+++ b/app/src/main/terminals.ts
@@ -3,7 +3,7 @@ import type { WebContents } from 'electron';
import * as pty from 'node-pty';
import { bridgeEnv } from './bridge';
import { envPaths } from './packages';
-import { gitCmdDir, pythonBinDir } from './paths';
+import { gitCmdDir, pythonBinDirs } from './paths';
/**
* Umgebung fuer Terminals: vorn im PATH die Projektumgebung (E67, `python` sieht Basis und Zusatzpakete), dahinter die
@@ -17,8 +17,7 @@ export function terminalEnv(projectDir: string): NodeJS.ProcessEnv {
const front: string[] = [pe.scripts];
env.VIRTUAL_ENV = pe.env;
env.PIP_PYTHON = pe.python;
- const bin = pythonBinDir();
- if (bin) front.push(bin);
+ front.push(...pythonBinDirs());
const git = gitCmdDir(); // das mitgelieferte Git hat Vorrang vor einem Git des Systems
if (git) front.push(git);
env[key] = [...front, env[key] ?? ''].join(path.delimiter);