From cdc1838c114fd4d13e032da9cf7312aa55a8a889 Mon Sep 17 00:00:00 2001 From: aaronvg Date: Sun, 15 Dec 2024 19:47:20 -0800 Subject: [PATCH] Fix windows generation through vscode always outputting to the root workspace 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 > [!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`. > > This description was created by [Ellipsis](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral) for 594a4cd45d95f3a32f7abeb12278fd09a7be7774. It will automatically update as commits are pushed. --- engine/Cargo.lock | 4 ++-- .../src/runtime_wasm/generator.rs | 6 ++++++ engine/language_client_codegen/Cargo.toml | 2 +- .../src/lib/baml_project_manager.ts | 16 +++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/engine/Cargo.lock b/engine/Cargo.lock index 5006542f1..61c17467d 100644 --- a/engine/Cargo.lock +++ b/engine/Cargo.lock @@ -1680,9 +1680,9 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" [[package]] name = "dunce" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" [[package]] name = "either" diff --git a/engine/baml-schema-wasm/src/runtime_wasm/generator.rs b/engine/baml-schema-wasm/src/runtime_wasm/generator.rs index 8a2b6cfd4..666c9f10e 100644 --- a/engine/baml-schema-wasm/src/runtime_wasm/generator.rs +++ b/engine/baml-schema-wasm/src/runtime_wasm/generator.rs @@ -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, } @@ -22,6 +24,10 @@ impl Into 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() diff --git a/engine/language_client_codegen/Cargo.toml b/engine/language_client_codegen/Cargo.toml index 981b0a899..28ad655d8 100644 --- a/engine/language_client_codegen/Cargo.toml +++ b/engine/language_client_codegen/Cargo.toml @@ -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" \ No newline at end of file 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 5ccae611c..43f943257 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 @@ -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') @@ -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()) { @@ -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) => {