question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`clasp push` changes the execution order of script files in Apps Script projects

See original GitHub issue

Expected 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

  1. Create a stand-alone script project

  2. 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. screenshot_1

  3. Create a 7th script file and add the following function: screenshot_2

  4. Run that function from the menu bar Run>Run function>runTest and then view the logs View>Logs and you will get the following: screenshot_3

  5. Open your terminal and use clasp clone [PROJECT-FILE-ID] and then clasp push from the command line (project file id omitted from screenshot): screenshot_4

  6. Then run the test function again and then view the logs and you’ll see the following: screenshot_5

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:36 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
JeanRemiDelteilcommented, Mar 27, 2018

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).

2reactions
DimuDesignscommented, Mar 23, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command Line Interface using clasp | Apps Script
This command uploads all of a script project's files from your computer to Google Drive. clasp push. List project versions.
Read more >
google apps script - Execution order of GS files in a Project
An easy fix is to make a copy of 1_File.gs and then delete the original, effectively moving it to the end of the...
Read more >
clasp - The Apps Script CLI - Google Codelabs
clasp push replaces code that is on script.google.com and clasp pull replaces all files locally.
Read more >
CLASP Web App, Google Apps Script, Parcel JS ... - YouTube
... tutorial we'll use CLASP with many other tools like Parcel JS bundler, Nodemon, Node JS and setup a Web App Google Apps...
Read more >
The 6 deadly sins of Google Apps Script Add-on development
Or if you update the UI for one of multiple add-ons that are basically the same, you will have to move those changes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found