Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nextjs and TS server hot-reload #1183

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using map here is misleading since the result is not used. Consider using forEach instead for clarity and performance.

Suggested change
g.files.map((f) => {
g.files.forEach((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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using fs.promises.utimes for better consistency and error handling.

Suggested change
utimes(fpath, newTime, newTime, (err) => {
await fs.promises.utimes(fpath, newTime, newTime)

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
Loading