From 21068e8282f97a50b35070a49b0a8b76c4b44116 Mon Sep 17 00:00:00 2001 From: Aaron Villalpando Date: Tue, 19 Nov 2024 22:10:40 -0800 Subject: [PATCH 1/2] Fix nextjs and TS server hot-reload --- .../language-server/src/bamlConfig.ts | 2 +- .../src/lib/baml_project_manager.ts | 23 ++++++++++++++++++- typescript/vscode-ext/packages/package.json | 4 ++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/typescript/vscode-ext/packages/language-server/src/bamlConfig.ts b/typescript/vscode-ext/packages/language-server/src/bamlConfig.ts index 197da23a2..82127298c 100644 --- a/typescript/vscode-ext/packages/language-server/src/bamlConfig.ts +++ b/typescript/vscode-ext/packages/language-server/src/bamlConfig.ts @@ -3,7 +3,7 @@ export const bamlConfigSchema = z .object({ cliPath: z.optional(z.string().nullable()).default(null), generateCodeOnSave: z.enum(['never', 'always']).default('always'), - restartTSServerOnSave: z.boolean().default(true), + restartTSServerOnSave: z.boolean().default(false), envCommand: z.string().default('env'), fileWatcher: z.boolean().default(false), trace: z.object({ diff --git a/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts b/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts index d9ee2fc55..97ec5537d 100644 --- a/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts +++ b/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts @@ -6,7 +6,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument' import { CompletionList, CompletionItem } from 'vscode-languageserver' import { exec } from 'child_process' -import { existsSync, readFileSync } from 'fs' +import { existsSync, readFileSync, utimes } from 'fs' import { URI } from 'vscode-uri' import { findTopLevelParent, gatherFiles } from '../file/fileUtils' @@ -417,6 +417,27 @@ class Project { await rename(g.output_dir, backupDir) } await rename(tmpDir, g.output_dir) + + try { + // some filewatchers don't trigger unless the file is touched. Creating the new dir alone doesn't work. + // if we remove this, TS will still have the old types, and nextjs will not hot-reload. + g.files.map((f) => { + const fpath = path.join(g.output_dir, f.path_in_output_dir) + const currentTime = new Date() + const newTime = new Date(currentTime.getTime() + 100) + utimes(fpath, newTime, newTime, (err) => { + if (err) { + console.log(`Error setting file times: ${err.message}`) + } + }) + }) + } catch (e) { + if (e instanceof Error) { + console.error(`Error setting file times: ${e.message}`) + } else { + console.error(`Error setting file times:`) + } + } await rm(backupDir, { recursive: true, force: true }) return g diff --git a/typescript/vscode-ext/packages/package.json b/typescript/vscode-ext/packages/package.json index 56a74b7c3..4502dfc16 100644 --- a/typescript/vscode-ext/packages/package.json +++ b/typescript/vscode-ext/packages/package.json @@ -49,8 +49,8 @@ }, "baml.restartTSServerOnSave": { "type": "boolean", - "default": true, - "description": "Restart the TypeScript server on save, to have it read the new generated types." + "default": false, + "description": "Restart the TypeScript server when generating baml_client code, in case it's not reading the new generated types." }, "baml.fileWatcher": { "scope": "window", From 7c891bcfb66b69845c9e44a355f0f991637357ba Mon Sep 17 00:00:00 2001 From: Aaron Villalpando Date: Tue, 19 Nov 2024 22:12:48 -0800 Subject: [PATCH 2/2] fix linter --- .../language-server/src/lib/baml_project_manager.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts b/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts index 97ec5537d..5ccae611c 100644 --- a/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts +++ b/typescript/vscode-ext/packages/language-server/src/lib/baml_project_manager.ts @@ -425,11 +425,11 @@ class Project { const fpath = path.join(g.output_dir, f.path_in_output_dir) const currentTime = new Date() const newTime = new Date(currentTime.getTime() + 100) - utimes(fpath, newTime, newTime, (err) => { - if (err) { - console.log(`Error setting file times: ${err.message}`) - } - }) + utimes(fpath, newTime, newTime, (err) => { + if (err) { + console.log(`Error setting file times: ${err.message}`) + } + }) }) } catch (e) { if (e instanceof Error) {