`clasp push` changes the execution order of script files in Apps Script projects
See original GitHub issueExpected Behavior
clasp push
should retain the order of the script files in the Apps Script Editor not just visually but also in terms of order of execution.
Actual Behavior
clasp push
re-orders the files so that they are executed in alphabetical order even though visually they retain the expected order in the sidebar of the editor. This bug is hazardous to projects that rely on script files to execute as listed in the Apps Script editor.
Steps to Reproduce the Problem
-
Create a stand-alone script project
-
In that project create 6 script files in sequence and name them Z, Y, X, C, B and A; each with a
Logger.log()
that logs their respective names. -
Create a 7th script file and add the following function:
-
Run that function from the menu bar Run>Run function>runTest and then view the logs View>Logs and you will get the following:
-
Open your terminal and use
clasp clone [PROJECT-FILE-ID]
and thenclasp push
from the command line (project file id omitted from screenshot): -
Then run the test function again and then view the logs and you’ll see the following:
The execution order of the scripts (though visually correct in the editor’s sidebar) is changed to run in alphabetical order!
This bug wreaked havoc on one of my projects before I found it. I would love for clasp to be a part of my development workflow but that won’t happen until this issue is resolved.
Specifications
- Node version - v6.11.0
- Clasp Version - 3.10.10
- OS - Windows
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:36 (23 by maintainers)
Top GitHub Comments
The question of running script in parallel in totally different from the matter of file execution order.
In AppsScript, strictly speaking, real parallelism or asynchronous behavior like you would expect with Promise, timeOut or thread does not exist at the moment.
All files are read and executed in their creation order, which is why using global is not recommended. This creation order was historically the order of file creation in the web editor.
And only for standalone script (not container bound script, eg: script linked to a spreadsheet and such), you could get and set the content as a Drive file. In such a case, execution order was the order in which files were in the drive file (with GAPPS, that was alphabetical order).
The new GAS API is totally different in its disclosed manner of action, in the way that it is a API working on AppsScript projects, and not on Drive files. As such it is not bound the standalone restriction, it can work on standalone and container bound both (which is a GREAT improvement, waited for many years).
Relying in wacky hacks due to Script execution order should not be the way in AppsScript. There are way better ways to achieve initialization with better control.
For example, you should know were all your entry points are located, and at the beginning of all those functions, just call the function initializing everything (by returning an object). Just to mention, in AppsScript, the ‘this’ object is the global object.
I have an example of this in this simple project: monitor-firebase-status (and I would be happy to discuss over this method of initialization).
Best practice aside, sometimes there is a need to store configuration options (some generated from custom classes) as global constants/enums. I could wrap said properties into getter/accessor functions or create some kind of singleton composed of such functions but that feels superfluous since I know I can rely on execution order. Unfortunately,
clasp
breaks that reliability.@JeanRemiDelteil Following a
clasp push
the script files are re-ordered to execute alphabetically (in truth lexicographical-ly) instead of the order in which they were created. However, that ordering is not reflected visually in the Apps Script editor so I don’t see the value in retaining this weird ordering glitch. But there is an option to sort files alphabetically under the view menu in the Apps Script editor.