Repository: netlify/zip-it-and-ship-it
Pull request #534 (2021-06-14 10:20:39)

fix: add limit to log lines produced by esbuild
**- Summary**

Limits the number of log lines produced by esbuild.

**- Test plan**

Added a new test.

**- A picture of a cute animal (not mandatory but encouraged)**

![images](https://user-images.githubusercontent.com/4162329/121877266-5aaf5200-cd02-11eb-83ca-bf3463c15090.jpg)


github-actions[bot]: ## ⏱ Benchmark results

*Comparing with f1363d24abeeeb11abaed0c564ca5ae8874a70ae*

### largeDepsEsbuild: 13s
 ⬆️ **9.89% increase** vs. f1363d24abeeeb11abaed0c564ca5ae8874a70ae
```
^                                                                                                   13s   
│                                                  12.1s   12.5s                   12.2s            ┌──┐  
│          11.4s   11.3s   11.5s   11.6s            ┌──┐    ┌──┐           11.4s    ┌──┐            |▒▒|  
│           ┌──┐    ┌──┐    ┌──┐    ┌──┐            |  |    |  |            ┌──┐    |  |   11.1s    |▒▒|  
│           |  |    |  |    |  |    |  |            |  |    |  |   10.3s    |  |    |  |    ┌──┐    |▒▒|  
│   9.7s    |  |    |  |    |  |    |  |    9.6s    |  |    |  |    ┌──┐    |  |    |  |    |  |    |▒▒|  
│   ┌──┐    |  |    |  |    |  |    |  |    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
```
<details>
<summary>Legend</summary>

- T-30 (f1363d24abeeeb11abaed0c564ca5ae8874a70ae): 11.7s
- T-29 (a5cef00715188d477ba69f033c16de00cea768c0): 13s
- T-28 (febeffcae56016e74348c4380fd5572edcbe84d0): 12.5s
- T-27 (991d9c3ab2e4e4da188024e3e005601ce9f14896): 12.3s
- T-26 (dd7dd81e5bf63586018d0ef384c399b2c1ba21c4): 11.6s
- T-25 (d4bcaf0ab1ab6f984d4658da105936a21011fe66): 12s
- T-24 (92f64a67160ac9eb0301ae738e38d258117f2b06): 10.3s
- T-23 (06bc4f69b05c96899a1efbd953c560219291b568): 11.6s
- T-22 (6c0e5f1059d5e672ea5ee6509150e0896030e745): 12.1s
- T-21 (e4c22192f1f66aae395b00fe8eaf4c886781d723): 11.4s
- T-20 (3ba92859e8fbf0f20abc46912284575c24e18124): 12s
- T-19 (5cdd4d23e88300d026f0152222136a35849c3b7f): 11.5s
- T-18 (df7f3e92e9547649a4c7c0b25d6e317af292c089): 12.7s
- T-17 (08f2a5d49a78e4424bc7478aa3cc8c98d21c5ee7): 9.9s
- T-16 (1e5ba0b8519f8ab643496979d7304d68d749d31e): 9.9s
- T-15 (1507586389e536db74867e3e8d3a7145373adfd3): 10s
- T-14 (6f4ab2998291fc3dd9dfa2d48d38be4d5ab8a901): 12s
- T-13 (d28f357f9dc98d7a0b278b99fee81daaa1e2c434): 12s
- T-12 (0687ba92a59971f75e554b70b407a85540f35616): 9.7s
- T-11 (98d18d9aa251f7f5758f55b3da27ccf066f98cc0): 11.4s
- T-10 (3de38a4f1d3c5c4b64c87a47926926a7812319e4): 11.3s
- T-9 (ab9f1d536555739628655ad674d4c0452ca47218): 11.5s
- T-8 (b248dfa11282ff23bd19001aa1ff66bd38a93d0b): 11.6s
- T-7 (c296d1706906a5d7b820cd7fb625c06453503f0e): 9.6s
- T-6 (62d7be931cf51cfb5cf5dba60a463678d69af4ce): 12.1s
- T-5 (8392f530c234ed8fa2881bad5c1ad7c937d7b588): 12.5s
- T-4 (772c7a216f983bb7d2e763aa0ef8ae0ae4f50f50): 10.3s
- T-3 (7085f74fa0bf71e204eecf269eabdc95ae84aa29): 11.4s
- T-2 (561de179a77b8877ccdf9cadaa3e6aaf54d45e20): 12.2s
- T-1 (e14fc0297b1e79c2a631af7b122b3ab0588525d1): 11.1s
- **T (current commit): 13s**</details>
### largeDepsZisi: 1m 7.9s
 ⬆️ **12.29% increase** vs. f1363d24abeeeb11abaed0c564ca5ae8874a70ae
```
^                                                         1m 9.7s                                         
│                                                 1m 5.5s   ┌──┐                  1m 5.2s         1m 7.9s 
│                                 1m 1.8s           ┌──┐    |  |          1m 2.4s   ┌──┐            ┌──┐  
│         1m 0.6s 1m 0.1s 1m 0.3s   ┌──┐            |  |    |  |            ┌──┐    |  |            |▒▒|  
│           ┌──┐    ┌──┐    ┌──┐    |  |            |  |    |  |   54.6s    |  |    |  |    56s     |▒▒|  
│  52.2s    |  |    |  |    |  |    |  |   51.8s    |  |    |  |    ┌──┐    |  |    |  |    ┌──┐    |▒▒|  
│   ┌──┐    |  |    |  |    |  |    |  |    ┌──┐    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
```
<details>
<summary>Legend</summary>

- T-30 (f1363d24abeeeb11abaed0c564ca5ae8874a70ae): 59.5s
- T-29 (a5cef00715188d477ba69f033c16de00cea768c0): 1m 9.5s
- T-28 (febeffcae56016e74348c4380fd5572edcbe84d0): 1m 3.7s
- T-27 (991d9c3ab2e4e4da188024e3e005601ce9f14896): 1m 2.9s
- T-26 (dd7dd81e5bf63586018d0ef384c399b2c1ba21c4): 59.5s
- T-25 (d4bcaf0ab1ab6f984d4658da105936a21011fe66): 1m 7.1s
- T-24 (92f64a67160ac9eb0301ae738e38d258117f2b06): 54.7s
- T-23 (06bc4f69b05c96899a1efbd953c560219291b568): 1m 4.4s
- T-22 (6c0e5f1059d5e672ea5ee6509150e0896030e745): 1m 3.6s
- T-21 (e4c22192f1f66aae395b00fe8eaf4c886781d723): 59.3s
- T-20 (3ba92859e8fbf0f20abc46912284575c24e18124): 1m 2.7s
- T-19 (5cdd4d23e88300d026f0152222136a35849c3b7f): 1m 2.4s
- T-18 (df7f3e92e9547649a4c7c0b25d6e317af292c089): 1m 6.3s
- T-17 (08f2a5d49a78e4424bc7478aa3cc8c98d21c5ee7): 52.2s
- T-16 (1e5ba0b8519f8ab643496979d7304d68d749d31e): 52.8s
- T-15 (1507586389e536db74867e3e8d3a7145373adfd3): 54.8s
- T-14 (6f4ab2998291fc3dd9dfa2d48d38be4d5ab8a901): 1m 5.4s
- T-13 (d28f357f9dc98d7a0b278b99fee81daaa1e2c434): 1m 5.9s
- T-12 (0687ba92a59971f75e554b70b407a85540f35616): 52.2s
- T-11 (98d18d9aa251f7f5758f55b3da27ccf066f98cc0): 1m 0.6s
- T-10 (3de38a4f1d3c5c4b64c87a47926926a7812319e4): 1m 0.1s
- T-9 (ab9f1d536555739628655ad674d4c0452ca47218): 1m 0.3s
- T-8 (b248dfa11282ff23bd19001aa1ff66bd38a93d0b): 1m 1.8s
- T-7 (c296d1706906a5d7b820cd7fb625c06453503f0e): 51.8s
- T-6 (62d7be931cf51cfb5cf5dba60a463678d69af4ce): 1m 5.5s
- T-5 (8392f530c234ed8fa2881bad5c1ad7c937d7b588): 1m 9.7s
- T-4 (772c7a216f983bb7d2e763aa0ef8ae0ae4f50f50): 54.6s
- T-3 (7085f74fa0bf71e204eecf269eabdc95ae84aa29): 1m 2.4s
- T-2 (561de179a77b8877ccdf9cadaa3e6aaf54d45e20): 1m 5.2s
- T-1 (e14fc0297b1e79c2a631af7b122b3ab0588525d1): 56s
- **T (current commit): 1m 7.9s**</details>
<!--delta:benchmark@{}-->

Patch:
diff --git a/src/runtimes/node/bundler.js b/src/runtimes/node/bundler.js
index 61a9b3f..ec87272 100644
--- a/src/runtimes/node/bundler.js
+++ b/src/runtimes/node/bundler.js
@@ -10,6 +10,11 @@ const { getBundlerTarget } = require('./bundler_target')
 const { getDynamicImportsPlugin } = require('./dynamic_imports')
 const { getNativeModulesPlugin } = require('./native_modules/plugin')
 
+// Maximum number of log messages that an esbuild instance will produce. This
+// limit is important to avoid out-of-memory errors due to too much data being
+// sent in the Go<>Node IPC channel.
+const ESBUILD_LOG_LIMIT = 10
+
 const supportsAsyncAPI = semver.satisfies(process.version, '>=9.x')
 
 // esbuild's async build API throws on Node 8.x, so we switch to the sync
@@ -52,6 +57,7 @@ const bundleJsFile = async function ({
       entryPoints: [srcFile],
       external,
       logLevel: 'warning',
+      logLimit: ESBUILD_LOG_LIMIT,
       metafile: true,
       nodePaths: additionalModulePaths,
       outfile: bundlePath,
@@ -96,4 +102,4 @@ const getSourcemapPath = (outputs) => {
   }
 }
 
-module.exports = { bundleJsFile }
+module.exports = { bundleJsFile, ESBUILD_LOG_LIMIT }


Test patch:
diff --git a/tests/fixtures/esbuild-log-limit/function.js b/tests/fixtures/esbuild-log-limit/function.js
new file mode 100644
index 0000000..18662fe
--- /dev/null
+++ b/tests/fixtures/esbuild-log-limit/function.js
@@ -0,0 +1,17 @@
+/* eslint-disable import/no-unresolved */
+const var1 = require('module-01')
+const var2 = require('module-02')
+const var3 = require('module-03')
+const var4 = require('module-04')
+const var5 = require('module-05')
+const var6 = require('module-06')
+const var7 = require('module-07')
+const var8 = require('module-08')
+const var9 = require('module-09')
+const var10 = require('module-10')
+const var11 = require('module-11')
+const var12 = require('module-12')
+const var13 = require('module-13')
+/* eslint-enable import/no-unresolved */
+
+module.exports = [var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13]
diff --git a/tests/main.js b/tests/main.js
index 502efd3..f9aaa38 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -15,6 +15,7 @@ const { dir: getTmpDir, tmpName } = require('tmp-promise')
 const unixify = require('unixify')
 
 const { zipFunction, listFunctions, listFunctionsFiles } = require('..')
+const { ESBUILD_LOG_LIMIT } = require('../src/runtimes/node/bundler')
 const {
   JS_BUNDLER_ESBUILD: ESBUILD,
   JS_BUNDLER_ESBUILD_ZISI: ESBUILD_ZISI,
@@ -864,6 +865,23 @@ testBundlers(
   },
 )
 
+test('Limits the amount of log lines produced by esbuild', async (t) => {
+  const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
+  const binaryPath = resolve(__dirname, '../src/bin.js')
+  const fixturePath = join(FIXTURES_DIR, 'esbuild-log-limit')
+
+  try {
+    await execa(binaryPath, [fixturePath, tmpDir, `--config.*.nodeBundler=esbuild`])
+
+    t.fail('Bundling should have thrown')
+  } catch (error) {
+    const logCount = (error.stderr.match(/require\('module-\d+'\)/g) || []).length
+
+    t.true(logCount <= ESBUILD_LOG_LIMIT)
+    t.true(error.stderr.includes(`${ESBUILD_LOG_LIMIT} of 13 errors shown`))
+  }
+})
+
 // We're not running this test for the `DEFAULT` bundler — not because it's not
 // supported, but because the legacy bundler doesn't use any of the available
 // configuration properties and therefore there is nothing we could test.