Files
daily_stock_analysis/scripts/build-backend.ps1
mumu 0924dce274 fix: detect stale static bundles in build pipeline and at runtime (#1064) (#1121)
* fix: detect stale static bundles in build pipeline and at runtime (#1064)

Add a packaging-time sanity check and a backend startup self-check that
verifies index.html only references /assets/*.js and /assets/*.css files
that actually exist on disk. This is the most common root cause of the
'Preparing backend...' / blank-page bug seen on the Windows immutable
Release packages: vite emits a fresh index.html with new content hashes,
but the desktop packaging step picks up a stale static/ directory, so
the browser receives a 404 (returned as application/json) for the main
bundle and refuses to execute it.

- scripts/check_static_assets.py: cross-platform sanity script.
- scripts/build-backend{.ps1,-macos.sh}: run the script after npm build
  on the source static/, and again after PyInstaller on the packaged
  static directory; fail the build on mismatch.
- api/app.py: log an actionable ERROR when an inconsistency is detected
  at startup so the root cause appears in logs/desktop.log instead of
  surfacing as a silent blank page.
- api/app.py: replace the StaticFiles mount with an explicit /assets/*
  route so misses return a plain-text 404 with a JS/CSS Content-Type,
  not the default JSON error response that masked diagnosis in #1064.
- tests/test_static_assets_consistency.py: 7 deterministic tests for
  both code paths.

Refs #1064 #1065 #1050
2026-04-25 20:22:59 +08:00

148 lines
3.9 KiB
PowerShell

$ErrorActionPreference = 'Stop'
Write-Host 'Building React UI (static assets)...'
Push-Location 'apps\dsa-web'
if (!(Test-Path 'node_modules')) {
npm install
}
npm run build
Pop-Location
$pythonBin = $env:PYTHON_BIN
if ([string]::IsNullOrWhiteSpace($pythonBin)) {
$pythonBin = 'python'
}
Write-Host "Using Python: $pythonBin"
Write-Host 'Verifying static asset references (source)...'
& $pythonBin "${PSScriptRoot}\check_static_assets.py" 'static'
if ($LASTEXITCODE -ne 0) {
throw "Static asset sanity check failed for source static/. See GitHub #1064."
}
function Test-PythonCode {
param(
[string]$Python,
[string]$Code
)
try {
& $Python -c $Code *> $null
return ($LASTEXITCODE -eq 0)
} catch {
return $false
}
}
Write-Host 'Building backend executable...'
if (-not (Test-PythonCode -Python $pythonBin -Code "import PyInstaller")) {
& $pythonBin -m pip install pyinstaller
}
Write-Host 'Installing backend dependencies...'
& $pythonBin -m pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) {
throw "pip install -r requirements.txt failed with exit code $LASTEXITCODE."
}
Write-Host 'Checking python-multipart availability...'
if (-not (Test-PythonCode -Python $pythonBin -Code "import multipart, multipart.multipart")) {
throw 'python-multipart is not importable in the selected Python environment.'
}
if (Test-Path 'dist\backend') {
Remove-Item -Recurse -Force 'dist\backend'
}
New-Item -ItemType Directory -Path 'dist\backend' | Out-Null
if (Test-Path 'dist\stock_analysis') {
Remove-Item -Recurse -Force 'dist\stock_analysis'
}
if (Test-Path 'build\stock_analysis') {
Remove-Item -Recurse -Force 'build\stock_analysis'
}
$hiddenImports = @(
'multipart',
'multipart.multipart',
'json_repair',
'tiktoken',
'tiktoken_ext',
'tiktoken_ext.openai_public',
'api',
'api.app',
'api.deps',
'api.v1',
'api.v1.router',
'api.v1.endpoints',
'api.v1.endpoints.analysis',
'api.v1.endpoints.history',
'api.v1.endpoints.stocks',
'api.v1.endpoints.health',
'api.v1.schemas',
'api.v1.schemas.analysis',
'api.v1.schemas.history',
'api.v1.schemas.stocks',
'api.v1.schemas.common',
'api.middlewares',
'api.middlewares.error_handler',
'src.services',
'src.services.task_queue',
'src.services.analysis_service',
'src.services.history_service',
'uvicorn.logging',
'uvicorn.loops',
'uvicorn.loops.auto',
'uvicorn.protocols',
'uvicorn.protocols.http',
'uvicorn.protocols.http.auto',
'uvicorn.protocols.websockets',
'uvicorn.protocols.websockets.auto',
'uvicorn.lifespan',
'uvicorn.lifespan.on'
)
$hiddenImportArgs = $hiddenImports | ForEach-Object { "--hidden-import=$_" }
$pyInstallerArgs = @(
'-m', 'PyInstaller',
'--name', 'stock_analysis',
'--onedir',
'--noconfirm',
'--noconsole',
'--add-data', 'static;static',
'--collect-data', 'litellm',
'--collect-data', 'tiktoken'
)
$pyInstallerArgs += $hiddenImportArgs
$pyInstallerArgs += 'main.py'
Write-Host "Running: $pythonBin $($pyInstallerArgs -join ' ')"
& $pythonBin @pyInstallerArgs
if ($LASTEXITCODE -ne 0) {
throw "PyInstaller failed with exit code $LASTEXITCODE."
}
if (!(Test-Path 'dist\stock_analysis')) {
throw 'PyInstaller finished but dist\stock_analysis was not generated.'
}
Copy-Item -Path 'dist\stock_analysis' -Destination 'dist\backend\stock_analysis' -Recurse -Force
Write-Host 'Verifying static asset references (packaged)...'
$packagedStatic = Join-Path 'dist\backend\stock_analysis' '_internal\static'
if (-not (Test-Path $packagedStatic)) {
$packagedStatic = Join-Path 'dist\backend\stock_analysis' 'static'
}
if (Test-Path $packagedStatic) {
& $pythonBin "${PSScriptRoot}\check_static_assets.py" $packagedStatic
if ($LASTEXITCODE -ne 0) {
throw "Static asset sanity check failed for packaged $packagedStatic. See GitHub #1064."
}
} else {
Write-Warning "Could not locate packaged static directory under dist\backend\stock_analysis; skipping post-package check."
}
Write-Host 'Backend build completed.'