@netlify/build 0.4.48 → 1.0.0-dl-test

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/bin.js +5 -0
  2. package/lib/core/bin.js +66 -0
  3. package/lib/core/build.js +392 -0
  4. package/lib/core/config.js +124 -0
  5. package/lib/core/constants.js +116 -0
  6. package/lib/core/dev.js +27 -0
  7. package/lib/core/dry.js +21 -0
  8. package/lib/core/feature_flags.js +17 -0
  9. package/lib/core/flags.js +206 -0
  10. package/lib/core/lingering.js +68 -0
  11. package/lib/core/main.js +114 -0
  12. package/lib/core/missing_side_file.js +17 -0
  13. package/lib/core/normalize_flags.js +62 -0
  14. package/lib/core/report_metrics.js +17 -0
  15. package/lib/core/severity.js +21 -0
  16. package/lib/core/types.js +8 -0
  17. package/lib/core/user_node_version.js +32 -0
  18. package/lib/env/changes.js +43 -0
  19. package/lib/env/main.js +14 -0
  20. package/lib/env/metadata.js +68 -0
  21. package/lib/error/api.js +37 -0
  22. package/lib/error/build.js +36 -0
  23. package/lib/error/cancel.js +7 -0
  24. package/lib/error/colors.js +9 -0
  25. package/lib/error/handle.js +44 -0
  26. package/lib/error/info.js +37 -0
  27. package/lib/error/monitor/location.js +16 -0
  28. package/lib/error/monitor/normalize.js +86 -0
  29. package/lib/error/monitor/print.js +20 -0
  30. package/lib/error/monitor/report.js +120 -0
  31. package/lib/error/monitor/start.js +61 -0
  32. package/lib/error/parse/clean_stack.js +70 -0
  33. package/lib/error/parse/location.js +50 -0
  34. package/lib/error/parse/normalize.js +24 -0
  35. package/lib/error/parse/parse.js +67 -0
  36. package/lib/error/parse/plugin.js +55 -0
  37. package/lib/error/parse/properties.js +16 -0
  38. package/lib/error/parse/serialize_log.js +34 -0
  39. package/lib/error/parse/serialize_status.js +18 -0
  40. package/lib/error/parse/stack.js +34 -0
  41. package/lib/error/report.js +29 -0
  42. package/lib/error/type.js +177 -0
  43. package/lib/install/functions.js +20 -0
  44. package/lib/install/local.js +45 -0
  45. package/lib/install/main.js +67 -0
  46. package/lib/install/missing.js +54 -0
  47. package/lib/log/colors.js +28 -0
  48. package/lib/log/description.js +21 -0
  49. package/lib/log/header.js +12 -0
  50. package/lib/log/header_func.js +13 -0
  51. package/lib/log/logger.js +140 -0
  52. package/lib/log/messages/compatibility.js +120 -0
  53. package/lib/log/messages/config.js +91 -0
  54. package/lib/log/messages/core.js +49 -0
  55. package/lib/log/messages/core_steps.js +75 -0
  56. package/lib/log/messages/dry.js +41 -0
  57. package/lib/log/messages/install.js +25 -0
  58. package/lib/log/messages/ipc.js +29 -0
  59. package/lib/log/messages/mutations.js +62 -0
  60. package/lib/log/messages/plugins.js +25 -0
  61. package/lib/log/messages/status.js +14 -0
  62. package/lib/log/messages/steps.js +18 -0
  63. package/lib/log/serialize.js +10 -0
  64. package/lib/log/stream.js +68 -0
  65. package/lib/log/theme.js +27 -0
  66. package/lib/plugins/child/diff.js +46 -0
  67. package/lib/plugins/child/error.js +26 -0
  68. package/lib/plugins/child/lazy.js +15 -0
  69. package/lib/plugins/child/load.js +22 -0
  70. package/lib/plugins/child/logic.js +57 -0
  71. package/lib/plugins/child/main.js +37 -0
  72. package/lib/plugins/child/run.js +19 -0
  73. package/lib/plugins/child/status.js +63 -0
  74. package/lib/plugins/child/typescript.js +28 -0
  75. package/lib/plugins/child/utils.js +42 -0
  76. package/lib/plugins/child/validate.js +31 -0
  77. package/lib/plugins/compatibility.js +104 -0
  78. package/lib/plugins/error.js +46 -0
  79. package/lib/plugins/events.js +12 -0
  80. package/lib/plugins/expected_version.js +81 -0
  81. package/lib/plugins/internal.js +10 -0
  82. package/lib/plugins/ipc.js +120 -0
  83. package/lib/plugins/list.js +73 -0
  84. package/lib/plugins/load.js +50 -0
  85. package/lib/plugins/manifest/check.js +85 -0
  86. package/lib/plugins/manifest/load.js +38 -0
  87. package/lib/plugins/manifest/main.js +19 -0
  88. package/lib/plugins/manifest/path.js +24 -0
  89. package/lib/plugins/manifest/validate.js +91 -0
  90. package/lib/plugins/node_version.js +35 -0
  91. package/lib/plugins/options.js +70 -0
  92. package/lib/plugins/pinned_version.js +83 -0
  93. package/lib/plugins/resolve.js +110 -0
  94. package/lib/plugins/spawn.js +55 -0
  95. package/lib/plugins_core/add.js +35 -0
  96. package/lib/plugins_core/build_command.js +50 -0
  97. package/lib/plugins_core/deploy/buildbot_client.js +87 -0
  98. package/lib/plugins_core/deploy/index.js +49 -0
  99. package/lib/plugins_core/deploy/manifest.yml +1 -0
  100. package/lib/plugins_core/edge_functions/index.js +106 -0
  101. package/lib/plugins_core/edge_functions/lib/error.js +17 -0
  102. package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +14 -0
  103. package/lib/plugins_core/functions/error.js +123 -0
  104. package/lib/plugins_core/functions/feature_flags.js +5 -0
  105. package/lib/plugins_core/functions/index.js +137 -0
  106. package/lib/plugins_core/functions/utils.js +45 -0
  107. package/lib/plugins_core/functions/zisi.js +64 -0
  108. package/lib/plugins_core/functions_install/index.js +10 -0
  109. package/lib/plugins_core/list.js +20 -0
  110. package/lib/plugins_core/save_artifacts/index.js +33 -0
  111. package/lib/report/statsd.js +56 -0
  112. package/lib/status/add.js +30 -0
  113. package/lib/status/colors.js +18 -0
  114. package/lib/status/load_error.js +10 -0
  115. package/lib/status/report.js +83 -0
  116. package/lib/status/success.js +14 -0
  117. package/lib/steps/core_step.js +62 -0
  118. package/lib/steps/error.js +65 -0
  119. package/lib/steps/get.js +44 -0
  120. package/lib/steps/plugin.js +55 -0
  121. package/lib/steps/return.js +27 -0
  122. package/lib/steps/run_core_steps.js +117 -0
  123. package/lib/steps/run_step.js +200 -0
  124. package/lib/steps/run_steps.js +102 -0
  125. package/lib/steps/update_config.js +66 -0
  126. package/lib/telemetry/main.js +94 -0
  127. package/lib/time/aggregate.js +109 -0
  128. package/lib/time/main.js +31 -0
  129. package/lib/time/measure.js +16 -0
  130. package/lib/time/report.js +30 -0
  131. package/lib/utils/errors.js +13 -0
  132. package/lib/utils/json.js +10 -0
  133. package/lib/utils/omit.js +3 -0
  134. package/lib/utils/package.js +24 -0
  135. package/lib/utils/remove_falsy.js +8 -0
  136. package/lib/utils/resolve.js +41 -0
  137. package/lib/utils/runtime.js +5 -0
  138. package/lib/utils/semver.js +28 -0
  139. package/package.json +119 -56
  140. package/types/config/build.d.ts +52 -0
  141. package/types/config/functions.d.ts +36 -0
  142. package/types/config/inputs.d.ts +7 -0
  143. package/types/config/netlify_config.d.ts +58 -0
  144. package/types/index.d.ts +7 -0
  145. package/types/netlify_event_handler.d.ts +29 -0
  146. package/types/netlify_plugin.d.ts +29 -0
  147. package/types/netlify_plugin_constants.d.ts +51 -0
  148. package/types/netlify_plugin_options.d.ts +23 -0
  149. package/types/options/index.d.ts +1 -0
  150. package/types/options/netlify_plugin_build_util.d.ts +7 -0
  151. package/types/options/netlify_plugin_cache_util.d.ts +39 -0
  152. package/types/options/netlify_plugin_functions_util.d.ts +32 -0
  153. package/types/options/netlify_plugin_git_util.d.ts +41 -0
  154. package/types/options/netlify_plugin_run_util.d.ts +24 -0
  155. package/types/options/netlify_plugin_status_util.d.ts +24 -0
  156. package/types/options/netlify_plugin_utils.d.ts +15 -0
  157. package/types/utils/json_value.d.ts +1 -0
  158. package/types/utils/many.d.ts +6 -0
  159. package/README.md +0 -3
  160. package/src/core/bin.js +0 -139
  161. package/src/core/commands.js +0 -304
  162. package/src/core/config.js +0 -130
  163. package/src/core/constants.js +0 -88
  164. package/src/core/dry.js +0 -23
  165. package/src/core/main.js +0 -196
  166. package/src/env/changes.js +0 -49
  167. package/src/env/git.js +0 -27
  168. package/src/env/main.js +0 -97
  169. package/src/env/metadata.js +0 -81
  170. package/src/error/api.js +0 -32
  171. package/src/error/build.js +0 -32
  172. package/src/error/cancel.js +0 -22
  173. package/src/error/colors.js +0 -13
  174. package/src/error/info.js +0 -12
  175. package/src/error/monitor/normalize.js +0 -50
  176. package/src/error/monitor/print.js +0 -43
  177. package/src/error/monitor/report.js +0 -140
  178. package/src/error/monitor/start.js +0 -50
  179. package/src/error/parse/clean_stack.js +0 -86
  180. package/src/error/parse/location.js +0 -50
  181. package/src/error/parse/parse.js +0 -87
  182. package/src/error/parse/plugin.js +0 -68
  183. package/src/error/parse/properties.js +0 -20
  184. package/src/error/parse/serialize_log.js +0 -46
  185. package/src/error/parse/serialize_status.js +0 -28
  186. package/src/error/parse/stack.js +0 -45
  187. package/src/error/process.js +0 -13
  188. package/src/error/type.js +0 -143
  189. package/src/install/functions.js +0 -52
  190. package/src/install/local.js +0 -65
  191. package/src/install/main.js +0 -103
  192. package/src/install/missing.js +0 -86
  193. package/src/log/colors.js +0 -59
  194. package/src/log/description.js +0 -38
  195. package/src/log/header.js +0 -19
  196. package/src/log/logger.js +0 -55
  197. package/src/log/main.js +0 -348
  198. package/src/log/old_version.js +0 -45
  199. package/src/log/serialize.js +0 -15
  200. package/src/log/stream.js +0 -15
  201. package/src/log/theme.js +0 -32
  202. package/src/log/timer.js +0 -28
  203. package/src/plugins/child/api.js +0 -59
  204. package/src/plugins/child/error.js +0 -39
  205. package/src/plugins/child/load.js +0 -40
  206. package/src/plugins/child/logic.js +0 -31
  207. package/src/plugins/child/main.js +0 -48
  208. package/src/plugins/child/normalize.js +0 -20
  209. package/src/plugins/child/run.js +0 -34
  210. package/src/plugins/child/status.js +0 -53
  211. package/src/plugins/child/utils.js +0 -43
  212. package/src/plugins/child/validate.js +0 -46
  213. package/src/plugins/error.js +0 -44
  214. package/src/plugins/events.js +0 -77
  215. package/src/plugins/ipc.js +0 -136
  216. package/src/plugins/load.js +0 -66
  217. package/src/plugins/manifest/check.js +0 -80
  218. package/src/plugins/manifest/load.js +0 -47
  219. package/src/plugins/manifest/main.js +0 -28
  220. package/src/plugins/manifest/path.js +0 -12
  221. package/src/plugins/manifest/validate.js +0 -136
  222. package/src/plugins/node_version.js +0 -74
  223. package/src/plugins/options.js +0 -78
  224. package/src/plugins/package.js +0 -17
  225. package/src/plugins/resolve.js +0 -159
  226. package/src/plugins/spawn.js +0 -82
  227. package/src/plugins_core/cache/manifest.yml +0 -1
  228. package/src/plugins_core/cache/plugin.js +0 -75
  229. package/src/plugins_core/functions/manifest.yml +0 -1
  230. package/src/plugins_core/functions/plugin.js +0 -37
  231. package/src/plugins_core/functions_install/plugin.js +0 -14
  232. package/src/plugins_core/main.js +0 -38
  233. package/src/status/add.js +0 -56
  234. package/src/status/colors.js +0 -25
  235. package/src/status/report.js +0 -57
  236. package/src/telemetry/complete.js +0 -49
  237. package/src/telemetry/request.js +0 -28
  238. package/src/telemetry/track.js +0 -33
  239. package/src/utils/omit.js +0 -8
  240. package/src/utils/polyfills.js +0 -3
  241. package/src/utils/remove_falsy.js +0 -12
  242. package/src/utils/resolve.js +0 -39
  243. /package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
@@ -0,0 +1,21 @@
1
+ const SEVERITY_MAP = {
2
+ success: { severityCode: 0, status: 'success' },
3
+ none: { severityCode: 1, status: 'cancelled' },
4
+ info: { severityCode: 2, status: 'user-error' },
5
+ warning: { severityCode: 3, status: 'plugin-error' },
6
+ error: { severityCode: 4, status: 'system-error' },
7
+ };
8
+ const SUCCESS_SEVERITY = 'success';
9
+ /** Indicates a bug in our codebase */
10
+ const FALLBACK_SEVERITY = 'error';
11
+ /** Map error severities to exit codes and status (used for telemetry purposes) */
12
+ export const FALLBACK_SEVERITY_ENTRY = SEVERITY_MAP[FALLBACK_SEVERITY];
13
+ /**
14
+ * Used to extract exit codes and respective status strings
15
+ * 1|2|3 indicate whether this was a user|plugin|system error.
16
+ */
17
+ export const getSeverity = (severity = FALLBACK_SEVERITY) => {
18
+ const severityEntry = severity in SEVERITY_MAP ? SEVERITY_MAP[severity] : FALLBACK_SEVERITY_ENTRY;
19
+ const success = severity === SUCCESS_SEVERITY;
20
+ return { success, ...severityEntry };
21
+ };
@@ -0,0 +1,8 @@
1
+ export var SeverityCode;
2
+ (function (SeverityCode) {
3
+ SeverityCode[SeverityCode["success"] = 1] = "success";
4
+ SeverityCode[SeverityCode["buildCancelled"] = 2] = "buildCancelled";
5
+ SeverityCode[SeverityCode["userError"] = 3] = "userError";
6
+ SeverityCode[SeverityCode["pluginError"] = 4] = "pluginError";
7
+ SeverityCode[SeverityCode["systemError"] = 5] = "systemError";
8
+ })(SeverityCode = SeverityCode || (SeverityCode = {}));
@@ -0,0 +1,32 @@
1
+ import { version as currentVersion, execPath } from 'process';
2
+ import { execa } from 'execa';
3
+ import semver from 'semver';
4
+ import { addErrorInfo } from '../error/info.js';
5
+ // Retrieve Node.js version if the Node.js path is using nvm.
6
+ // `node.exe` on Windows, `bin/node` on Unix.
7
+ const NVM_NODE_VERSION_REGEXP = /[/\\]v(\d+\.\d+\.\d+)[/\\](bin[/\\]node|node.exe)$/;
8
+ // Retrieve Node.js version from current process
9
+ const getCurrentNodeVersion = function () {
10
+ return semver.clean(currentVersion);
11
+ };
12
+ // Retrieve Node.js version from `--node-path` or fallback to extracting the current Node.js process version
13
+ export const getUserNodeVersion = async function (nodePath) {
14
+ // No `--node-path` CLI flag, use the current node process version
15
+ if (nodePath === execPath) {
16
+ return getCurrentNodeVersion();
17
+ }
18
+ // Extract version from path
19
+ const result = NVM_NODE_VERSION_REGEXP.exec(nodePath);
20
+ if (result !== null) {
21
+ return result[1];
22
+ }
23
+ // Fallback to actually running `node --version` with the given nodePath
24
+ const { stdout } = await execa(nodePath, ['--version'], { reject: false });
25
+ const version = semver.clean(stdout);
26
+ if (version === null) {
27
+ const error = new Error(`Invalid --node-path CLI flag: ${nodePath}`);
28
+ addErrorInfo(error, { type: 'resolveConfig' });
29
+ throw error;
30
+ }
31
+ return version;
32
+ };
@@ -0,0 +1,43 @@
1
+ import { env } from 'process';
2
+ import filterObj from 'filter-obj';
3
+ import mapObj from 'map-obj';
4
+ // If plugins modify `process.env`, this is propagated in other plugins and in
5
+ // `build.command`. Since those are different processes, we figure out when they
6
+ // do this and communicate the new `process.env` to other processes.
7
+ export const getNewEnvChanges = function (envBefore, netlifyConfig, netlifyConfigCopy) {
8
+ const processEnvChanges = diffEnv(envBefore, env);
9
+ const netlifyConfigEnvChanges = diffEnv(netlifyConfig.build.environment, netlifyConfigCopy.build.environment);
10
+ return { ...processEnvChanges, ...netlifyConfigEnvChanges };
11
+ };
12
+ const diffEnv = function (envBefore, envAfter) {
13
+ const envChanges = filterObj(envAfter, (name, value) => value !== envBefore[name]);
14
+ const deletedEnv = filterObj(envBefore, (name) => envAfter[name] === undefined);
15
+ const deletedEnvA = mapObj(deletedEnv, setToNull);
16
+ return { ...envChanges, ...deletedEnvA };
17
+ };
18
+ // `undefined` is not JSON-serializable (which is used in process IPC), so we
19
+ // convert it to `null`
20
+ // Note: `process.env[name] = undefined` actually does
21
+ // `process.env[name] = 'undefined'` in Node.js.
22
+ const setToNull = function (name) {
23
+ return [name, null];
24
+ };
25
+ // Set `process.env` changes from a previous different plugin.
26
+ // Can also merge with a `currentEnv` plain object instead of `process.env`.
27
+ export const setEnvChanges = function (envChanges, currentEnv = env) {
28
+ Object.entries(envChanges).forEach(([name, value]) => {
29
+ setEnvChange(name, value, currentEnv);
30
+ });
31
+ return { ...currentEnv };
32
+ };
33
+ const setEnvChange = function (name, value, currentEnv) {
34
+ if (currentEnv[name] === value) {
35
+ return;
36
+ }
37
+ if (value === null) {
38
+ // `currentEnv` is a mutable variable
39
+ delete currentEnv[name];
40
+ return;
41
+ }
42
+ currentEnv[name] = value;
43
+ };
@@ -0,0 +1,14 @@
1
+ import { env } from 'process';
2
+ import filterObj from 'filter-obj';
3
+ import { getParentColorEnv } from '../log/colors.js';
4
+ // Retrieve the environment variables passed to plugins and `build.command`
5
+ // When run locally, this tries to emulate the production environment.
6
+ export const getChildEnv = function ({ envOpt, env: allConfigEnv }) {
7
+ const parentColorEnv = getParentColorEnv();
8
+ const parentEnv = { ...env, ...allConfigEnv, ...envOpt, ...parentColorEnv };
9
+ return filterObj(parentEnv, shouldKeepEnv);
10
+ };
11
+ const shouldKeepEnv = function (key) {
12
+ return !REMOVED_PARENT_ENV.has(key.toLowerCase());
13
+ };
14
+ const REMOVED_PARENT_ENV = new Set(['bugsnag_key']);
@@ -0,0 +1,68 @@
1
+ import { env } from 'process';
2
+ import filterObj from 'filter-obj';
3
+ // Retrieve environment variables used in error monitoring
4
+ export const getEnvMetadata = function (childEnv = env) {
5
+ return filterObj(childEnv, isEnvMetadata);
6
+ };
7
+ const isEnvMetadata = function (name) {
8
+ return ENVIRONMENT_VARIABLES.has(name);
9
+ };
10
+ // This is sorted by most useful in debugging to least
11
+ const ENVIRONMENT_VARIABLES = new Set([
12
+ // URL and IDs
13
+ 'BUILD_ID',
14
+ 'DEPLOY_ID',
15
+ 'SITE_NAME',
16
+ 'SITE_ID',
17
+ 'REPOSITORY_URL',
18
+ 'URL',
19
+ 'DEPLOY_URL',
20
+ 'DEPLOY_PRIME_URL',
21
+ // Git
22
+ 'HEAD',
23
+ 'COMMIT_REF',
24
+ 'CACHED_COMMIT_REF',
25
+ 'BRANCH',
26
+ 'CONTEXT',
27
+ 'PULL_REQUEST',
28
+ 'REVIEW_ID',
29
+ // Node
30
+ 'NODE_VERSION',
31
+ 'AWS_LAMBDA_JS_RUNTIME',
32
+ 'NPM_VERSION',
33
+ 'YARN_VERSION',
34
+ 'NPM_FLAGS',
35
+ 'YARN_FLAGS',
36
+ 'NVM_FLAGS',
37
+ 'NODE_ENV',
38
+ // Go
39
+ 'GO_VERSION',
40
+ 'GO_IMPORT_PATH',
41
+ 'GOCACHE',
42
+ 'GOPATH',
43
+ 'GOROOT',
44
+ // Ruby
45
+ 'RUBY_VERSION',
46
+ 'GEM_HOME',
47
+ 'GEM_PATH',
48
+ 'GIMME_GCO_ENABLED',
49
+ 'GIMME_ENV_PREFIX',
50
+ 'GIMME_GO_PREFIX',
51
+ 'GIMME_NO_ENV_ALIAS',
52
+ 'GIMME_TYPE',
53
+ 'IRBRC',
54
+ 'MY_RUBY_HOME',
55
+ // Hugo
56
+ 'HUGO_VERSION',
57
+ // Java
58
+ 'JAVA_VERSION',
59
+ // PHP
60
+ 'PHP_VERSION',
61
+ // Python
62
+ 'PIPENV_DEFAULT_PYTHON_VERSION',
63
+ 'PIPENV_RUNTIME',
64
+ 'PIPENV_VENV_IN_PROJECT',
65
+ // Git LFS
66
+ 'GIT_LFS_ENABLED',
67
+ 'GIT_LFS_FETCH_INCLUDE',
68
+ ]);
@@ -0,0 +1,37 @@
1
+ import isPlainObj from 'is-plain-obj';
2
+ import mapObj from 'map-obj';
3
+ import { addErrorInfo } from './info.js';
4
+ // Wrap `api.*` methods so that they add more error information
5
+ export const addApiErrorHandlers = function (api) {
6
+ if (api === undefined) {
7
+ return;
8
+ }
9
+ return mapObj(api, addErrorHandler);
10
+ };
11
+ const addErrorHandler = function (key, value) {
12
+ if (typeof value !== 'function') {
13
+ return [key, value];
14
+ }
15
+ const valueA = apiMethodHandler.bind(null, key, value);
16
+ return [key, valueA];
17
+ };
18
+ const apiMethodHandler = async function (endpoint, method, parameters, ...args) {
19
+ try {
20
+ return await method(parameters, ...args);
21
+ }
22
+ catch (error) {
23
+ redactError(error);
24
+ addErrorInfo(error, { type: 'api', location: { endpoint, parameters } });
25
+ throw error;
26
+ }
27
+ };
28
+ // Redact API token from the build logs
29
+ const redactError = function (error) {
30
+ if (error instanceof Error &&
31
+ isPlainObj(error.data) &&
32
+ isPlainObj(error.data.headers) &&
33
+ typeof error.data.headers.Authorization === 'string') {
34
+ error.data.headers.Authorization = error.data.headers.Authorization.replace(HEX_REGEXP, 'HEX');
35
+ }
36
+ };
37
+ const HEX_REGEXP = /[\da-f]{6,}/g;
@@ -0,0 +1,36 @@
1
+ import safeJsonStringify from 'safe-json-stringify';
2
+ import { CUSTOM_ERROR_KEY } from './info.js';
3
+ // Retrieve error information from child process and re-build it in current
4
+ // process. We need this since errors static properties are not kept by
5
+ // `v8.serialize()`.
6
+ export const jsonToError = function ({ name, message, stack, ...errorProps }) {
7
+ const error = new Error('');
8
+ assignErrorProps(error, { name, message, stack });
9
+ // Assign static error properties (if any)
10
+ // We need to mutate the `error` directly to preserve its `name`, `stack`, etc.
11
+ Object.assign(error, errorProps);
12
+ return error;
13
+ };
14
+ // Make sure `name`, `message` and `stack` are not enumerable
15
+ const assignErrorProps = function (error, values) {
16
+ ERROR_PROPS.forEach((name) => {
17
+ assignErrorProp(error, name, values[name]);
18
+ });
19
+ };
20
+ const ERROR_PROPS = ['name', 'message', 'stack'];
21
+ const assignErrorProp = function (error, name, value) {
22
+ // `Object.defineProperty()` requires direct mutation
23
+ Object.defineProperty(error, name, { value, enumerable: false, writable: true, configurable: true });
24
+ };
25
+ // Inverse of `jsonToError()`.
26
+ export const errorToJson = function (error) {
27
+ const { name, message, stack, [CUSTOM_ERROR_KEY]: customError, ...errorProps } = error;
28
+ // diagnosticText is not enumerable in TSError, so we need to grab it manually. destructuring won't work.
29
+ if (error.diagnosticText) {
30
+ errorProps.diagnosticText = error.diagnosticText;
31
+ }
32
+ return {
33
+ ...safeJsonStringify.ensureProperties(errorProps),
34
+ ...safeJsonStringify.ensureProperties({ name, message, stack, [CUSTOM_ERROR_KEY]: customError }),
35
+ };
36
+ };
@@ -0,0 +1,7 @@
1
+ // Cancel builds, for example when a plugin uses `utils.build.cancelBuild()`
2
+ export const cancelBuild = async function ({ api, deployId }) {
3
+ if (api === undefined || !deployId) {
4
+ return;
5
+ }
6
+ await api.cancelSiteDeploy({ deploy_id: deployId });
7
+ };
@@ -0,0 +1,9 @@
1
+ import stripAnsi from 'strip-ansi';
2
+ // Remove ANSI sequences from `error.message`
3
+ export const removeErrorColors = function (error) {
4
+ if (!(error instanceof Error)) {
5
+ return;
6
+ }
7
+ error.message = stripAnsi(error.message);
8
+ error.stack = stripAnsi(error.stack);
9
+ };
@@ -0,0 +1,44 @@
1
+ import { cwd as getCwd } from 'process';
2
+ import { pathExists } from 'path-exists';
3
+ import { logBuildError } from '../log/messages/core.js';
4
+ import { removeErrorColors } from './colors.js';
5
+ import { getErrorInfo } from './info.js';
6
+ import { reportBuildError } from './monitor/report.js';
7
+ import { parseErrorInfo } from './parse/parse.js';
8
+ // Logs and reports a build failure
9
+ export const handleBuildError = async function (error, { errorMonitor, netlifyConfig, childEnv, logs, debug, testOpts }) {
10
+ const basicErrorInfo = parseErrorInfo(error);
11
+ if (await isCancelCrash(error)) {
12
+ return basicErrorInfo;
13
+ }
14
+ removeErrorColors(error);
15
+ // Some errors, such as telemetry ones, should not be logged
16
+ if (basicErrorInfo.showInBuildLog) {
17
+ logBuildError({ error, netlifyConfig, logs, debug });
18
+ }
19
+ await reportBuildError({ error, errorMonitor, childEnv, logs, testOpts });
20
+ return basicErrorInfo;
21
+ };
22
+ // When builds are canceled, the whole filesystem is being deleted.
23
+ // However, the process (and build) keeps going. Because no files exist anymore,
24
+ // the build eventually crashes with a randomly odd error. Those should not be
25
+ // logged nor reported.
26
+ // However, builds canceled with `utils.build.cancelBuild()` should still show
27
+ // "Build canceled by ..."
28
+ const isCancelCrash = async function (error) {
29
+ const [{ type }] = getErrorInfo(error);
30
+ if (type === 'cancelBuild') {
31
+ return false;
32
+ }
33
+ try {
34
+ // TODO: find a better way to detect that the build is being cancelled.
35
+ // Otherwise bugs due to (for example) the build command removing
36
+ // `process.cwd` are currently not logged/reported.
37
+ const cwd = getCwd();
38
+ return !(await pathExists(cwd));
39
+ // `process.cwd()` fails when the current directory does not exist
40
+ }
41
+ catch {
42
+ return true;
43
+ }
44
+ };
@@ -0,0 +1,37 @@
1
+ // Add information related to an error without colliding with existing properties
2
+ export const addDefaultErrorInfo = function (error, info) {
3
+ if (!canHaveErrorInfo(error)) {
4
+ return;
5
+ }
6
+ error[CUSTOM_ERROR_KEY] = { ...info, ...error[CUSTOM_ERROR_KEY] };
7
+ };
8
+ // Retrieve error information added by our system
9
+ export const addErrorInfo = function (error, info) {
10
+ if (!canHaveErrorInfo(error)) {
11
+ return;
12
+ }
13
+ error[CUSTOM_ERROR_KEY] = { ...error[CUSTOM_ERROR_KEY], ...info };
14
+ };
15
+ export const getErrorInfo = function (error) {
16
+ if (!isBuildError(error)) {
17
+ return [{}, error];
18
+ }
19
+ const { [CUSTOM_ERROR_KEY]: errorInfo, ...errorA } = error;
20
+ return [errorInfo, errorA];
21
+ };
22
+ // Change error type from one to another
23
+ export const changeErrorType = function (error, oldType, newType) {
24
+ const [{ type }] = getErrorInfo(error);
25
+ if (type === oldType) {
26
+ addErrorInfo(error, { type: newType });
27
+ }
28
+ };
29
+ export const isBuildError = function (error) {
30
+ return canHaveErrorInfo(error) && error[CUSTOM_ERROR_KEY] !== undefined;
31
+ };
32
+ // Exceptions that are not objects (including `Error` instances) cannot have an
33
+ // `CUSTOM_ERROR_KEY` property
34
+ const canHaveErrorInfo = function (error) {
35
+ return error != null;
36
+ };
37
+ export const CUSTOM_ERROR_KEY = 'customErrorInfo';
@@ -0,0 +1,16 @@
1
+ // Retrieve plugin's location and build logs
2
+ export const getLocationMetadata = function (location, envMetadata) {
3
+ const buildLogs = getBuildLogs(envMetadata);
4
+ if (buildLogs === undefined && location === undefined) {
5
+ return;
6
+ }
7
+ return { buildLogs, ...location };
8
+ };
9
+ // Retrieve the URL to the build logs
10
+ const getBuildLogs = function ({ SITE_NAME, DEPLOY_ID }) {
11
+ if (SITE_NAME === undefined || DEPLOY_ID === undefined) {
12
+ return;
13
+ }
14
+ return `${NETLIFY_ORIGIN}/sites/${SITE_NAME}/deploys/${DEPLOY_ID}`;
15
+ };
16
+ const NETLIFY_ORIGIN = 'https://app.netlify.com';
@@ -0,0 +1,86 @@
1
+ // We group errors by `error.message`. However some `error.message` contain
2
+ // unique IDs, etc. which defeats that grouping. So we normalize those to make
3
+ // them consistent
4
+ export const normalizeGroupingMessage = function (message, type) {
5
+ const messageA = removeDependenciesLogs(message, type);
6
+ const messageB = NORMALIZE_REGEXPS.reduce(normalizeMessage, messageA);
7
+ // If this is a functions bundling error, we'll use additional normalization
8
+ // rules to group errors more aggressively.
9
+ if (type === 'functionsBundling') {
10
+ return FUNCTIONS_BUNDLING_REGEXPS.reduce(normalizeMessage, messageB);
11
+ }
12
+ return messageB;
13
+ };
14
+ // Discard debug/info installation information
15
+ const removeDependenciesLogs = function (message, type) {
16
+ if (type !== 'dependencies') {
17
+ return message;
18
+ }
19
+ return message.split('\n').filter(isErrorLine).join('\n');
20
+ };
21
+ const isErrorLine = function (line) {
22
+ return ERROR_LINES.some((errorLine) => line.startsWith(errorLine));
23
+ };
24
+ const ERROR_LINES = [
25
+ // Prefix for npm
26
+ 'npm ERR!',
27
+ // Prefix for Yarn
28
+ 'error',
29
+ ];
30
+ const normalizeMessage = function (message, [regExp, replacement]) {
31
+ return message.replace(regExp, replacement);
32
+ };
33
+ const NORMALIZE_REGEXPS = [
34
+ // Base64 URL
35
+ [/(data:[^;]+;base64),[\w+/-=]+/g, 'dataURI'],
36
+ // File paths
37
+ [/(["'`, ]|^)([^"'`, \n]*[/\\][^"'`, \n]*)(?=["'`, ]|$)/gm, '$1/file/path'],
38
+ // Semantic versions
39
+ [/\d+\.\d+\.\d+(-\d+)?/g, '1.0.0'],
40
+ [/version "[^"]+"/g, 'version "1.0.0"'],
41
+ // Cypress plugin prints the user's platform
42
+ [/^Platform: .*/gm, ''],
43
+ // URLs
44
+ [/https?:[\w.+~!$&'()*,;=:@/?#]+/g, 'https://domain.com'],
45
+ // Numbers, e.g. number of issues/problems
46
+ [/\d+/g, '0'],
47
+ // Hexadecimal strings
48
+ [/[\da-fA-F-]{6,}/g, 'hex'],
49
+ // On unknown inputs, we print the inputs
50
+ [/(does not accept any inputs but you specified: ).*/, '$1'],
51
+ [/(Unknown inputs for plugin).*/, '$1'],
52
+ [/(Plugin inputs should be one of: ).*/, '$1'],
53
+ // On required inputs, we print the inputs
54
+ [/^Plugin inputs[^]*/gm, ''],
55
+ [/(Required inputs for plugin).*/gm, '$1'],
56
+ // Netlify Functions validation check
57
+ [/(should target a directory, not a regular file):.*/, '$1'],
58
+ // zip-it-and-ship-it error when there is an `import()` but dependencies
59
+ // were not installed
60
+ [/(Cannot find module) '([^']+)'/g, "$1 'moduleName'"],
61
+ [/(A Netlify Function is using) "[^"]+"/g, '$1 "moduleName"'],
62
+ // Deploy upload errors include the filename
63
+ [/(Upload cancelled).*/g, '$1'],
64
+ [/(aborting upload of file).*/g, '$1'],
65
+ // netlify-plugin-inline-critical-css errors prints a list of file paths
66
+ [/Searched in: .*/g, ''],
67
+ // netlify-plugin-subfont prints font name in errors
68
+ [/(is not supported yet): .*/, '$1'],
69
+ // netlify-plugin-subfont prints generic information in every error that
70
+ // is highly build-specific
71
+ [/^(vers?ions|Plugin configuration|Subfont called with): {[^}]+}/gm, ''],
72
+ [/^Resolved entry points: \[[^\]]+]/gm, ''],
73
+ // netlify-plugin-minify-html parse error
74
+ [/(Parse Error):[^]*/, '$1'],
75
+ // Multiple empty lines
76
+ [/^\s*$/gm, ''],
77
+ ];
78
+ const FUNCTIONS_BUNDLING_REGEXPS = [
79
+ // String literals and identifiers
80
+ [/"([^"]+)"/g, '""'],
81
+ [/'([^']+)'/g, "''"],
82
+ [/`([^`]+)`/g, '``'],
83
+ // Rust crates
84
+ [/(?:Downloaded \S+ v[\d.]+\s*)+/gm, 'Downloaded crates'],
85
+ [/(?:Compiling \S+ v[\d.]+\s*)+/gm, 'Compiled crates'],
86
+ ];
@@ -0,0 +1,20 @@
1
+ import { log } from '../../log/logger.js';
2
+ // Print event payload instead of sending actual request during tests
3
+ export const printEventForTest = function ({ name: errorClass, message: errorMessage }, { context, groupingHash, severity, unhandled, _metadata: { location, plugin: { packageName, homepage } = {}, pluginPackageJson, tsConfig, env: { BUILD_ID } = {}, other, }, }, logs) {
4
+ const eventString = JSON.stringify({
5
+ errorClass,
6
+ errorMessage,
7
+ context,
8
+ groupingHash,
9
+ severity,
10
+ unhandled,
11
+ location,
12
+ packageName,
13
+ pluginPackageJson: pluginPackageJson !== undefined,
14
+ homepage,
15
+ tsConfig,
16
+ BUILD_ID,
17
+ other,
18
+ }, null, 2);
19
+ log(logs, `\nError monitoring payload:\n${eventString}`);
20
+ };
@@ -0,0 +1,120 @@
1
+ import { type as osType, freemem, totalmem } from 'os';
2
+ import { promisify } from 'util';
3
+ import osName from 'os-name';
4
+ import { getEnvMetadata } from '../../env/metadata.js';
5
+ import { log } from '../../log/logger.js';
6
+ import { parseErrorInfo } from '../parse/parse.js';
7
+ import { getHomepage } from '../parse/plugin.js';
8
+ import { getLocationMetadata } from './location.js';
9
+ import { normalizeGroupingMessage } from './normalize.js';
10
+ import { printEventForTest } from './print.js';
11
+ // Report a build failure for monitoring purpose
12
+ export const reportBuildError = async function ({ error, errorMonitor, childEnv, logs, testOpts }) {
13
+ if (errorMonitor === undefined) {
14
+ return;
15
+ }
16
+ const { errorInfo, type, severity, title, group = title } = parseErrorInfo(error);
17
+ const severityA = getSeverity(severity, errorInfo);
18
+ const groupA = getGroup(group, errorInfo);
19
+ const groupingHash = getGroupingHash(groupA, error, type, errorInfo);
20
+ const metadata = getMetadata(errorInfo, childEnv, groupingHash);
21
+ const app = getApp();
22
+ const eventProps = getEventProps({ severity: severityA, group: groupA, groupingHash, metadata, app });
23
+ const errorName = updateErrorName(error, type);
24
+ try {
25
+ await reportError({ errorMonitor, error, logs, testOpts, eventProps });
26
+ }
27
+ finally {
28
+ error.name = errorName;
29
+ }
30
+ };
31
+ // Plugin authors test their plugins as local plugins. Errors there are more
32
+ // like development errors, and should be reported as `info` only.
33
+ const getSeverity = function (severity, { location: { loadedFrom } = {} }) {
34
+ if (loadedFrom === 'local' || severity === 'none') {
35
+ return 'info';
36
+ }
37
+ return severity;
38
+ };
39
+ const getGroup = function (group, errorInfo) {
40
+ if (typeof group !== 'function') {
41
+ return group;
42
+ }
43
+ return group(errorInfo);
44
+ };
45
+ const getGroupingHash = function (group, error, type, errorInfo = {}) {
46
+ // If the error has a `normalizedMessage`, we use it as the grouping hash.
47
+ if (errorInfo.normalizedMessage) {
48
+ return errorInfo.normalizedMessage;
49
+ }
50
+ const message = error instanceof Error && typeof error.message === 'string' ? error.message : String(error);
51
+ const messageA = normalizeGroupingMessage(message, type);
52
+ return `${group}\n${messageA}`;
53
+ };
54
+ const getMetadata = function ({ location, plugin, tsConfig }, childEnv, groupingHash) {
55
+ const pluginMetadata = getPluginMetadata({ location, plugin });
56
+ const envMetadata = getEnvMetadata(childEnv);
57
+ const locationMetadata = getLocationMetadata(location, envMetadata);
58
+ return { location: locationMetadata, ...pluginMetadata, tsConfig, env: envMetadata, other: { groupingHash } };
59
+ };
60
+ const getPluginMetadata = function ({ location, plugin }) {
61
+ if (plugin === undefined) {
62
+ return {};
63
+ }
64
+ const { pluginPackageJson, ...pluginA } = plugin;
65
+ const homepage = getHomepage(pluginPackageJson, location);
66
+ return { plugin: { ...pluginA, homepage }, pluginPackageJson };
67
+ };
68
+ const getApp = function () {
69
+ return {
70
+ osName: osType(),
71
+ osVersion: osName(),
72
+ freeMemory: freemem(),
73
+ totalMemory: totalmem(),
74
+ };
75
+ };
76
+ // `error.name` is shown proeminently in the Bugsnag UI. We need to update it to
77
+ // match error `type` since it is more granular and useful.
78
+ // But we change it back after Bugsnag is done reporting.
79
+ const updateErrorName = function (error, type) {
80
+ const { name } = error;
81
+ // This might fail if `name` is a getter or is non-writable.
82
+ try {
83
+ error.name = type;
84
+ }
85
+ catch {
86
+ // continue regardless error
87
+ }
88
+ return name;
89
+ };
90
+ const reportError = async function ({ errorMonitor, error, logs, testOpts, eventProps }) {
91
+ if (testOpts.errorMonitor) {
92
+ printEventForTest(error, eventProps, logs);
93
+ return;
94
+ }
95
+ try {
96
+ await promisify(errorMonitor.notify)(error, (event) => onError(event, eventProps));
97
+ // Failsafe
98
+ }
99
+ catch {
100
+ log(logs, `Error monitor could not notify\n${error.stack}`);
101
+ }
102
+ };
103
+ const getEventProps = function ({ severity, group, groupingHash, metadata, app }) {
104
+ // `unhandled` is used to calculate Releases "stabiity score", which is
105
+ // basically the percentage of unhandled errors. Since we handle all errors,
106
+ // we need to implement this according to error types.
107
+ const unhandled = severity === 'error';
108
+ return { severity, context: group, groupingHash, _metadata: metadata, app, unhandled };
109
+ };
110
+ // Add more information to Bugsnag events
111
+ const onError = function (event, eventProps) {
112
+ // Bugsnag client requires directly mutating the `event`
113
+ Object.assign(event, {
114
+ ...eventProps,
115
+ unhandled: event.unhandled || eventProps.unhandled,
116
+ _metadata: { ...event._metadata, ...eventProps._metadata },
117
+ app: { ...event.app, ...eventProps.app },
118
+ });
119
+ return true;
120
+ };