From 0af5363804a2e37fb20dc0a0944e3713b95abd85 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:58:23 +0000 Subject: [PATCH 01/13] Accept thread arguments filter --- lib/cli.js | 6 ++++++ lib/fork.js | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 4de8cd57d..2f17a8442 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -388,9 +388,15 @@ export default async function loadCli() { // eslint-disable-line complexity exit(error.message); } + const workerThreads = combined.workerThreads !== false; + let nodeArguments; try { nodeArguments = normalizeNodeArguments(conf.nodeArguments, argv['node-arguments']); + nodeArguments ??= process.execArgv; + if (workerThreads && conf.threadArgumentsFilter) { + nodeArguments = nodeArguments.filter(conf.threadArgumentsFilter); + } } catch (error) { exit(error.message); } diff --git a/lib/fork.js b/lib/fork.js index 8317774ff..b2b2405e6 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -60,7 +60,7 @@ const createWorker = (options, execArgv) => { }; }; -export default function loadFork(file, options, execArgv = process.execArgv) { +export default function loadFork(file, options, execArgv) { let finished = false; const emitter = new Emittery(); From fb1539f757e580f98a708fea81873dc0603d277f Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:27:19 +0000 Subject: [PATCH 02/13] Validate threadArgumentsFilter --- lib/load-config.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/load-config.js b/lib/load-config.js index b6526a3c2..68d2f265b 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -157,6 +157,10 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau ...defaults, nonSemVerExperiments: {}, ...fileConf, ...packageConf, projectDir, configFile, }; + if ('threadArgumentsFilter' in config && typeof config.threadArgumentsFilter !== 'function') { + throw new Error(`threadArgumentsFilter from ${fileForErrorMessage} must be a function`); + } + const {nonSemVerExperiments: experiments} = config; if (!isPlainObject(experiments)) { throw new Error(`nonSemVerExperiments from ${fileForErrorMessage} must be an object`); From 7146c5251517a3f91445a0fa443c276b381434cb Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:50:50 +0000 Subject: [PATCH 03/13] Test invalid threadArgumentsFilter --- test-tap/fixture/load-config/non-function/ava.config.js | 3 +++ test-tap/fixture/load-config/non-function/package.json | 3 +++ test/config/loader.js | 2 ++ 3 files changed, 8 insertions(+) create mode 100644 test-tap/fixture/load-config/non-function/ava.config.js create mode 100644 test-tap/fixture/load-config/non-function/package.json diff --git a/test-tap/fixture/load-config/non-function/ava.config.js b/test-tap/fixture/load-config/non-function/ava.config.js new file mode 100644 index 000000000..749214339 --- /dev/null +++ b/test-tap/fixture/load-config/non-function/ava.config.js @@ -0,0 +1,3 @@ +export default { + threadArgumentsFilter: [], +}; diff --git a/test-tap/fixture/load-config/non-function/package.json b/test-tap/fixture/load-config/non-function/package.json new file mode 100644 index 000000000..bedb411a9 --- /dev/null +++ b/test-tap/fixture/load-config/non-function/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/config/loader.js b/test/config/loader.js index 74c43ffbc..5a128e76f 100644 --- a/test/config/loader.js +++ b/test/config/loader.js @@ -117,6 +117,8 @@ test.serial('throws an error if a .js config file has no default export', notOk( test.serial('throws an error if a config file contains `ava` property', notOk('contains-ava-property')); +test.serial('throws an error if a config file contains a non-function `threadArgumentsFilter` property', notOk('non-function')); + test.serial('throws an error if a config file contains a non-object `nonSemVerExperiments` property', notOk('non-object-experiments')); test.serial('throws an error if a config file enables an unsupported experiment', notOk('unsupported-experiments')); From d2cfdbb0f857e06f5eb651e7c7953e9d27422e68 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:52:00 +0000 Subject: [PATCH 04/13] Normalize threadArgumentsFilter usage Conforms to eslint rule for safe Array#filter usage. --- lib/cli.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 2f17a8442..8fac5fcf7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -394,8 +394,9 @@ export default async function loadCli() { // eslint-disable-line complexity try { nodeArguments = normalizeNodeArguments(conf.nodeArguments, argv['node-arguments']); nodeArguments ??= process.execArgv; - if (workerThreads && conf.threadArgumentsFilter) { - nodeArguments = nodeArguments.filter(conf.threadArgumentsFilter); + if (workerThreads && 'threadArgumentsFilter' in conf) { + const {threadArgumentsFilter: filter} = conf; + nodeArguments = nodeArguments.filter(argument => filter(argument)); } } catch (error) { exit(error.message); From 05b9b21115709769015e69ee6c93c3a69ad20ae6 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:56:11 +0000 Subject: [PATCH 05/13] Save test snapshot --- test/config/snapshots/loader.js.md | 6 ++++++ test/config/snapshots/loader.js.snap | Bin 594 -> 625 bytes 2 files changed, 6 insertions(+) diff --git a/test/config/snapshots/loader.js.md b/test/config/snapshots/loader.js.md index 499926429..7074ece76 100644 --- a/test/config/snapshots/loader.js.md +++ b/test/config/snapshots/loader.js.md @@ -40,6 +40,12 @@ Generated by [AVA](https://avajs.dev). 'Encountered ’ava’ property in ava.config.js; avoid wrapping the configuration' +## throws an error if a config file contains a non-function `threadArgumentsFilter` property + +> error message + + 'threadArgumentsFilter from ava.config.js must be a function' + ## throws an error if a config file contains a non-object `nonSemVerExperiments` property > error message diff --git a/test/config/snapshots/loader.js.snap b/test/config/snapshots/loader.js.snap index 83344ffc9c7ecd858f0f0c67af9dad1e56a16a00..8b209f7cd80a66277d79d6cf3c947ce2e22ee1fe 100644 GIT binary patch literal 625 zcmV-%0*?JbRzVmF*7iDeuRb7Hdgv+^+}n%4=G_@VS*+2gpFyiUpNH zEIx-sI8$>6XbFrZN9>hjG%gqKem`ar{?49`o}XQeg)^-ZuXx_>uPk~c z42l647_AqhqSi~nU~+?ViV7!88e`JsnxQ1;a84cL>y}YvZPlsL1Y(-XIX3VWyUG}` zo>HKtCw5ABANXqzcsiC!r@-Pt3#M&#$dslFP##>~{LgVg5EbqvL`mU5EJS7D{>con ze+cv!H$s?ru`5W0Tti{WSC#?6XjBXm7nB6re%W&|p0;I5^10OZ+djmrou-$S`;W$u zh4Uc`8}zm6LG;f(^tS=NV4=F{UAMA|6MvxXVdh$6>x`T1us})~R=QK(XTIt&PbN*l zIxuz4dk)S+4R7LuJ1WQIJ)D^uJQ4P&*ue}JI0IwFkoBv5^dCL+%Lyk^u_yaC9glbS zxWLD|`_1;fTY3Sw5S7BMMPq8SKBxZ09;q!Tpd~DMJSo*T-{As;*CU)P{G)!dFyYlbtLLS=B%d+4u6^m*cz5EvwzZNJE`#|y0crf zAPvoi?H`K>00000000A(mCtU|KoG`LA;g(Cm^+X&G;kqG5OAuBDsE(KkK=9EJJ#;H zO>S`H5jfIE<4HQ}KT+H)PMU*bdo?q^@7tO6-#pQr-nifQIfIoe*t=i%S%x0Je||Q3F$ur3=aZLbv$@1*m3n2?U3_iP zD>k4E;4Mzum(%M|FM|rx8(fn-vLTHL>9S=g$vIi*8Q*q{g4QNjDowyulexeKzBaC| zDz=_7pp7TvWbqLA+W>eu=UQiA*n?)wy6lik`WYw>5*pSEyd@CI*Dy^5T>z)0{O`() zv44#Ci@QRYdbV50m|R0)$=6n_f=Z)U#>q9M5!){YD(1^BO-a7sAiwh=**-G8tlfWV zj7c~jldwZ?s~$!FGC+SH;fp2;ruS~;wMqRE+vCik#@-$`y<-?M%CXjcLUd(Z@l6otdPp-)N?IHS)0s6(lq+A=jCHI$9UXfjFgiDoj^tdsR zp9Uh|#lj)CT*fvhp}=>12*&3)h#`ZCrpRH`Lmb_m82EHQS44zrrw<`k@u^ML9Q^(1 g+?;hMeFeS_WPEtyS77grRE!}%0O7-DwzUQT0O=MZ00000 From 6ae4ab4bc5ca59878e75dddf7953396d4bcbb3a7 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:37:54 +0000 Subject: [PATCH 06/13] Test fork thread with explicit execArgv Necessary due to default being removed, in favor of explicit filtered execArgv constructed at higher level. --- test-tap/api.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-tap/api.js b/test-tap/api.js index e65eb5982..6820efd2d 100644 --- a/test-tap/api.js +++ b/test-tap/api.js @@ -1,5 +1,6 @@ import fs from 'node:fs'; import path from 'node:path'; +import process from 'node:process'; import {fileURLToPath} from 'node:url'; import ciInfo from 'ci-info'; @@ -20,6 +21,7 @@ async function apiCreator(options = {}) { options.globs = normalizeGlobs({ files: options.files, ignoredByWatcher: options.watchMode?.ignoreChanges, extensions: options.extensions, providers: [], }); + options.nodeArguments = process.execArgv; const instance = new Api(options); return instance; From 94be613762a98ab81bbe901071f7fbe401785797 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 23:23:53 +0000 Subject: [PATCH 07/13] Test threadArgumentsFilter filters for thread --- .../thread-arguments-filter/package.json | 3 +++ .../thread-arguments-filter.config.mjs | 9 +++++++++ .../fixtures/thread-arguments-filter/thread.js | 7 +++++++ test/node-arguments/snapshots/test.js.md | 11 +++++++++++ test/node-arguments/snapshots/test.js.snap | Bin 440 -> 507 bytes test/node-arguments/test.js | 10 ++++++++++ 6 files changed, 40 insertions(+) create mode 100644 test/node-arguments/fixtures/thread-arguments-filter/package.json create mode 100644 test/node-arguments/fixtures/thread-arguments-filter/thread-arguments-filter.config.mjs create mode 100644 test/node-arguments/fixtures/thread-arguments-filter/thread.js diff --git a/test/node-arguments/fixtures/thread-arguments-filter/package.json b/test/node-arguments/fixtures/thread-arguments-filter/package.json new file mode 100644 index 000000000..bedb411a9 --- /dev/null +++ b/test/node-arguments/fixtures/thread-arguments-filter/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/node-arguments/fixtures/thread-arguments-filter/thread-arguments-filter.config.mjs b/test/node-arguments/fixtures/thread-arguments-filter/thread-arguments-filter.config.mjs new file mode 100644 index 000000000..aa7dcb44c --- /dev/null +++ b/test/node-arguments/fixtures/thread-arguments-filter/thread-arguments-filter.config.mjs @@ -0,0 +1,9 @@ +const processOnly = new Set(['--allow-natives-syntax']); + +export default { + nodeArguments: [ + '--throw-deprecation', + '--allow-natives-syntax', + ], + threadArgumentsFilter: argument => !processOnly.has(argument), +}; diff --git a/test/node-arguments/fixtures/thread-arguments-filter/thread.js b/test/node-arguments/fixtures/thread-arguments-filter/thread.js new file mode 100644 index 000000000..9d65540bb --- /dev/null +++ b/test/node-arguments/fixtures/thread-arguments-filter/thread.js @@ -0,0 +1,7 @@ +import test from 'ava'; + +test('exec arguments filtered', t => { + t.plan(2); + t.truthy(process.execArgv.includes('--throw-deprecation')); + t.falsy(process.execArgv.includes('--allow-natives-syntax')); +}); diff --git a/test/node-arguments/snapshots/test.js.md b/test/node-arguments/snapshots/test.js.md index 12555650a..78234130b 100644 --- a/test/node-arguments/snapshots/test.js.md +++ b/test/node-arguments/snapshots/test.js.md @@ -15,6 +15,17 @@ Generated by [AVA](https://avajs.dev). }, ] +## `threadArgumentsFilter` configuration filters arguments for worker thread + +> tests pass + + [ + { + file: 'thread.js', + title: 'exec arguments filtered', + }, + ] + ## detects incomplete --node-arguments > fails with message diff --git a/test/node-arguments/snapshots/test.js.snap b/test/node-arguments/snapshots/test.js.snap index c8e204a5149c2e10bab9fc13bc365185e521c98d..ddb06dae2df1d6b531c99918bec72b6811b6f5c5 100644 GIT binary patch literal 507 zcmVs@PSO%705KJ9uv3AQ z!iIb>Oe&Ub%{~dGK${TP9L0)PjZ`7Yw+mxbQlI~h;URPnKElx(0C)=k$9Ocu>?B5Y zjyS9)lL_DxKm>e>_--f~mjF-$z|W!Z=ybVaop7{UPN!4AXMiKX2^{0-IgSCd89pei z<6$%FROdU@^OfvEJUg{yw@NHYwu}Z~ z9~Y^Ltjx*K=7V*Kc(+m%JZ8s9&-7*I-!kC71b=FO5RS|*I002c)_6q<2 literal 440 zcmV;p0Z0BpRzVNaufRU0>m z+G+H1OCO5}00000000B6Qn60MFc9^%1P}7$F^)Sr9W~Wvmll61R?B`5f9x zEQrrwq6`1Rckm%Nty0l;U;s;&@8rAFd+*+Ro@$$o>2-l#d5tqZ5kx4tu@bp(MO~rs z#J%NH>&ECvkr5Lrt*4iTl){V0uP!@a&4EMcT!r^K+*8=^Lv-t$m; { t.snapshot(result.stats.passed, 'tests pass'); }); +test('`threadArgumentsFilter` configuration filters arguments for worker thread', async t => { + const options = { + cwd: cwd('thread-arguments-filter'), + }; + + const result = await fixture(['--config=thread-arguments-filter.config.mjs', 'thread.js'], options); + + t.snapshot(result.stats.passed, 'tests pass'); +}); + test('detects incomplete --node-arguments', async t => { const options = { cwd: cwd('node-arguments'), From e39f509a4f2aa21057920278117faa0f8c12fe72 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 23:34:34 +0000 Subject: [PATCH 08/13] Test threadArgumentsFilter ignored for process --- .../fixtures/thread-arguments-filter/process.js | 7 +++++++ test/node-arguments/snapshots/test.js.md | 11 +++++++++++ test/node-arguments/snapshots/test.js.snap | Bin 507 -> 547 bytes test/node-arguments/test.js | 10 ++++++++++ 4 files changed, 28 insertions(+) create mode 100644 test/node-arguments/fixtures/thread-arguments-filter/process.js diff --git a/test/node-arguments/fixtures/thread-arguments-filter/process.js b/test/node-arguments/fixtures/thread-arguments-filter/process.js new file mode 100644 index 000000000..b831c723a --- /dev/null +++ b/test/node-arguments/fixtures/thread-arguments-filter/process.js @@ -0,0 +1,7 @@ +import test from 'ava'; + +test('exec arguments unfiltered', t => { + t.plan(2); + t.truthy(process.execArgv.includes('--throw-deprecation')); + t.truthy(process.execArgv.includes('--allow-natives-syntax')); +}); diff --git a/test/node-arguments/snapshots/test.js.md b/test/node-arguments/snapshots/test.js.md index 78234130b..05b21577f 100644 --- a/test/node-arguments/snapshots/test.js.md +++ b/test/node-arguments/snapshots/test.js.md @@ -26,6 +26,17 @@ Generated by [AVA](https://avajs.dev). }, ] +## `threadArgumentsFilter` configuration ignored for worker process + +> tests pass + + [ + { + file: 'process.js', + title: 'exec arguments unfiltered', + }, + ] + ## detects incomplete --node-arguments > fails with message diff --git a/test/node-arguments/snapshots/test.js.snap b/test/node-arguments/snapshots/test.js.snap index ddb06dae2df1d6b531c99918bec72b6811b6f5c5..853b95b6d4797918705beee6842fd07700837d76 100644 GIT binary patch literal 547 zcmV+;0^I#URzV9_l+4be+j3(OUR|L+^6Nj zXeQ&o%O8sf00000000B6l(CM}KoEv!6MVoSlthPwD3C^?1gD^;;Dm%kfoLhS*&REZ ztaq)QHMx^$uHXrvqv9G~1;K0ZAjokX+rX1|(>bXqdYk2NI0SJYL=VIO&P4Bl=z~Z` z;#OuXwVP3`I^C!q&$;t;vMRFOJ*5j$TSXeDY^8}YP2(*1WvZzRq+~SSk(E=j^GF+5kLoyMtD3$~JH)Ergyf$|}ciuQ8c)DkjjEaTF|85YDWbJ~oa+6dg8JX!F?+6}t} l{XAN)zaQA^$2ZBPwd}N!y|4XA?P%U<^&iq7$0(Zx002ZA3WWdw literal 507 zcmVs@PSO%705KJ9uv3AQ z!iIb>Oe&Ub%{~dGK${TP9L0)PjZ`7Yw+mxbQlI~h;URPnKElx(0C)=k$9Ocu>?B5Y zjyS9)lL_DxKm>e>_--f~mjF-$z|W!Z=ybVaop7{UPN!4AXMiKX2^{0-IgSCd89pei z<6$%FROdU@^OfvEJUg{yw@NHYwu}Z~ z9~Y^Ltjx*K=7V*Kc(+m%JZ8s9&-7*I-!kC71b=FO5RS|*I002c)_6q<2 diff --git a/test/node-arguments/test.js b/test/node-arguments/test.js index ac510ce3e..417b98aef 100644 --- a/test/node-arguments/test.js +++ b/test/node-arguments/test.js @@ -23,6 +23,16 @@ test('`threadArgumentsFilter` configuration filters arguments for worker thread' t.snapshot(result.stats.passed, 'tests pass'); }); +test('`threadArgumentsFilter` configuration ignored for worker process', async t => { + const options = { + cwd: cwd('thread-arguments-filter'), + }; + + const result = await fixture(['--config=thread-arguments-filter.config.mjs', '--no-worker-threads', 'process.js'], options); + + t.snapshot(result.stats.passed, 'tests pass'); +}); + test('detects incomplete --node-arguments', async t => { const options = { cwd: cwd('node-arguments'), From fdb229b8d4b3ea6b4107743977181a0be90a16b5 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Mon, 22 Jul 2024 23:42:02 +0000 Subject: [PATCH 09/13] Document threadArgumentsFilter --- docs/06-configuration.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/06-configuration.md b/docs/06-configuration.md index 31531fe29..622c6579e 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -337,3 +337,18 @@ These may also export a function which is then invoked, and can receive argument The `nodeArguments` configuration may be used to specify additional arguments for launching worker processes. These are combined with `--node-arguments` passed on the CLI and any arguments passed to the `node` binary when starting AVA. [CLI]: ./05-command-line.md + +## Thread arguments filter + +In a config file only, `threadArgumentsFilter` may provide a function used for filtering `nodeArguments` sent to worker threads. This enables excluding arguments that throw if sent to a thread. The filter is ignored by worker processes. + +```js +const processOnly = new Set([ + '--allow-natives-syntax', + '--expose-gc' +]); + +export default { + threadArgumentsFilter: argument => !processOnly.has(argument) +} +``` From 3550e289fe89ceaaa9a28078078409da17c12275 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Tue, 23 Jul 2024 21:35:48 -0600 Subject: [PATCH 10/13] Title example configuration file Co-authored-by: Tommy --- docs/06-configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/06-configuration.md b/docs/06-configuration.md index 622c6579e..c5bca2e22 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -342,6 +342,7 @@ The `nodeArguments` configuration may be used to specify additional arguments fo In a config file only, `threadArgumentsFilter` may provide a function used for filtering `nodeArguments` sent to worker threads. This enables excluding arguments that throw if sent to a thread. The filter is ignored by worker processes. +`ava.config.js`: ```js const processOnly = new Set([ '--allow-natives-syntax', From 3cc44eb9bdd36814037da979688c2382f992036b Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:46:36 -0600 Subject: [PATCH 11/13] Remove unreachable default Co-authored-by: Mark Wubben --- lib/cli.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 8fac5fcf7..c180b6252 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -393,7 +393,6 @@ export default async function loadCli() { // eslint-disable-line complexity let nodeArguments; try { nodeArguments = normalizeNodeArguments(conf.nodeArguments, argv['node-arguments']); - nodeArguments ??= process.execArgv; if (workerThreads && 'threadArgumentsFilter' in conf) { const {threadArgumentsFilter: filter} = conf; nodeArguments = nodeArguments.filter(argument => filter(argument)); From e40eed7fa04a2f30f9cb25e32dfd7be965b3d002 Mon Sep 17 00:00:00 2001 From: bojavou <134245276+bojavou@users.noreply.github.com> Date: Fri, 2 Aug 2024 20:10:15 +0000 Subject: [PATCH 12/13] Make option name more explicit --- docs/06-configuration.md | 6 ++-- lib/cli.js | 4 +-- lib/load-config.js | 7 +++-- .../load-config/non-function/ava.config.js | 2 +- test/config/loader.js | 2 +- test/config/snapshots/loader.js.md | 10 +++++-- test/config/snapshots/loader.js.snap | Bin 625 -> 660 bytes .../thread-arguments-filter.config.mjs | 2 +- test/node-arguments/snapshots/test.js.md | 26 ++++++++++++++++-- test/node-arguments/snapshots/test.js.snap | Bin 547 -> 583 bytes test/node-arguments/test.js | 4 +-- 11 files changed, 47 insertions(+), 16 deletions(-) diff --git a/docs/06-configuration.md b/docs/06-configuration.md index c5bca2e22..c3941f41a 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -338,9 +338,9 @@ The `nodeArguments` configuration may be used to specify additional arguments fo [CLI]: ./05-command-line.md -## Thread arguments filter +## Node arguments filter for worker threads -In a config file only, `threadArgumentsFilter` may provide a function used for filtering `nodeArguments` sent to worker threads. This enables excluding arguments that throw if sent to a thread. The filter is ignored by worker processes. +In a config file only, `filterNodeArgumentsForWorkerThreads` may provide a function used for filtering `nodeArguments` sent to worker threads. This enables excluding arguments that throw if sent to a thread. The filter is ignored by worker processes. `ava.config.js`: ```js @@ -350,6 +350,6 @@ const processOnly = new Set([ ]); export default { - threadArgumentsFilter: argument => !processOnly.has(argument) + filterNodeArgumentsForWorkerThreads: argument => !processOnly.has(argument) } ``` diff --git a/lib/cli.js b/lib/cli.js index c180b6252..dd0c9feb2 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -393,8 +393,8 @@ export default async function loadCli() { // eslint-disable-line complexity let nodeArguments; try { nodeArguments = normalizeNodeArguments(conf.nodeArguments, argv['node-arguments']); - if (workerThreads && 'threadArgumentsFilter' in conf) { - const {threadArgumentsFilter: filter} = conf; + if (workerThreads && 'filterNodeArgumentsForWorkerThreads' in conf) { + const {filterNodeArgumentsForWorkerThreads: filter} = conf; nodeArguments = nodeArguments.filter(argument => filter(argument)); } } catch (error) { diff --git a/lib/load-config.js b/lib/load-config.js index 68d2f265b..be7b36d96 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -157,8 +157,11 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau ...defaults, nonSemVerExperiments: {}, ...fileConf, ...packageConf, projectDir, configFile, }; - if ('threadArgumentsFilter' in config && typeof config.threadArgumentsFilter !== 'function') { - throw new Error(`threadArgumentsFilter from ${fileForErrorMessage} must be a function`); + if ( + 'filterNodeArgumentsForWorkerThreads' in config + && typeof config.filterNodeArgumentsForWorkerThreads !== 'function' + ) { + throw new Error(`filterNodeArgumentsForWorkerThreads from ${fileForErrorMessage} must be a function`); } const {nonSemVerExperiments: experiments} = config; diff --git a/test-tap/fixture/load-config/non-function/ava.config.js b/test-tap/fixture/load-config/non-function/ava.config.js index 749214339..66d67d233 100644 --- a/test-tap/fixture/load-config/non-function/ava.config.js +++ b/test-tap/fixture/load-config/non-function/ava.config.js @@ -1,3 +1,3 @@ export default { - threadArgumentsFilter: [], + filterNodeArgumentsForWorkerThreads: [], }; diff --git a/test/config/loader.js b/test/config/loader.js index 5a128e76f..af25c248d 100644 --- a/test/config/loader.js +++ b/test/config/loader.js @@ -117,7 +117,7 @@ test.serial('throws an error if a .js config file has no default export', notOk( test.serial('throws an error if a config file contains `ava` property', notOk('contains-ava-property')); -test.serial('throws an error if a config file contains a non-function `threadArgumentsFilter` property', notOk('non-function')); +test.serial('throws an error if a config file contains a non-function `filterNodeArgumentsForWorkerThreads` property', notOk('non-function')); test.serial('throws an error if a config file contains a non-object `nonSemVerExperiments` property', notOk('non-object-experiments')); diff --git a/test/config/snapshots/loader.js.md b/test/config/snapshots/loader.js.md index 7074ece76..7a3c373d3 100644 --- a/test/config/snapshots/loader.js.md +++ b/test/config/snapshots/loader.js.md @@ -40,11 +40,11 @@ Generated by [AVA](https://avajs.dev). 'Encountered ’ava’ property in ava.config.js; avoid wrapping the configuration' -## throws an error if a config file contains a non-function `threadArgumentsFilter` property +## throws an error if a config file contains a non-function `filterNodeArgumentsForWorkerThreads` property > error message - 'threadArgumentsFilter from ava.config.js must be a function' + 'filterNodeArgumentsForWorkerThreads from ava.config.js must be a function' ## throws an error if a config file contains a non-object `nonSemVerExperiments` property @@ -63,3 +63,9 @@ Generated by [AVA](https://avajs.dev). > error message 'Conflicting configuration in ava.config.js and ava.config.cjs' + +## throws an error if a config file contains a non-function `threadArgumentsFilter` property + +> error message + + 'threadArgumentsFilter from ava.config.js must be a function' diff --git a/test/config/snapshots/loader.js.snap b/test/config/snapshots/loader.js.snap index 8b209f7cd80a66277d79d6cf3c947ce2e22ee1fe..f42c1bf106bb3d426148c89bf67ebcde225b7a99 100644 GIT binary patch literal 660 zcmV;F0&D$2RzVmBDV)KoEvoA;g(Cm^oOyyC>7(^Yz>d?#&EllZ(b3NT{j>k<{K;Y~$(j3oL*95x7q5J-a_#^bNL3U} z2C?`Y65-6u9H0_ZN{-kY$5^>sy!&;Nrs(ne!LywgJI!zR`OeGT{gH5HP2vsDy7RT7 zH^N{TaE{U9V^lPHDO8x8;f$if36sT`Y`I`4$vK?SlJRZFXtJ*A#8?8U+R8b$@HM-- zszg1dKo_3a8R2!{;~wy2B(+U}#e)@0yXue`OZ#9vxV-&8$2mb%xYrORg#%HD%EH~t z46%O-^cU}hF!7?7kP5k$Ldn<4fKXX93=$WV1loSsb1|B9WlHk7H1@kb#Fr16Ue@kE z8bcQLhAiBnFH|?8f9avW5AX#GHBDc3E2}y28`>UbZZuZzxXlg=q?BQ8m&)tRM?L28 zxGmTOrpbBF!Ct80ZG7;7$~JiqXQly9ggqK|FvU4ep;DmF*7iDeuRb7Hdgv+^+}n%4=G_@VS*+2gpFyiUpNH zEIx-sI8$>6XbFrZN9>hjG%gqKem`ar{?49`o}XQeg)^-ZuXx_>uPk~c z42l647_AqhqSi~nU~+?ViV7!88e`JsnxQ1;a84cL>y}YvZPlsL1Y(-XIX3VWyUG}` zo>HKtCw5ABANXqzcsiC!r@-Pt3#M&#$dslFP##>~{LgVg5EbqvL`mU5EJS7D{>con ze+cv!H$s?ru`5W0Tti{WSC#?6XjBXm7nB6re%W&|p0;I5^10OZ+djmrou-$S`;W$u zh4Uc`8}zm6LG;f(^tS=NV4=F{UAMA|6MvxXVdh$6>x`T1us})~R=QK(XTIt&PbN*l zIxuz4dk)S+4R7LuJ1WQIJ)D^uJQ4P&*ue}JI0IwFkoBv5^dCL+%Lyk^u_yaC9glbS zxWLD|`_1;fTY3Sw5S7BMMPq8SKBxZ09;q!Tpd~DMJSo*T-{As;*CU)P{G)!dFyYlbtLLS=B%d+4u6^m*cz5EvwzZNJE` !processOnly.has(argument), + filterNodeArgumentsForWorkerThreads: argument => !processOnly.has(argument), }; diff --git a/test/node-arguments/snapshots/test.js.md b/test/node-arguments/snapshots/test.js.md index 05b21577f..9fdb63532 100644 --- a/test/node-arguments/snapshots/test.js.md +++ b/test/node-arguments/snapshots/test.js.md @@ -15,7 +15,7 @@ Generated by [AVA](https://avajs.dev). }, ] -## `threadArgumentsFilter` configuration filters arguments for worker thread +## `filterNodeArgumentsForWorkerThreads` configuration filters arguments for worker thread > tests pass @@ -26,7 +26,7 @@ Generated by [AVA](https://avajs.dev). }, ] -## `threadArgumentsFilter` configuration ignored for worker process +## `filterNodeArgumentsForWorkerThreads` configuration ignored for worker process > tests pass @@ -53,3 +53,25 @@ Generated by [AVA](https://avajs.dev). title: 'works', }, ] + +## `threadArgumentsFilter` configuration filters arguments for worker thread + +> tests pass + + [ + { + file: 'thread.js', + title: 'exec arguments filtered', + }, + ] + +## `threadArgumentsFilter` configuration ignored for worker process + +> tests pass + + [ + { + file: 'process.js', + title: 'exec arguments unfiltered', + }, + ] diff --git a/test/node-arguments/snapshots/test.js.snap b/test/node-arguments/snapshots/test.js.snap index 853b95b6d4797918705beee6842fd07700837d76..bbccccf41e85c97ad9c54ef26ccfe632b0cbd8b0 100644 GIT binary patch literal 583 zcmV-N0=WG_RzVuY%w;co4L^*^o;(~S(M)~e{8*W1R;Vo=E0f46faDaP# z>>P$5&k&ncx7!8W2SmU-h|ij!b_M`50QlMj?!6gJNJVTdmc3pN@Gf8runPy+x`%DR zP9JZi#!|ECm#UM6>cNCp{_a#oHoM1kN@}S{9_l+4be+j3(OUR|L+^6Nj zXeQ&o%O8sf00000000B6l(CM}KoEv!6MVoSlthPwD3C^?1gD^;;Dm%kfoLhS*&REZ ztaq)QHMx^$uHXrvqv9G~1;K0ZAjokX+rX1|(>bXqdYk2NI0SJYL=VIO&P4Bl=z~Z` z;#OuXwVP3`I^C!q&$;t;vMRFOJ*5j$TSXeDY^8}YP2(*1WvZzRq+~SSk(E=j^GF+5kLoyMtD3$~JH)Ergyf$|}ciuQ8c)DkjjEaTF|85YDWbJ~oa+6dg8JX!F?+6}t} l{XAN)zaQA^$2ZBPwd}N!y|4XA?P%U<^&iq7$0(Zx002ZA3WWdw diff --git a/test/node-arguments/test.js b/test/node-arguments/test.js index 417b98aef..92134a0f7 100644 --- a/test/node-arguments/test.js +++ b/test/node-arguments/test.js @@ -13,7 +13,7 @@ test('passed node arguments to workers', async t => { t.snapshot(result.stats.passed, 'tests pass'); }); -test('`threadArgumentsFilter` configuration filters arguments for worker thread', async t => { +test('`filterNodeArgumentsForWorkerThreads` configuration filters arguments for worker thread', async t => { const options = { cwd: cwd('thread-arguments-filter'), }; @@ -23,7 +23,7 @@ test('`threadArgumentsFilter` configuration filters arguments for worker thread' t.snapshot(result.stats.passed, 'tests pass'); }); -test('`threadArgumentsFilter` configuration ignored for worker process', async t => { +test('`filterNodeArgumentsForWorkerThreads` configuration ignored for worker process', async t => { const options = { cwd: cwd('thread-arguments-filter'), }; From fbd9d336d1b0039e5ef1c696dd6937c1e4c3d77d Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Tue, 20 Aug 2024 15:44:28 +0200 Subject: [PATCH 13/13] Restore default assignment --- lib/fork.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fork.js b/lib/fork.js index b2b2405e6..8317774ff 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -60,7 +60,7 @@ const createWorker = (options, execArgv) => { }; }; -export default function loadFork(file, options, execArgv) { +export default function loadFork(file, options, execArgv = process.execArgv) { let finished = false; const emitter = new Emittery();