Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions de_shell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,23 @@ async def _main(
pass # the app claimed it
else:
log.warning("[backend] unknown message type: %s", msg_type)
except Exception as e:
log.exception("handling %s failed", msg_type)
ipc.emit_error(str(e))
except Exception:
what = _what_failed(msg)
log.exception("%s failed", what)
ipc.emit_error(f"{what} failed — details are in the log.")

session.shutdown()


def _what_failed(msg: dict) -> str:
"""What the status bar names when handling ``msg`` raised: the action for an
action, else the message type. The exception text (a KeyError shows as just
'name') goes to the log with its traceback instead."""
if msg.get("type") == "action":
return str(msg.get("action"))
return str(msg.get("type"))


def run(
build_session: Callable[[], object],
*,
Expand Down
5 changes: 4 additions & 1 deletion de_shell/js/main/envSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
* are buffered until the renderer listens — a progress line sent before React
* mounted is not lost.
*/
import { join } from 'path'

import { app, dialog, ipcMain } from 'electron'

import { channel, shellConfig } from './config'
import { runEnvSetupMachine } from './envSetupMachine'
import type { EnvSetupAnswer } from './envSetupMachine'
import { resolvePythonEnv } from './pythonEnv'
import { readLockedTorchVersion, resolvePythonEnv } from './pythonEnv'
import type { EnsureOptions, ResolvedPython } from './pythonEnv'
import type { ShellWindow } from './window'

Expand Down Expand Up @@ -56,6 +58,7 @@ export function runEnvSetup(
listenForAnswers()
return runEnvSetupMachine<ResolvedPython>({
initialWheelhouse: opts.wheelhouse ?? null,
torch: readLockedTorchVersion(join(opts.resourcesPath, 'python')) !== null,
run: (wheelhouse, onProgress) => resolvePythonEnv({
...opts,
wheelhouse,
Expand Down
24 changes: 24 additions & 0 deletions de_shell/js/main/envSetupMachine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,27 @@ describe('the setup state machine', () => {
assert.deepEqual(s.runs, ['E:/offline'])
})
})

describe('the PyTorch step', () => {
async function startMessage(torch: boolean | undefined) {
const out: EnvSetupWireMessage[] = []
await runEnvSetupMachine({
run: async () => 'ok',
emit: (m) => out.push(m),
waitForAnswer: async () => 'quit' as EnvSetupAnswer,
pickFolder: async () => null,
quit: () => {},
...(torch === undefined ? {} : { torch }),
})
return out.find((m) => m.state === 'start')
}

test('start says so when the app has no torch', async () => {
assert.equal((await startMessage(false))?.torch, false)
})

test('start leaves the default alone otherwise', async () => {
assert.equal((await startMessage(true))?.torch, undefined)
assert.equal((await startMessage(undefined))?.torch, undefined)
})
})
10 changes: 9 additions & 1 deletion de_shell/js/main/envSetupMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface EnvSetupWireMessage {
event?: EnvProgressEvent
/** The raw lines of this batch, newline-joined (progress only). */
line?: string
/** False when setup has no PyTorch step (start only). */
torch?: boolean
/** Human-readable failure (error only). */
message?: string
/** The wheelhouse folder being installed from, when there is one. */
Expand Down Expand Up @@ -117,6 +119,8 @@ export interface MachineDeps<R> {
/** Native folder picker; null when cancelled. */
pickFolder: () => Promise<string | null>
quit: () => void
/** False when the app's lock has no torch, so the overlay drops that step. */
torch?: boolean
/** Wheelhouse to start with (an app that already knows the machine is offline). */
initialWheelhouse?: string | null
intervalMs?: number
Expand All @@ -126,7 +130,11 @@ export async function runEnvSetupMachine<R>(deps: MachineDeps<R>): Promise<R> {
let wheelhouse = deps.initialWheelhouse ?? null
for (;;) {
const batcher = createProgressBatcher({ emit: deps.emit, intervalMs: deps.intervalMs })
deps.emit({ type: 'env_setup', state: 'start', ...(wheelhouse ? { wheelhouse } : {}) })
deps.emit({
type: 'env_setup', state: 'start',
...(wheelhouse ? { wheelhouse } : {}),
...(deps.torch === false ? { torch: false } : {}),
})
try {
const result = await deps.run(wheelhouse, batcher.push)
batcher.flush()
Expand Down
12 changes: 9 additions & 3 deletions de_shell/js/renderer/EnvSetupOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function EnvSetupOverlay({ setup, appName, envSetup, description }: EnvSe
}, [setup.lines])

const failed = !!setup.error
const tail = logTail(setup.lines)
// The raw failure (often a tool's exit code) goes to the end of the log,
// where it stays on screen; the headline says what to do instead.
const tail = logTail(failed ? [...setup.lines, String(setup.error)] : setup.lines)

return (
<div data-testid="env-setup-overlay" style={S.scrim}>
Expand All @@ -49,7 +51,7 @@ export function EnvSetupOverlay({ setup, appName, envSetup, description }: EnvSe
</div>

{failed ? (
<div data-testid="env-setup-error" style={S.error}>{setup.error}</div>
<div data-testid="env-setup-error" style={S.error}>{SETUP_FAILED_TEXT}</div>
) : (
<div style={S.blurb}>
{description ?? 'First launch only — downloading the analysis environment. '
Expand All @@ -61,7 +63,7 @@ export function EnvSetupOverlay({ setup, appName, envSetup, description }: EnvSe
{!failed && (
<>
<div style={S.pills}>
{phasePills(setup.phase).map((p) => (
{phasePills(setup.phase, setup.torch ?? true).map((p) => (
<span key={p.key} style={{
fontSize: 11, padding: '3px 9px', borderRadius: 999,
border: `1px solid ${p.state === 'todo' ? colors.border : colors.accent}`,
Expand Down Expand Up @@ -104,6 +106,10 @@ export function EnvSetupOverlay({ setup, appName, envSetup, description }: EnvSe
)
}

const SETUP_FAILED_TEXT =
'Details are in the setup log below. Press Retry. ' +
'If this PC has no internet, press Install from a folder…'

/** Indeterminate when percent is null; a filled bar when we have a %. */
function ProgressBar({ percent }: { percent: number | null }) {
return (
Expand Down
8 changes: 5 additions & 3 deletions de_shell/js/renderer/envSetupView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export const ENV_PHASES: ReadonlyArray<{ key: EnvPhase; label: string }> = [

export type PillState = 'done' | 'active' | 'todo'

export function phasePills(phase: EnvPhase) {
/** `torch` false leaves out the PyTorch pill, for an app with no torch in its lock. */
export function phasePills(phase: EnvPhase, torch = true) {
const phases = torch ? ENV_PHASES : ENV_PHASES.filter(p => p.key !== 'torch')
const active = phase === 'working' ? 'resolving' : phase
const activeIdx = ENV_PHASES.findIndex(p => p.key === active)
return ENV_PHASES.map((p, i) => ({
const activeIdx = phases.findIndex(p => p.key === active)
return phases.map((p, i) => ({
key: p.key,
label: p.label,
state: (i < activeIdx ? 'done' : i === activeIdx ? 'active' : 'todo') as PillState,
Expand Down
7 changes: 7 additions & 0 deletions de_shell/js/renderer/shellModels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ test('env_setup messages from runEnvSetup flow through the shell reducer', () =>
s = shellReducer(s, act({ state: 'done' }))
assert.equal(s.envSetup, null)
})

test('phasePills leaves out PyTorch for an app with no torch', () => {
assert.deepEqual(phasePills('installing', false).map(p => p.label),
['Resolve', 'Download', 'Install', 'Build'])
assert.deepEqual(phasePills('installing', false).map(p => p.state),
['done', 'done', 'active', 'todo'])
})
7 changes: 7 additions & 0 deletions de_shell/js/renderer/shellState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ describe('computing overlays and action state', () => {
})

describe('toShellAction', () => {
test('carries a setup with no PyTorch step through to the overlay state', () => {
const start = toShellAction({ type: 'env_setup', state: 'start', torch: false })!
assert.deepEqual(start, { type: 'ENV_SETUP_START', wheelhouse: null, torch: false })
assert.equal(shellReducer(appState, start).envSetup?.torch, false)
assert.equal(shellReducer(appState, { type: 'ENV_SETUP_START' }).envSetup?.torch, true)
})

test('maps the chrome messages', () => {
assert.deepEqual(toShellAction({ type: 'status', text: 'hi' }),
{ type: 'STATUS', text: 'hi' })
Expand Down
10 changes: 8 additions & 2 deletions de_shell/js/renderer/shellState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export interface EnvSetupState {
error?: string | null
/** Folder being installed from, once the user picked one. */
wheelhouse?: string | null
/** Whether setup has a PyTorch step; false for an app with no torch in its
* lock. Absent means true, which is what older mains imply. */
torch?: boolean
}

export interface ShellState {
Expand Down Expand Up @@ -125,7 +128,7 @@ export type ShellAction =
| { type: 'LOG_CLEAR' }
| { type: 'LOG_LEVEL'; level: string }
| { type: 'BACKEND_EXITED'; code: number | null; reason?: string }
| { type: 'ENV_SETUP_START'; wheelhouse?: string | null }
| { type: 'ENV_SETUP_START'; wheelhouse?: string | null; torch?: boolean }
| { type: 'ENV_SETUP_ERROR'; message: string; wheelhouse?: string | null }
| { type: 'ENV_SETUP_PROGRESS'; phase?: EnvPhase; step?: string; percent: number | null; raw: string }
| { type: 'ENV_SETUP_DONE' }
Expand Down Expand Up @@ -199,6 +202,7 @@ export function shellReducer<S extends ShellState>(state: S, action: ShellAction
lines: [],
error: null,
wheelhouse: action.wheelhouse ?? null,
torch: action.torch ?? true,
},
status: 'Setting up the analysis environment…',
}
Expand Down Expand Up @@ -319,7 +323,9 @@ export function toShellAction(msg: Record<string, unknown>): ShellAction | null
// flat shape some apps still emit themselves.
const wheelhouse = (msg.wheelhouse ?? null) as string | null
const st = msg.state ?? (typeof msg.event === 'string' ? msg.event : 'progress')
if (st === 'start') return { type: 'ENV_SETUP_START', wheelhouse }
if (st === 'start') {
return { type: 'ENV_SETUP_START', wheelhouse, ...(msg.torch === false ? { torch: false } : {}) }
}
if (st === 'done') return { type: 'ENV_SETUP_DONE' }
if (st === 'error') {
return { type: 'ENV_SETUP_ERROR', message: String(msg.message ?? 'Setup failed'), wheelhouse }
Expand Down
9 changes: 9 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pytest

from de_shell.actions import figure_registry
from de_shell.app import _what_failed
from de_shell.actions.lifecycle import (
bump_generation, is_current, progress_emitter, replace_tree_attr,
run_on_worker, window_computing,
Expand Down Expand Up @@ -292,3 +293,11 @@ def test_registering_the_same_evictor_twice_is_idempotent(self):
assert seen == [4244], "a module importing twice must not double-evict"
finally:
figure_registry._EVICTORS.remove(seen.append)


class TestFailureText:
def test_an_action_is_named_by_its_action(self):
assert _what_failed({"type": "action", "action": "find_vectors"}) == "find_vectors"

def test_another_message_is_named_by_its_type(self):
assert _what_failed({"type": "figure_event", "fig_id": "f"}) == "figure_event"
1 change: 1 addition & 0 deletions upcoming_changes/40.bugfix.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A failed first-run setup tells the user what to do next instead of showing a tool's exit code, which moves to the end of the setup log.
1 change: 1 addition & 0 deletions upcoming_changes/40.bugfix.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When handling a message fails, the status bar names the action that failed instead of showing the raw exception text.
1 change: 1 addition & 0 deletions upcoming_changes/40.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
First-run setup no longer shows a PyTorch step in apps that do not install PyTorch.
Loading