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

windows path fixes2 #1311

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from
Open
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 @@ -212,11 +212,11 @@ class Project {
this.current_runtime = undefined
}

get_file(file_path: string) {
get_file(file_path: URI) {
// Read the file content
const fileContent = readFileSync(file_path, 'utf8')
const fileContent = readFileSync(file_path.fsPath, 'utf8')

const doc = TextDocument.create(file_path, 'plaintext', 1, fileContent)
const doc = TextDocument.create(file_path.fsPath, 'plaintext', 1, fileContent)

return doc
}
Expand Down Expand Up @@ -281,7 +281,7 @@ class Project {
end: { line: match.end_line, character: match.end_character },
}

const hoverDoc = this.get_file(URI.file(match.uri).toString())
const hoverDoc = this.get_file(URI.file(match.uri))

if (hoverDoc) {
const hoverText = hoverDoc.getText(range)
Expand Down Expand Up @@ -377,10 +377,11 @@ class Project {

const out_dir = path.join(this.rootPath(), g.output_dir_relative_to_baml_src)

const tmpDir = path.join(path.dirname(g.output_dir), path.basename(g.output_dir) + '.tmp')
const backupDir = path.join(path.dirname(g.output_dir), path.basename(g.output_dir) + '.bak')
const tmpDir = path.join(out_dir + '.tmp')
const backupDir = path.join(out_dir + '.bak')

await mkdir(tmpDir, { recursive: true })
console.log(`tmpdir ${tmpDir}`)
await Promise.all(
g.files.map(async (f) => {
const fpath = path.join(tmpDir, f.path_in_output_dir)
Expand All @@ -393,6 +394,7 @@ class Project {
await rm(backupDir, { recursive: true, force: true })
}
if (existsSync(out_dir)) {
console.log('out dir exists')
const contents = await readdir(out_dir, { withFileTypes: true })
const contentsWithSafeToRemove = await Promise.all(
contents.map(async (c) => {
Expand All @@ -403,7 +405,7 @@ class Project {
return { path: c.name, safeToRemove: false }
}

const handle = await open(path.join(g.output_dir, c.name))
const handle = await open(path.join(out_dir, c.name))
try {
const { bytesRead, buffer } = await handle.read(Buffer.alloc(1024), 0, 1024, 0)
const firstNBytes = buffer.subarray(0, bytesRead).toString('utf8')
Expand All @@ -420,7 +422,11 @@ class Project {
`Output dir ${g.output_dir} contains this file(s) not generated by BAML: ${notSafeToRemove.join(', ')}`,
)
}
await rename(out_dir, backupDir)
try {
await rename(out_dir, backupDir)
} catch (e) {
console.error(`Something happened backing up baml_client to the .bak directory. ${e}`)
}
}
await rename(tmpDir, out_dir)

Expand All @@ -429,7 +435,6 @@ class Project {
// 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(out_dir, f.path_in_output_dir)
console.log(`fpath ${fpath}`)
const currentTime = new Date()
const newTime = new Date(currentTime.getTime() + 100)
utimes(fpath, newTime, newTime, (err) => {
Expand Down Expand Up @@ -457,6 +462,7 @@ class Project {
onSuccess(`BAML client generated! (took ${Math.round(endMillis - startMillis)}ms)`)
}
} catch (e) {
console.error(`Failed to generate BAML client: ${e}`)
onError(`Failed to generate BAML client: ${e}`)
}
}
Expand Down
Loading