google / clasp

🔗 Command Line Apps Script Projects
https://developers.google.com/apps-script/guides/clasp
Apache License 2.0
4.6k stars 429 forks source link

Clasp push exits with code 0 when it fails #786

Open ddomonkos opened 4 years ago

ddomonkos commented 4 years ago

Was there an intention behind catching the exception and not letting the process exit with a non-zero code?

See https://github.com/google/clasp/blob/master/src/files.ts#L373

The reason why this matters is that we are using clasp in our CI/CD and the pipeline appears ok even if it, in reality, fails.

In our case the push failed because ts2gas transpiled into ES2019 rather than ES3.

Expected Behavior

When a clasp command fails, it exits with a non-zero code.

Actual Behavior

When clasp push fails, it exits with a zero code.

PopGoesTheWza commented 4 years ago

@ddomonkos can you test with the #791 unofficial release?

npm uninstall -g @google/clasp && npm install -g forked-clasp
clasp --version
# should be 2.4.0 or more
PopGoesTheWza commented 3 years ago

@ddomonkos What is the status for this issue?

ddomonkos commented 3 years ago

Sorry @PopGoesTheWza, we stopped using the library because we had to split it into separate build and upload steps.

Solely looking at the source code, I don't see any change in pushFiles function. Or maybe I'm just looking at the wrong place?

PopGoesTheWza commented 3 years ago

@ddomonkos If you want to see the actual code, it is here: https://github.com/PopGoesTheWza/clasp/tree/forked

PopGoesTheWza commented 3 years ago

Should be fixed in https://github.com/google/clasp/releases/tag/v2.3.1

cldellow commented 2 years ago

Is there a command line flag or clasp.json setting that needs to be enabled?

With clasp 2.3.2, I still get a zero exit code if I push code that explicitly has an error:

$ ./node_modules/.bin/clasp push && echo success!
- Pushing files...Push failed. Errors:
GaxiosError: Syntax error: SyntaxError: Unexpected token ';' line: 31 file: main.gs
    at Gaxios._request (/home/cldellow/src/ds-connector/node_modules/gaxios/build/src/gaxios.js:85:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async OAuth2Client.requestAsync (/home/cldellow/src/ds-connector/node_modules/google-auth-library/build/src/auth/oauth2client.js:350:18) {
...lots of gaxios output omitted...
  code: 400,
  errors: [
    {
      message: "Syntax error: SyntaxError: Unexpected token ';' line: 31 file: main.gs",
      domain: 'global',
      reason: 'badRequest'
    }
  ]
}
└─ src/appsscript.json
└─ src/fields.js
└─ src/main.js
Pushed 3 files.
success!

Clasp 2.4.1 fails some node assertion and dumps core:

$ ./node_modules/.bin/clasp push && echo success!
node[1213831]: ../src/api/callback.cc:129:void node::InternalCallbackScope::Close(): Assertion `(env_->execution_async_id()) == (0)' failed.
 1: 0xb17ec0 node::Abort() [node]
 2: 0xb17f3e  [node]
 3: 0xa59021 node::InternalCallbackScope::Close() [node]
 4: 0xa590e1 node::InternalCallbackScope::~InternalCallbackScope() [node]
 5: 0xb1baf7 node::fs::FileHandle::CloseReq::Resolve() [node]
 6: 0xb1bc79  [node]
 7: 0x158ab2d  [node]
 8: 0x158f186  [node]
 9: 0x15a1f15  [node]
10: 0x158fab8 uv_run [node]
11: 0xa59f25 node::SpinEventLoop(node::Environment*) [node]
12: 0xb59bd7 node::NodeMainInstance::Run(node::EnvSerializeInfo const*) [node]
13: 0xadfaa2 node::Start(int, char**) [node]
14: 0x7f2b0a88f0b3 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
15: 0xa57d6c  [node]
Aborted (core dumped)
hankei6km commented 2 months ago

I'm also getting a zero exit code.

Below is the result using the latest version command. (d633ca9f8544c94cd245006623de3b8d68181700).

$ clasp --auth ~/.clasprc_tmp.json push
Push failed. Errors:
SyntaxError: Unexpected token 'export' - "/workspace/tmp/gas-webapp-gh-repo-files/build/webapp-gh-repo-file.js:36"
      return htmlOutput
    }
  }

⇒ export async function repoToFile(opts) {
    try {
      return _entry_point_.WebappGhRepoFiles.repoToFile(opts)
    } catch (e) {
      console.error(e)
$ echo $?
0

The reason why the exit code is zero.

The pushFiles function in src/files.ts doesn't throw anything after catching. Therefore, the src/index.ts side is silently terminated.

https://github.com/google/clasp/blob/d633ca9f8544c94cd245006623de3b8d68181700/src/files.ts#L364-L442

https://github.com/google/clasp/blob/d633ca9f8544c94cd245006623de3b8d68181700/src/index.ts#L399-L415

The workaround is not to use console.error in the pushFiles function. And thorw the error.

diff --git a/src/files.ts b/src/files.ts
index cac6213..cda018c 100644
--- a/src/files.ts
+++ b/src/files.ts
@@ -428,10 +428,16 @@ export const pushFiles = async (silent = false) => {
               snippet = preLines + '\n' + errLine + '\n' + postLines;
             }
           }
-          console.error(chalk.red(message));
-          console.log(snippet);
+
+          // TODO improve error handling
+          throw new ClaspError(`${chalk.red(message)}\n${snippet}`);
         } else {
-          console.error(error);
+          if (error instanceof ClaspError) {
+            throw error;
+          }
+
+          // TODO improve error handling
+          throw error;
         }
       }
     } else {

When the push command failed, I received exit code 1.

$ clasp --auth ~/.clasprc_tmp.json push
Push failed. Errors:
SyntaxError: Unexpected token 'export' - "/workspace/tmp/gas-webapp-gh-repo-files/build/webapp-gh-repo-file.js:36"
      return htmlOutput
    }
  }

⇒ export async function repoToFile(opts) {
    try {
      return _entry_point_.WebappGhRepoFiles.repoToFile(opts)
    } catch (e) {
      console.error(e)
$ echo $?
1