initPromise in main.ts (#6285)

* initPromise in main.ts

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* move initPromise so dep tree doesnt shit the bed

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2025-04-11 11:17:46 -07:00
committed by GitHub
parent 319c60d4fa
commit c0aa763c3b
32 changed files with 123 additions and 72 deletions

View File

@ -20,7 +20,7 @@ import type {
} from '@src/editor/plugins/lsp/types'
import { LspWorker } from '@src/editor/plugins/lsp/types'
import Worker from '@src/editor/plugins/lsp/worker.ts?worker'
import { wasmUrl } from '@src/lang/wasm'
import { wasmUrl } from '@src/lang/wasmUtils'
import { PROJECT_ENTRYPOINT } from '@src/lib/constants'
import { isDesktop } from '@src/lib/isDesktop'
import { PATHS } from '@src/lib/paths'

View File

@ -1,5 +1,6 @@
import { processMemory } from '@src/components/ModelingSidebar/ModelingPanes/MemoryPane'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
beforeAll(async () => {

View File

@ -7,7 +7,7 @@ import ModalContainer from 'react-modal-promise'
import { Router } from '@src/Router'
import { ToastUpdate } from '@src/components/ToastUpdate'
import '@src/index.css'
import { initPromise } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { AUTO_UPDATER_TOAST_ID } from '@src/lib/constants'
import { initializeWindowExceptionHandler } from '@src/lib/exceptions'
import { isDesktop } from '@src/lib/isDesktop'

View File

@ -30,13 +30,8 @@ import type {
SourceRange,
VariableMap,
} from '@src/lang/wasm'
import {
emptyExecState,
getKclVersion,
initPromise,
parse,
recast,
} from '@src/lang/wasm'
import { emptyExecState, getKclVersion, parse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import type { ArtifactIndex } from '@src/lib/artifactIndex'
import { buildArtifactIndex } from '@src/lib/artifactIndex'
import {

View File

@ -1,4 +1,5 @@
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
beforeAll(async () => {

View File

@ -4,7 +4,8 @@ import { KCLError } from '@src/lang/errors'
import { defaultArtifactGraph } from '@src/lang/std/artifactGraph'
import { topLevelRange } from '@src/lang/util'
import type { Sketch } from '@src/lang/wasm'
import { assertParse, initPromise, sketchFromKclValue } from '@src/lang/wasm'
import { assertParse, sketchFromKclValue } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
beforeAll(async () => {

View File

@ -5,7 +5,8 @@ import { ARG_INDEX_FIELD, LABELED_ARG_FIELD } from '@src/lang/queryAstConstants'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import { topLevelRange } from '@src/lang/util'
import type { Parameter } from '@src/lang/wasm'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { err } from '@src/lib/trap'
beforeAll(async () => {

View File

@ -1,7 +1,8 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
// The purpose of these tests is to act as a first line of defense

View File

@ -28,7 +28,8 @@ import { codeRefFromRange } from '@src/lang/std/artifactGraph'
import type { InputArgKeys, SimplifiedArgDetails } from '@src/lang/std/stdTypes'
import { topLevelRange } from '@src/lang/util'
import type { Identifier, Literal, LiteralValue } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -27,7 +27,8 @@ import type {
SourceRange,
VariableDeclarator,
} from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import type { Selection, Selections } from '@src/lib/selections'
import {
codeManager,

View File

@ -24,7 +24,8 @@ import { codeRefFromRange } from '@src/lang/std/artifactGraph'
import { addCallExpressionsToPipe, addCloseToPipe } from '@src/lang/std/sketch'
import { topLevelRange } from '@src/lang/util'
import type { Identifier, PathToNode } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -1,5 +1,6 @@
import { getIdentifiersInProgram } from '@src/lang/queryAst/getIndentifiersInProgram'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
function identifier(name: string, start: number, end: number) {
return {

View File

@ -1,5 +1,6 @@
import { getSafeInsertIndex } from '@src/lang/queryAst/getSafeInsertIndex'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
beforeAll(async () => {
await initPromise

View File

@ -1,5 +1,6 @@
import { getTagDeclaratorsInProgram } from '@src/lang/queryAst/getTagDeclaratorsInProgram'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
function tagDeclaratorWithIndex(
value: string,

View File

@ -1,7 +1,8 @@
import fs from 'node:fs'
import type { Program } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { err } from '@src/lib/trap'
beforeAll(async () => {

View File

@ -14,7 +14,8 @@ import {
} from '@src/lang/std/sketch'
import { topLevelRange } from '@src/lang/util'
import type { CallExpression, CallExpressionKw } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -7,12 +7,8 @@ import {
} from '@src/lang/std/sketchcombos'
import { topLevelRange } from '@src/lang/util'
import type { Sketch, SourceRange } from '@src/lang/wasm'
import {
assertParse,
initPromise,
recast,
sketchFromKclValue,
} from '@src/lang/wasm'
import { assertParse, recast, sketchFromKclValue } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -19,7 +19,8 @@ import {
} from '@src/lang/std/sketchcombos'
import { findKwArg, topLevelRange } from '@src/lang/util'
import type { Expr, Program } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import type { Selection, Selections } from '@src/lib/selections'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -1,4 +1,5 @@
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
beforeAll(async () => {

View File

@ -2,7 +2,8 @@ import type { Node } from '@rust/kcl-lib/bindings/Node'
import type { Program } from '@rust/kcl-lib/bindings/Program'
import type { ParseResult } from '@src/lang/wasm'
import { formatNumber, initPromise, parse } from '@src/lang/wasm'
import { formatNumber, parse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { enginelessExecutor } from '@src/lib/testHelpers'
import { err } from '@src/lib/trap'

View File

@ -50,7 +50,6 @@ import {
format_number,
get_kcl_version,
get_tangential_arc_to_info,
init,
is_kcl_empty_or_only_settings,
is_points_ccw,
kcl_lint,
@ -59,7 +58,6 @@ import {
parse_project_settings,
parse_wasm,
recast_wasm,
reloadModule,
serialize_configuration,
serialize_project_configuration,
} from '@src/lib/wasm_lib_wrapper'
@ -158,36 +156,6 @@ function firstSourceRange(error: RustKclError): SourceRange {
: defaultSourceRange()
}
export const wasmUrl = () => {
// For when we're in electron (file based) or web server (network based)
// For some reason relative paths don't work as expected. Otherwise we would
// just do /wasm_lib_bg.wasm. In particular, the issue arises when the path
// is used from within worker.ts.
const fullUrl = document.location.protocol.includes('http')
? document.location.origin + '/kcl_wasm_lib_bg.wasm'
: document.location.protocol +
document.location.pathname.split('/').slice(0, -1).join('/') +
'/kcl_wasm_lib_bg.wasm'
return fullUrl
}
// Initialise the wasm module.
const initialise = async () => {
try {
await reloadModule()
const fullUrl = wasmUrl()
const input = await fetch(fullUrl)
const buffer = await input.arrayBuffer()
return await init({ module_or_path: buffer })
} catch (e) {
console.log('Error initialising WASM', e)
return Promise.reject(e)
}
}
export const initPromise = initialise()
const splitErrors = (
input: CompilationError[]
): { errors: CompilationError[]; warnings: CompilationError[] } => {

View File

@ -2,6 +2,37 @@ import {
import_file_extensions,
relevant_file_extensions,
} from '@rust/kcl-wasm-lib/pkg/kcl_wasm_lib'
import { init, reloadModule } from '@src/lib/wasm_lib_wrapper'
export const wasmUrl = () => {
// For when we're in electron (file based) or web server (network based)
// For some reason relative paths don't work as expected. Otherwise we would
// just do /wasm_lib_bg.wasm. In particular, the issue arises when the path
// is used from within worker.ts.
const fullUrl = document.location.protocol.includes('http')
? document.location.origin + '/kcl_wasm_lib_bg.wasm'
: document.location.protocol +
document.location.pathname.split('/').slice(0, -1).join('/') +
'/kcl_wasm_lib_bg.wasm'
return fullUrl
}
// Initialise the wasm module.
const initialise = async () => {
try {
await reloadModule()
const fullUrl = wasmUrl()
const input = await fetch(fullUrl)
const buffer = await input.arrayBuffer()
return await init({ module_or_path: buffer })
} catch (e) {
console.log('Error initialising WASM', e)
return Promise.reject(e)
}
}
export const initPromise = initialise()
export function importFileExtensions(): string[] {
return import_file_extensions()

41
src/lang/wasmUtilsNode.ts Normal file
View File

@ -0,0 +1,41 @@
import { init, reloadModule } from '@src/lib/wasm_lib_wrapper'
import fs from 'fs'
import fsPromises from 'fs/promises'
import path from 'path'
export const wasmUrlNode = () => {
// In prod the file will be right next to the compiled js file.
const prodPath = path.join(__dirname, 'kcl_wasm_lib_bg.wasm')
// Check if the file exists.
if (fs.existsSync(prodPath)) {
console.log('Found wasm file in prod', prodPath)
return prodPath
}
// Get the wasm module from public/kcl_wasm_lib_bg.wasm
console.log('Using dev wasm file')
const devPath = path.join(
__dirname,
'..',
'..',
'public',
'kcl_wasm_lib_bg.wasm'
)
console.log('Using dev wasm file', devPath)
return devPath
}
// Initialise the wasm module.
const initialiseNode = async () => {
try {
await reloadModule()
const fullPath = wasmUrlNode()
const buffer = await fsPromises.readFile(fullPath)
return await init({ module_or_path: buffer })
} catch (e) {
console.log('Error initialising WASM', e)
return Promise.reject(e)
}
}
export const initPromiseNode = initialiseNode()

View File

@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { Configuration } from '@rust/kcl-lib/bindings/Configuration'
import { initPromise } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { listProjects } from '@src/lib/desktop'
import type { DeepPartial } from '@src/lib/types'

View File

@ -6,11 +6,10 @@ import type { ProjectConfiguration } from '@rust/kcl-lib/bindings/ProjectConfigu
import { newKclFile } from '@src/lang/project'
import {
defaultAppSettings,
initPromise,
parseAppSettings,
parseProjectSettings,
} from '@src/lang/wasm'
import { relevantFileExtensions } from '@src/lang/wasmUtils'
import { initPromise, relevantFileExtensions } from '@src/lang/wasmUtils'
import {
DEFAULT_DEFAULT_LENGTH_UNIT,
PROJECT_ENTRYPOINT,

View File

@ -3,7 +3,7 @@ import os from 'os'
import path from 'path'
import { v4 as uuidv4 } from 'uuid'
import { initPromise } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import getCurrentProjectFile from '@src/lib/getCurrentProjectFile'
beforeAll(async () => {

View File

@ -4,7 +4,8 @@ import { getNodeFromPath } from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import { topLevelRange } from '@src/lang/util'
import type { VariableDeclaration } from '@src/lang/wasm'
import { assertParse, initPromise, recast } from '@src/lang/wasm'
import { assertParse, recast } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { updateCenterRectangleSketch } from '@src/lib/rectangleTool'
import { trap } from '@src/lib/trap'

View File

@ -14,9 +14,9 @@ import type { ExecState } from '@src/lang/wasm'
import {
errFromErrWithOutputs,
execStateFromRust,
initPromise,
mockExecStateFromRust,
} from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import type ModelingAppFile from '@src/lib/modelingAppFile'
import type { DefaultPlaneStr } from '@src/lib/planes'
import { defaultPlaneStrToKey } from '@src/lib/planes'

View File

@ -3,7 +3,8 @@ import { expect } from 'vitest'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import type { Artifact } from '@src/lang/std/artifactGraph'
import type { ArtifactGraph, SourceRange } from '@src/lang/wasm'
import { assertParse, initPromise } from '@src/lang/wasm'
import { assertParse } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import type { ArtifactIndex } from '@src/lib/artifactIndex'
import { buildArtifactIndex } from '@src/lib/artifactIndex'
import type { Selection } from '@src/lib/selections'

View File

@ -7,12 +7,12 @@ import { TEST } from '@src/env'
import {
defaultAppSettings,
defaultProjectSettings,
initPromise,
parseAppSettings,
parseProjectSettings,
serializeConfiguration,
serializeProjectConfiguration,
} from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
import { mouseControlsToCameraSystem } from '@src/lib/cameraControls'
import { BROWSER_PROJECT_NAME } from '@src/lib/constants'
import {

View File

@ -1,5 +1,6 @@
import type { Coords2d } from '@src/lang/std/sketch'
import { initPromise, isPointsCCW } from '@src/lang/wasm'
import { isPointsCCW } from '@src/lang/wasm'
import { initPromise } from '@src/lang/wasmUtils'
beforeAll(async () => {
await initPromise

View File

@ -28,6 +28,7 @@ import {
getPathOrUrlFromArgs,
parseCLIArgs,
} from '@src/commandLineArgs'
import { initPromiseNode } from '@src/lang/wasmUtilsNode'
import { ZOO_STUDIO_PROTOCOL } from '@src/lib/constants'
import getCurrentProjectFile from '@src/lib/getCurrentProjectFile'
import { reportRejection } from '@src/lib/trap'
@ -512,6 +513,7 @@ app.on('ready', () => {
const getProjectPathAtStartup = async (
filePath?: string
): Promise<string | null> => {
await initPromiseNode
// If we are in development mode, we don't want to load a project at
// startup.
// Since the args passed are always '.'