Hi - Recently I have been investigating an annoying situation whilst editing ObjectScript classes or routines in VSCode.
What was happening to me was, as I was typing in lines of code into my class (for example, adding a new Method, or changing the Class signature, or a block of code), this would quickly get syntax checked, re-formatted, and compiled - inevitably, (since I would be mid-way through my typing), this would generate compilation errors.
Knowing that I was mid-way through adding code, I could simply dismiss this message, however, this soon got very annoying, and conditioned me to ignore the error (which is not a good thing).
Even worse, however, is that the compilation process also reformatted the document and sometimes broke what was previously well formatted lines of code (appearing below the line I was writing) - which required me to go into that area of the document and fix them back up.
What's going on ?
There are two settings at play here:
- VSCode's auto-save (files.autosave) settings which dictates wether to automatically save a document that has been changed, and if so, how long to wait before saving (files.autoSaveDelay)
- ObjectScript extension's compileOnSave (objectscript.compileOnSave), which determines wether after saving the document, this also imports into and compiles on the connected IRIS namespace.
Since I had autosave turned on, and on a short wait delay together with compileOnSave meant that partially edited code kept compiling, generating the above erros, and as mentioned, sometimes re-formatting good, pre-existing blocks of code.
In my case, I have settled on the following:
- AutoSave (files.autosave) off (autosaveDelay is ignored), and
- CompileOnSave (objectscript.compileOnSave) on.
Now, when I'm happy with my edits (in my own time !), I press Ctrl-S, to save, import and compile my class, and look out for that pop-up message which has real meaning and purpose now.
You may choose a different combination, (for example, go ahead and Autosave after 5 minutes or so, accepting the code may not fully compile), however, I've found the above combination works for me, and hope that - if you are experiencing the same annoyance, then, this article may help you find your optimum configuration.
Thankyou to @Patrick Sulin, and @Raj Singh for pointing me in the direction of these settings !
Steve Pisani
Thank you for this! It's been irritating me for a while
This can be frustrating; thanks for sharing it Steve!
Maybe we can try to get https://github.com/microsoft/vscode/issues/141868 implemented.
As @Joel Solon pointed out, there's already a setting to achieve this. It's called files.autoSaveWhenNoErrors
This is exactly how I have it setup. No one writes a perfect class the first time (well maybe Hello World) but saving randomly only causes grief.
I never encountered this issue. I assume it's because AutoSave is off by default, and Steve and others that have commented turned it on. I noticed that there IS a setting called "Auto Save When No Errors."
Will that work for Object Script? Don't we need to save before we can compile and even find out if there are errors?
This is helpful, but I feel like it's still just a workaround for the real problem, which is that the compiler is reformatting code. Even if I allow that the compiler knows more about how code should be formatted than I do (it doesn't, if only because correct formatting is defined by personal opinion), it shouldn't be doing that until it's fully parsed and validated the code. If it hasn't successfully parsed the code it can't possibly make good decisions about how it should be formatted.
I don't see this. If I save it (ctrl-S) it just saves and compiles it. It does not reformat it.
If I ask it to format it (shift-alt-f) it does a simple formatting (basically seems to 'Sentence case' each keyword) but leaves the format unchanged.
Have you got a formatter installed for objectscript classes on top of the InterSystems one?
The automatic formatting that I see is mainly about adding spaces or applying capital letters to certain keywords. I have no formatter installed, nor something else for that purpose.
Thanks for this post. I have been using VS Code and have "files.autoSave": "afterDelay" set which seemed to have been working fine, however at some point while writing code it stopped auto saving which lead me down a path of trying to understand why functionality I had added was not appearing and why syntax errors were NOT going away after multiple edits in a local workspace. Finally an explicit save prompted for a server overwrite and changed the server version of the cls file. Using I4H 2024.2. Wondering if anyone else has seen this?
💡 This article is considered InterSystems Data Platform Best Practice.