Skip to content

Commit

Permalink
Fix windows generation through vscode always outputting to the root w…
Browse files Browse the repository at this point in the history
…orkspace directory if the path was ../ (#1247)

The issue for windows is that `g.output_dir` is always /baml_client when
the user set it to `../`. Rather than have rust try to do fancy things
like resolve the output directory (which doesn't seem to work in wasm in
windows), give VSCode the relative path to baml_src, since vscode can
build it from the project.path

<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Fixes file generation path issue in `baml_project_manager.ts` and
updates `dunce` package version in `Cargo.lock`.
> 
>   - **Behavior**:
> - Fixes file generation path issue in `baml_project_manager.ts` by
using `output_dir_relative_to_baml_src` for output directory.
>     - Logs output directory paths for debugging.
>   - **Dependencies**:
> - Updates `dunce` package version from `1.0.4` to `1.0.5` in
`Cargo.lock`.
> 
> <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 594a4cd. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
aaronvg authored Dec 16, 2024
1 parent 66af7c5 commit cdc1838
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions engine/baml-schema-wasm/src/runtime_wasm/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub struct WasmGeneratorOutput {
#[wasm_bindgen(readonly)]
pub output_dir: String,
#[wasm_bindgen(readonly)]
pub output_dir_relative_to_baml_src: String,
#[wasm_bindgen(readonly)]
pub files: Vec<WasmGeneratedFile>,
}

Expand All @@ -22,6 +24,10 @@ impl Into<WasmGeneratorOutput> for GenerateOutput {
fn into(self) -> WasmGeneratorOutput {
WasmGeneratorOutput {
output_dir: self.output_dir_full.to_string_lossy().to_string(),
output_dir_relative_to_baml_src: self
.output_dir_shorthand
.to_string_lossy()
.to_string(),
files: self
.files
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion engine/language_client_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ sugar_path = "1.2.0"
walkdir.workspace = true
semver = "1.0.23"
colored = "2.1.0"
itertools = "0.13.0"
itertools = "0.13.0"
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ class Project {
// Creating the tmpdir next to the output dir can cause some weird issues with vscode, if we recover
// from an error and delete the tmpdir - vscode's explorer UI will still show baml_client.tmp even
// though it doesn't exist anymore, and vscode has no good way of letting the user purge it from the UI
console.log(`outputdir ${g.output_dir}`)
console.log(`relative output dir ${g.output_dir_relative_to_baml_src}`)

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')

Expand All @@ -386,8 +391,8 @@ class Project {
if (existsSync(backupDir)) {
await rm(backupDir, { recursive: true, force: true })
}
if (existsSync(g.output_dir)) {
const contents = await readdir(g.output_dir, { withFileTypes: true })
if (existsSync(out_dir)) {
const contents = await readdir(out_dir, { withFileTypes: true })
const contentsWithSafeToRemove = await Promise.all(
contents.map(async (c) => {
if (c.isDirectory()) {
Expand All @@ -414,15 +419,16 @@ class Project {
`Output dir ${g.output_dir} contains this file(s) not generated by BAML: ${notSafeToRemove.join(', ')}`,
)
}
await rename(g.output_dir, backupDir)
await rename(out_dir, backupDir)
}
await rename(tmpDir, g.output_dir)
await rename(tmpDir, out_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 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

0 comments on commit cdc1838

Please sign in to comment.