Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vscode instead of fs to create new files #161

Merged
merged 2 commits into from
Mar 5, 2023
Merged

Conversation

higab85
Copy link
Contributor

@higab85 higab85 commented Jun 18, 2022

Summary

Use vscode.WorkspaceEdit.createFile instead of fs.writeFileSync to create new files.

Why

Other extensions on vscode might want to know when files are created in the workspace. The best way to do this, is by listening for a FileCreateEvent with vscode.workspace.onDidCreateFiles. This event is not fired if vscode.workspace.fs or a foreign program or script creates the file.

Choices made

Since vscode.workspace.fs.writeFile will also create a file if none is found, we wait for the vscode.workspace.applyEdit to finish to execute it.

Copy link
Owner

@kortina kortina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just getting back to reviewing activity on this repo after long hiatus....

This looks like a good change, but I am a little concerned about the rest of the downstream effects of switching from sync to async. For example, as I think I understand it, createNewNoteFile can now return before the file is actually created, correct?

Have you investigated all the possible downstream effects of this and verified this doesn't introduce an edge case / race condition that might cause an error?

Also, can you tell me a little bit about how you tested this?

Thanks!

@higab85
Copy link
Contributor Author

higab85 commented Jan 25, 2023

Hey @kortina, sorry for the delay! I made this PR for a project I was working on, but have since abandoned.

Correct, createNewNoteFile can now return before the file is created. I did not investigate the downstream effects, but I tried creating files through [[ filename ]] notation without problems.

The only way I could see a race condition happening is by clicking the [[ new filename ]] link many times, but:
a. VS Code switches to that file tab before I can click it again
b. applyEdit will not apply changes if there's an error:

When applying a workspace edit that consists only of text edits an 'all-or-nothing'-strategy is used. A workspace edit with resource creations or deletions aborts the operation, e.g. consecutive edits will not be attempted, when a single edit fails. - VS Code API

Seems pretty safe. What outcomes are you worried about?

@kortina
Copy link
Owner

kortina commented Jan 27, 2023

OK, ya, my main concern was that I almost always use ctrl ] with the cursor over a [[note that does not exist]] as a mechanism to create new notes. Are you saying that you have rigorously tested this and it consistently works fine / does not fail because opening the note in vscode always happens after the file gets created in the file system?

And the only way you triggered the failure mode is if you somehow trigger the create+open function 2x?

Please confirm, and if that's the case I will checkout and play with the branch myself. Thanks!

const edit = new vscode.WorkspaceEdit();
const fileUri = vscode.Uri.file(filepath);
edit.createFile(fileUri);
vscode.workspace.applyEdit(edit).then(() =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see here there is opportunity for a callback. I was thinking I wonder if there's a way to somehow leverage this when using goto definition for a non-existent note, ie here:

https://github.com/kortina/vscode-markdown-notes/blob/master/src/MarkdownDefinitionProvider.ts#L39

In the case where we must create the note, can we await the async vscode create file operation before we return the vscode.Location array in this function?

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense to use async. Changed

@higab85
Copy link
Contributor Author

higab85 commented Feb 5, 2023

I'm on a Mac so had to look up the keybinding. Seems like that ctrl ] outdents the line. Is this a custom shortcut you have?
I cannot open a [[wikilink]] with my cursor over it and pressing ⌘] (equivalent on OSX), I get an outdent. ⌘ shift ] doesn't do much either.
I have tested with and click on the file and this will:

  • Create the file if it doesn't exist
  • Go to the file if it does

I made another test where I changed the permission of the folder to not allow writes (555), and this will just not create the file, and will not throw an error. Tried your extension and it behaved the same.

I could not trigger the error. Clicking the [[filename]] multiple scenarios was a hypothetical scenario I could not replicate.

@kortina kortina merged commit 19f6495 into kortina:master Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants