From c2078ea3fd20bc135d0cccdbe3e5846a0335dd46 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 12 Mar 2020 07:21:42 +0100 Subject: [PATCH] Ensure process exits if a process warning is emitted (#59651) Crash Kibana in dev/CI if a process warning is detected. This does not influence how Kibana behaves in production when run via `./bin/kibana` as there the `--no-warnings` flag is used. We will detect this flag and as a result, disable this behavior. Previously we used the flags `--trace-warnings --throw-deprecation` when you started Kibana via `yarn start` and we used `--throw-deprecation` in CI. This meant that we would only crash on deprecation warnings and log a stack trace for all other types of warnings. There were a couple of drawbacks to this approach: 1. If the deprecated API was called in a place enclosed in a try-catch (or inside of a Promise or an async/await context), the throw would be caught and the program would not crash. 2. If you ran `./scripts/kibana` directly instead of running `yarn start`, you would not get these flags automatically. 3. If you ran any of our tests locally you would not get the standard CI flags. This meant something that might seem to pass locally would not pass in CI. This commit changes this behavior by ensuring: - That we always crash - no matter if the offending code is surrounded by a try-catch (etc). - That you always get the same behavior whether you run `yarn start` or `./scripts/kibana`. - That you always get the same behavior in CI or if you run individual tests locally. Furthermore, we now crash for all types of warnings - not only deprecation warnings (except `MaxListenersExceededWarning`). Closes #59646 --- package.json | 2 +- src/dev/ci_setup/setup_env.sh | 4 +-- src/setup_node_env/exit_on_warning.js | 38 +++++++++++++++++++++++++++ src/setup_node_env/index.js | 6 ++++- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/setup_node_env/exit_on_warning.js diff --git a/package.json b/package.json index 0e30000ae3c57d..67c648ef30e165 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "typespec": "typings-tester --config x-pack/legacy/plugins/canvas/public/lib/aeroelastic/tsconfig.json x-pack/legacy/plugins/canvas/public/lib/aeroelastic/__fixtures__/typescript/typespec_tests.ts", "checkLicenses": "node scripts/check_licenses --dev", "build": "node scripts/build --all-platforms", - "start": "node --trace-warnings --throw-deprecation scripts/kibana --dev", + "start": "node scripts/kibana --dev", "debug": "node --nolazy --inspect scripts/kibana --dev", "debug-break": "node --nolazy --inspect-brk scripts/kibana --dev", "lint": "yarn run lint:es && yarn run lint:sass", diff --git a/src/dev/ci_setup/setup_env.sh b/src/dev/ci_setup/setup_env.sh index dc3fa38f3129c9..05840926d35de1 100644 --- a/src/dev/ci_setup/setup_env.sh +++ b/src/dev/ci_setup/setup_env.sh @@ -14,7 +14,7 @@ cacheDir="$HOME/.kibana" RED='\033[0;31m' C_RESET='\033[0m' # Reset color -export NODE_OPTIONS="$NODE_OPTIONS --throw-deprecation --max-old-space-size=4096" +export NODE_OPTIONS="$NODE_OPTIONS --max-old-space-size=4096" ### ### Since the Jenkins logging output collector doesn't look like a TTY @@ -168,4 +168,4 @@ if [[ -d "$ES_DIR" && -f "$ES_JAVA_PROP_PATH" ]]; then export JAVA_HOME=$HOME/.java/$ES_BUILD_JAVA fi -export CI_ENV_SETUP=true \ No newline at end of file +export CI_ENV_SETUP=true diff --git a/src/setup_node_env/exit_on_warning.js b/src/setup_node_env/exit_on_warning.js new file mode 100644 index 00000000000000..5be5ccd72bd024 --- /dev/null +++ b/src/setup_node_env/exit_on_warning.js @@ -0,0 +1,38 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +if (process.noProcessWarnings !== true) { + var ignore = ['MaxListenersExceededWarning']; + + process.on('warning', function(warn) { + if (ignore.includes(warn.name)) return; + + if (process.traceProcessWarnings === true) { + console.error('Node.js process-warning detected - Terminating process...'); + } else { + console.error('Node.js process-warning detected:'); + console.error(); + console.error(warn.stack); + console.error(); + console.error('Terminating process...'); + } + + process.exit(1); + }); +} diff --git a/src/setup_node_env/index.js b/src/setup_node_env/index.js index 0f51f47572be68..97de5ba76b926e 100644 --- a/src/setup_node_env/index.js +++ b/src/setup_node_env/index.js @@ -17,7 +17,11 @@ * under the License. */ -require('./harden'); // this require MUST be executed before any others +// The following require statements MUST be executed before any others - BEGIN +require('./exit_on_warning'); +require('./harden'); +// The following require statements MUST be executed before any others - END + require('symbol-observable'); require('./root'); require('./node_version_validator');