[BUG] Typescript environment generation bugs
- Dominant language
- C
- Stars
- 765
- Forks
- 46
- PR merge metrics
- No merged PRs in 30d
Description
## Bug 1: `jsb_process.cpp` — `_flush()` missing null terminator
### Environment
- Godot **4.6.1-stable**
- GodotJS **main** (`17db3e2`)
- **Windows 10**
- **VS2022** MSVC v143
- **V8** 12.4.254.21
### Symptom
`tsc` watch output lines show **garbled trailing text** and Godot logs errors such as:
```
[JSProcess][Log] [tsc] 16:04:46 - Starting compilation in watch mode...搃䈏⧯뗧圀考x
[JSProcess][Log] [tsc] 16:04:47 - Found 0 errors. Watching for file changes.de...搃䈏⧯뗧圀考x
ERROR: Unicode parsing error: Unpaired surrogate (dc2a)
```
### Root cause
In `internal/jsb_process.cpp`, Windows `ProcessImpl::_flush()`:
`MultiByteToWideChar` **does not** append `L'\0'` when `cbMultiByte` is an explicit length (not `-1`).
`buffer[num]` is therefore **uninitialized**, so `String(buffer.ptr())` reads past the valid buffer, producing garbage and **Unicode parsing error: Unpaired surrogate**.
### Suggested fix
```cpp
buffer.resize(num + 1);
if (MultiByteToWideChar(CP_ACP, 0, rd_line.ptr(), rd_line.size(), buffer.ptr(), num) == 0)
{
buffer.clear();
}
else
{
buffer[num] = L'\0'; // FIX: manually null-terminate since API won't do it
}
```
---
## Bug 2: `jsb_editor_plugin.cpp` — dangling pointer from temporary `PresetSource`
### Environment
Same as Bug 1 (Godot 4.6.1-stable, GodotJS main `17db3e2`, Windows 10, VS2022 MSVC v143, V8 12.4.254.21).
### Symptom
After **Install Preset Files** or **Generate Types**, **compressed** preset files (`godot.minimal.d.ts`, `godot.mix.d.ts`, `jsb.*.bundle.d.ts`, `tsconfig.json`, etc.) are written with **only the first character** (`/`, `d`, `{`, …). You may need to trigger the action repeatedly before it occasionally works.
**Uncompressed** small files (`package.json`, `.gdignore`) are always correct.
### Root cause
In `weaver-editor/jsb_editor_plugin.cpp`, `apply_file()` and `verify_file()`:
`get_preset_source()` returns a **temporary** `PresetSource` **by value**. For compressed data, `get_data()` lazily decompresses into `uncompressed_data_` (`Vector`) on that temporary object and returns `ptr()`.
The temporary is destroyed at the **end of the full expression** (e.g. at the semicolon), so the pointer **dangles**. Later allocations can reuse that memory; the second byte may become `0x00`.
`String::append_utf8()` stops when `*ptrtmp` is zero (`while (ptrtmp < ptr_limit && *ptrtmp)`), so the resulting `String` contains **only the first character**.
### Suggested fix
```cpp
// BEFORE (dangling pointer — use-after-free):
const char* data = get_preset_source(p_file.source_name).get_data(size);
// AFTER (PresetSource lifetime covers use of data):
const jsb::internal::PresetSource preset = get_preset_source(p_file.source_name);
const char* data = preset.get_data(size);
```
---
## Bug 3: `jsb.editor.codegen.ts` — `SceneNodes` not generated for `.glb` PackedScenes
### Environment
- Godot **4.6.1-stable**
- GodotJS **main** (`17db3e2`)
- **Windows 10**
### Symptom
TypeScript compiler error:
`Property 'SM_PoolTableRed.glb' does not exist on type 'SceneNodes'`.
Imported `.glb` assets are treated as **PackedScene** in Godot. The resource type generator emits references like `PackedScene>`, but the **scene node code generator** does **not** emit the corresponding **`SceneNodes`** definition.
### Root cause
In `scripts/jsb.editor/src/jsb.editor.codegen.ts`, `SceneTSDCodeGen.make_scene_path()`:
The regex `/\.t?scn$/i` only matches `.tscn` / `.scn`, not other PackedScene extensions such as **`.glb`**, so the output path does not get the `.nodes.gen.ts` suffix and the **`SceneNodes`** definition is missing.
### Suggested fix
```typescript
// BEFORE (only .tscn/.scn):
scene_path.replace(/\.t?scn$/i, ".nodes.gen.ts")
// AFTER (any extension):
scene_path.replace(/\.[^.\/]+$/, ".nodes.gen.ts")
```
Contributor guide
Assessment
This issue has not been assessed yet.