dynamicdan / sn-filesync

2-way sync ServiceNow field values to local files
MIT License
66 stars 37 forks source link

Visual Studio: Local file sometimes overwritten during sync (atomic save) #43

Closed mak42 closed 6 years ago

mak42 commented 7 years ago

When using sn-filesync on windows 10 with vscode sometimes it happens that sn-filesync will pull the file from service-now instead of pushing it on save. It seems like sn-filesync assumes the file is empty during save and pulls it from the instance. Apparently you can't even Ctrl+Z to get back your changes, so you have data loss which is pretty annoying. Maybe I can provide a fix, currently looking into this issue. Anybody else can reproduce this behavior?

dynamicdan commented 7 years ago

This is due to the "atomic save" behaviour which some editors have. There is a check in the code (app.js) for exactly this case to avoid thinking there is a new/empty file.

Given that search has improved so much, it's probably worth adding a config option to disable this feature (pull remote version if file empty). Would mean that users must run the resync option if there is a sync conflict and to get the latest version from the instance.

dynamicdan commented 7 years ago

I have a general comment/question.

I'm guessing you have a todo file per project with a list of outstanding or future tasks for a project/customer? I would steer clear of this type of task management or documentation. It reduces transparency with the dev team and customer and increases the risk of loss for the "hit by the bus" scenario. Also, it makes it hard to prioritise tasks across a project or multiple aspects. In my opinion, all todos and comments about a project should always be transparently tracked as comments on the record/script.

My 2 cents.

dynamicdan commented 7 years ago

@mak42 , can you always replicate the issue? Have you tried adding debug in the writeFile function?

Example:

if (mustBeEmpty) {
   console.log('doing readFile check to ensure file is really new/empty');
   ....

This will let me know if there is something to look into or not for your issue. Have you tried another editor to see if it is also affected?

To replicate the issue, you can look into how the test function works (testForLocalDataLoss) and run --test to see what the outcome is.

mak42 commented 7 years ago

Hello Dan, sorry for the long delay, nope I can't reproduce this issue every time. I have researched a bit and found the following question on stackoverflow Currently I'm testing the option "awaitWriteFinish" and it looks promising. I will keep you uptated if the bug is gone with this options set to true.

meinzeugde commented 6 years ago

I'm using a different approach for file uploading in VSC (April 2018 v1.23):

  1. Add a ".vscode/tasks.json" and put following content:
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "sync current file with ServiceNow",
            "type": "shell",
            "isBackground": false,
            "command": "node /c/sn-filesync-master/bin/app.js --config ./sync-config.json --push \"${relativeFile}\"",
            "problemMatcher": []
        }
    ]
}
  1. If you want to bind this task to a hotkey, just press F1 and type Preferences: Open Keyboard Shortcuts File, then insert (or add) the following to the opened file Default Keybindings:
[
    {
        "key": "ctrl+shift+s",
        "command": "workbench.action.tasks.runTask",
        "args": "sync current file with ServiceNow"
    }
]
dynamicdan commented 6 years ago

Given that this issue is IDE specific and that most IDEs provide ways to configure how saves work on the file system, I will close this issue. The comments here server as good guides for anyone in the same situation.

I especially like the creative solution of using a custom save command that triggers off the filesync --push option.