@@ -12,7 +12,6 @@ import {
1212 type CanonicalWorkflow ,
1313 type ChildResultFields ,
1414 calledWorkflowKey ,
15- calledWorkflowKeys ,
1615 catalogBlock ,
1716 childResultFields ,
1817 codeFrame ,
@@ -50,9 +49,11 @@ import {
5049 readSourceText ,
5150 unchangedTexts ,
5251 type Written ,
52+ workspaceFolderOf ,
5353} from './source-dir'
5454import { STRICT_FINDINGS } from './source-strict'
5555import {
56+ cachedCatalog ,
5657 cachedSourceCatalog ,
5758 type LocalDeclarations ,
5859 refreshTsconfigs ,
@@ -61,7 +62,13 @@ import {
6162} from './source-tree'
6263import { type TypeCheck , typeCheck } from './source-types'
6364import { type Diagnostic , findingsOutput } from './source-ui'
64- import { blockDigest , readStoredBlockDigests , readStoredEdges } from './stored-edges'
65+ import {
66+ blockDigest ,
67+ type ChildResults ,
68+ compiledWithChildResults ,
69+ readStoredBlockDigests ,
70+ readStoredEdges ,
71+ } from './stored-edges'
6572import { lockRecoveryHint , readSourceFiles } from './workflow-dir'
6673import {
6774 type DeclaredColumn ,
@@ -459,13 +466,13 @@ function declarationFindings(
459466/**
460467 * Compiles the workflow source directory `dir` against the catalog its base names when that one is
461468 * cached (a workflow holding a type the workspace catalog lacks was printed with its own), else
462- * `catalog`, checking env names against the names `secrets.ts` declares when `declarations` hold
463- * them.
469+ * `catalog`, checking env names against `envNames` (what `secrets.ts` declares) when given. Its
470+ * calls are untyped: { @link compiledWithChildResults} types them.
464471 */
465472async function compileWorkflowDir (
466473 dir : string ,
467474 workspaceCatalog : BlockCatalog ,
468- declarations ?: WorkspaceDeclarations
475+ envNames ?: ReadonlySet < string >
469476) {
470477 const lockText = await readSourceText ( dir , SOURCE_LOCK_FILE )
471478 const parsedLock = lockText === null ? null : parseWorkflowLock ( lockText )
@@ -477,8 +484,6 @@ async function compileWorkflowDir(
477484 cachedSourceCatalog ( dir ) ,
478485 ] )
479486 const catalog = printedWith ?? workspaceCatalog
480- const envNames = declarations ?. envNames
481- const childResults = declarations ?. childResults
482487 const input : Parameters < typeof compileWorkflow > [ 0 ] = {
483488 entry : { path : SOURCE_ENTRY_FILE , text : files [ SOURCE_ENTRY_FILE ] } ,
484489 files : new Map ( Object . entries ( files ) . filter ( ( [ path ] ) => path !== SOURCE_ENTRY_FILE ) ) ,
@@ -489,25 +494,20 @@ async function compileWorkflowDir(
489494 ...( storedEdges ? { storedUnreadEdges : storedEdges } : { } ) ,
490495 ...( envNames ? { envNames, envDeclaredIn : KIND_DIRS . secret } : { } ) ,
491496 }
492- const compiled = compileWorkflow ( { ...input , ...( childResults ? { childResults } : { } ) } )
493- // `.sim/stored-edges.json` digests each block as the printed source compiles with no child
494- // results, so a block typed by one counts as changed only when its source did.
495- const typesCall =
496- compiled . workflow !== null &&
497- calledWorkflowKeys ( compiled . workflow ) . some ( ( key ) => childResults ?. has ( key ) )
498- const digested = typesCall ? compileWorkflow ( input ) . workflow : compiled . workflow
499497 return {
500498 lock,
501499 lockProblems : parsedLock ?. diagnostics ?? [ ] ,
502500 files,
503- compiled ,
504- digested ,
501+ input ,
502+ compiled : compileWorkflow ( input ) ,
505503 storedBlocks,
506504 printedWith,
507505 catalog,
508506 }
509507}
510508
509+ type CompiledWorkflowDir = Awaited < ReturnType < typeof compileWorkflowDir > >
510+
511511/** What {@link readCheckDeclarations} reads: the declarations and the compiled workflows' facts. */
512512type CheckDeclarations = WorkspaceDeclarations &
513513 Awaited < ReturnType < typeof compileWorkspaceWorkflows > >
@@ -518,33 +518,49 @@ type CheckDeclarations = WorkspaceDeclarations &
518518 * (`startInputs`, `null` without a Start trigger), which `workspace.d.ts` also types
519519 * `inputMapping` with, the keys of those that end at a Response block on every run, whose
520520 * `result` is that block's output, and the typed fields of the data each of those responds with,
521- * which type a call's `<call.result.data.<field>>`.
521+ * which type a call's `<call.result.data.<field>>`. Each compile is kept (`compiledWorkflows`, by
522+ * key) for {@link checkWorkflowDir}: env names change only its findings, never its blocks.
522523 */
523524export async function readCheckDeclarations (
524525 root : string ,
525526 catalog : BlockCatalog
526527) : Promise < CheckDeclarations > {
527- const [ declared , workflows ] = await Promise . all ( [
528- readWorkspaceDeclarations ( root ) ,
529- compileWorkspaceWorkflows ( root , catalog ) ,
530- ] )
531- return { ...declared , ...workflows }
528+ const declared = await readWorkspaceDeclarations ( root )
529+ return { ...declared , ...( await compileWorkspaceWorkflows ( root , catalog , declared . envNames ) ) }
532530}
533531
534- async function compileWorkspaceWorkflows ( root : string , catalog : BlockCatalog ) {
532+ async function compileWorkspaceWorkflows (
533+ root : string ,
534+ catalog : BlockCatalog ,
535+ envNames ?: ReadonlySet < string >
536+ ) {
535537 const workflowInputs = new Map < string , WorkflowInput [ ] | null > ( )
536538 const respondingWorkflows = new Set < string > ( )
537539 const childResults = new Map < string , ChildResultFields > ( )
540+ const compiledWorkflows = new Map < string , CompiledWorkflowDir > ( )
538541 for ( const key of await workspaceWorkflowKeys ( root ) ) {
539- const dir = await compileWorkflowDir ( join ( root , KIND_DIRS . workflow , key ) , catalog )
542+ const dir = await compileWorkflowDir ( join ( root , KIND_DIRS . workflow , key ) , catalog , envNames )
543+ compiledWorkflows . set ( key , dir )
540544 const { workflow } = dir . compiled
541545 if ( ! workflow ) continue
542546 workflowInputs . set ( key , startInputs ( workflow ) )
543547 if ( respondsOnEveryRun ( workflow ) ) respondingWorkflows . add ( key )
544548 const fields = childResultFields ( workflow , dir . catalog )
545549 if ( fields ) childResults . set ( key , fields )
546550 }
547- return { workflowInputs, respondingWorkflows, childResults }
551+ return { workflowInputs, respondingWorkflows, childResults, compiledWorkflows }
552+ }
553+
554+ /**
555+ * The child results that type the calls of a workflow written into `dir`, as a check of it types
556+ * them: those of the workspace directory it is a workflow folder of, compiled from disk against
557+ * the workspace's cached catalog; `undefined` for a workflow directory of its own, which has none.
558+ */
559+ export async function folderChildResults ( dir : string ) : Promise < ChildResults | undefined > {
560+ const folder = await workspaceFolderOf ( dir )
561+ const catalog = folder && cachedCatalog ( folder . workspaceId )
562+ if ( ! folder || ! catalog ) return undefined
563+ return ( await compileWorkspaceWorkflows ( folder . root , catalog ) ) . childResults
548564}
549565
550566/**
@@ -618,10 +634,23 @@ async function writtenWorkspaceTypes(
618634export async function checkWorkflowDir (
619635 dir : string ,
620636 catalog : BlockCatalog ,
621- options : { declarations ?: WorkspaceDeclarations ; strict : StrictMode ; prefix ?: string }
637+ options : {
638+ declarations ?: CheckDeclarations
639+ strict : StrictMode
640+ /** The key of the workflow folder `dir` is, whose compile `declarations` hold. */
641+ key ?: string
642+ prefix ?: string
643+ }
622644) : Promise < { findings : CheckFinding [ ] ; entry : string ; unlisted : UnlistedCounts } > {
623- const compiledDir = await compileWorkflowDir ( dir , catalog , options . declarations )
624- const { lock, lockProblems, files, compiled, digested, storedBlocks, printedWith } = compiledDir
645+ const { declarations } = options
646+ const untyped =
647+ ( options . key === undefined ? undefined : declarations ?. compiledWorkflows . get ( options . key ) ) ??
648+ ( await compileWorkflowDir ( dir , catalog , declarations ?. envNames ) )
649+ const compiledDir = {
650+ ...untyped ,
651+ compiled : compiledWithChildResults ( untyped . input , untyped . compiled , declarations ?. childResults ) ,
652+ }
653+ const { lock, lockProblems, files, compiled, storedBlocks, printedWith } = compiledDir
625654 const entryText = files [ SOURCE_ENTRY_FILE ]
626655 const locate = ( block : string , param : string , names : string | undefined ) => {
627656 const prefix = encodeAnchor ( { kind : 'param' , block, param : '' } )
@@ -685,7 +714,7 @@ export async function checkWorkflowDir(
685714 */
686715 const changed = ( blockKey : string ) => {
687716 if ( ! bound ( blockKey ) || moved ( blockKey ) ) return true
688- const block = digested ?. blocks [ blockKey ]
717+ const block = compiled . workflow ?. blocks [ blockKey ]
689718 const printed = printedWith ? storedBlocks ?. get ( blockKey ) : undefined
690719 return block !== undefined && printed !== undefined && blockDigest ( block ) !== printed
691720 }
0 commit comments