diff --git a/packages/sim-cli/src/commands/protocol/source-tree.ts b/packages/sim-cli/src/commands/protocol/source-tree.ts index c38bf266ce5..89364534640 100644 --- a/packages/sim-cli/src/commands/protocol/source-tree.ts +++ b/packages/sim-cli/src/commands/protocol/source-tree.ts @@ -751,12 +751,14 @@ function fullIndexPath(cacheIndex: string | null): string { * block types, and rewrites `tsconfig.json` only when it exists (`tsconfig.full.json`, which names * this machine's cache, is written again when missing, as a fresh clone lacks it). It reads the * cached catalog, so it costs no request; without one nothing changes until the next get. A file - * whose text would not change is not rewritten. + * whose text would not change is not rewritten. Declarations another catalog or CLI build wrote + * (after `sim update`, or a committed `.sim/sdk`) are replaced as a get replaces them, so every + * block `tsconfig.json` includes is declared; `workspace.d.ts`, which needs the server, is kept. */ export async function refreshTsconfigs( dir: string, workspaceId: string, - profile: Pick, + profile: Pick, use: SourceTreeUse, options: { incremental?: boolean } = {} ): Promise { @@ -786,9 +788,9 @@ export async function refreshTsconfigs( [TSCONFIG_FILE, tsconfig], [TSCONFIG_FULL_FILE, full], ]) - // Declarations of another catalog or generator are left for the next get, which replaces them. + const sdkCurrent = index?.startsWith(`${sdkIndexHead(catalog)}\n`) === true const declarations = - cacheIndex !== null && index?.startsWith(`${sdkIndexHead(catalog)}\n`) + sdkCurrent && cacheIndex !== null ? localSdkFiles(catalog, used, true).filter(([path]) => path.startsWith(`${SDK_DIR}/blocks/`)) : [] const texts = await mapWithConcurrency(declarations, FILE_CONCURRENCY, ([path]) => @@ -800,7 +802,19 @@ export async function refreshTsconfigs( current.get(path) !== text && (!options.incremental || path !== TSCONFIG_FILE || current.get(path) !== null) ) - if (changed.length > 0) await updateSourceDir(dir, { write: changed }) + if (sdkCurrent) { + if (changed.length > 0) await updateSourceDir(dir, { write: changed }) + return + } + await updateSourceDir(dir, { + write: [ + ...changed, + [`${SDK_DIR}/core.d.ts`, CORE_DTS], + ...(profile.sdk ?? []), + ...localSdkFiles(catalog, used, cacheIndex !== null), + ], + remove: [`${SDK_DIR}/blocks`], + }) } const INCLUDED_BLOCK = new RegExp(`^${escapeRegExp(SDK_DIR)}/blocks/(.+)\\.d\\.ts$`) diff --git a/packages/sim-cli/src/commands/protocol/workflow-source-get.test.ts b/packages/sim-cli/src/commands/protocol/workflow-source-get.test.ts index 4921b481d3d..5c6d17a7094 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-source-get.test.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-source-get.test.ts @@ -709,6 +709,49 @@ describe('sim workflows source get --dir', () => { ]) }) + it('types a block the edit added in a check after another CLI build wrote the declarations', async () => { + const workflow = [ + '// @sim-sdk 1', + "import { trigger, workflow } from '@sim/workflow-sdk'", + '', + "export default workflow('W', {}, () => {", + " const start = trigger.start('Start', {})", + '})', + '', + ].join('\n') + serve({ [SOURCE_PATH]: () => source({ 'workflow.ts': workflow }, []) }) + await get('--dir', outDir) + const index = join(outDir, '.sim/sdk/index.d.ts') + writeFileSync( + index, + readFileSync(index, 'utf8').replace(/core\.d\.ts [0-9a-f]{16}/, 'core.d.ts 28b78b775e892665') + ) + writeFileSync( + join(outDir, 'workflow.ts'), + workflow + .replace('{ trigger, workflow }', '{ api, trigger, workflow }') + .replace( + " const start = trigger.start('Start', {})\n", + " const start = trigger.start('Start', {})\n const request = api('Request', { url: 'https://example.com', method: 'GET' })\n start.to(request)\n" + ) + ) + contextMockFns.mockProfileFrom.mockReturnValue({ + ...mockedProfile(), + workspaceId: WORKSPACE_ID, + output: 'json', + }) + const check = new Command('source') + attachWorkflowSourceCheck(check) + check.commands.forEach((command) => command.exitOverride()) + await check.parseAsync(['node', 'source', 'check', '--dir', outDir]) + + const printed = JSON.parse(vi.mocked(console.log).mock.calls.at(-1)?.join(' ') ?? '{}') + expect(printed.diagnostics).toEqual([]) + expect(process.exitCode).toBeUndefined() + expect(existsSync(join(outDir, '.sim/sdk/blocks/api.d.ts'))).toBe(true) + expect(readFileSync(index, 'utf8')).not.toContain('core.d.ts 28b78b775e892665') + }) + it('names the refresh when a row write names a column the table types the last get wrote lack', async () => { const workflow = [ '// @sim-sdk 1',