johnfn / ts2gd

💥 Compile TypeScript to GDScript for Godot
200 stars 14 forks source link

fix async file watcher #77

Closed ksjogo closed 2 years ago

ksjogo commented 2 years ago

https://github.com/johnfn/ts2gd/commit/d533dadd003c5c6d085a2ea54c2e16a5909741b8 broke the watching functionality due to endlessly looping in the second while loop (not sure why though, something with promises in while conditions?). This fixes that and merges the two synchronization loops into one.

johnfn commented 2 years ago

LGTM pending a few writing nits... but are you sure this fixes the problem? The code in the previous commit was this:

    while (
      !this.project.args.buildOnly &&
      (await fs.readFile(this.fsPath, "utf-8")) !== sourceFileAst.getFullText()
    ) {

I can't see what the error was with that code. It looks fine to me...

ksjogo commented 2 years ago

Yeah, I also didn't see what is wrong with the current code, but it got stuck endlessly retrying on my machine without the changes. It does work with the changes. Didn't see anything in the generated js as well.

Only thing I come up with reasoning is that the promise somehow stays the same with non-atomic writes. So editor writes a bit, triggers chokidar, promises gets created and executed, reading half-finished-state from fs, gets compared, is not equal to getFullText(), fs finishes write, getFullText() now returns fully new version, but promise resolved with the in-between one.