Skip to content

Commit

Permalink
Fix nextjs and TS server hot-reload (#1183)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Fixes Next.js and TypeScript server hot-reload by changing
`restartTSServerOnSave` default and modifying file timestamps.
> 
>   - **Behavior**:
> - Change `restartTSServerOnSave` default to `false` in `bamlConfig.ts`
and `package.json`.
> - Modify file timestamps in `baml_project_manager.ts` to trigger file
watchers for hot-reload.
>   - **Imports**:
>     - Add `utimes` import from `fs` in `baml_project_manager.ts`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 7c891bc. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
aaronvg authored Nov 20, 2024
1 parent 60c823a commit 22e6bbb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions typescript/vscode-ext/packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 22e6bbb

Please sign in to comment.