lmscloud-io / mdlcode-docs

5 stars 3 forks source link

Odd behaviour when creating a new file with save as #4

Closed FMCorz closed 6 months ago

FMCorz commented 7 months ago

I typically "Save as" some file to create another, that way I keep a lot of the boilerplate and sometimes even method. It's my way of duplicating code.

I have observed that when I "Save as" with MDLCode, the following occurs:

  1. The file gets copied, duh
  2. A new tab is opened with the new file
  3. VS code warns me of the following, in the bottom left corner:

    Failed to save 'group_leaderboard.php': The content of the file is newer. Please compare your version with the file contents or overwrite the content of the file with your changes. [Compare] [Overwrite]

  4. The focus remains in the file in which I originally was, which was not intended.
  5. Saving the newly generated file will not be possible until the above error is dismissed.

I suspect this is due to the intent for MDLCode to add the boilerplate itself, which conflicts with my file duplication.

lmscloud-io commented 6 months ago

ok, this is the worst kind of error - race condition. I could not reproduce it at first but then I turned off "autosave" and was able to reproduce it.

Unfortunately vscode api does not tell me how the new file was created - using "Save as" or just creating a new file. I was already checking that the file is empty before inserting boilerplate but clearly this is when the race condition occurred, it was first reported as empty but then two processes wrote to the file simulteneously.

The only solution I could think of is to wait 300ms after the file is created but before checking if it is empty. Hope you have a fast computer :) This will be added in the new release (tonight), let me know if it fixed it for you

lmscloud-io commented 6 months ago

This is implemented in version 1.0.32 (released today)

FMCorz commented 6 months ago

It works perfectly now, thank you!