Skip to content

Commit 2e4e1d2

Browse files
codebytereaduh95
authored andcommitted
module: disable the compile cache while V8 coverage is collected
V8 cannot collect block coverage for functions that are deserialized from the code cache, so it only reports how many times each of them ran. With a warm compile cache, branches that never ran inside those functions are reported as covered, and tools that merge reports from several processes can report functions that ran as never run. The documentation recommended turning the cache off by hand. Do it automatically when coverage collection starts with the process, through NODE_V8_COVERAGE or --experimental-test-coverage. The compile cache tests stop propagating NODE_V8_COVERAGE to the processes they spawn, so that they keep working when the test suite itself runs with coverage. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #67120 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 1ef0527 commit 2e4e1d2

24 files changed

Lines changed: 109 additions & 26 deletions

‎doc/api/module.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,12 @@ no-op, and the directory is not created if it is missing.
414414
415415
### Limitations of the compile cache
416416
417-
Currently when using the compile cache with [V8 JavaScript code coverage][], the
418-
coverage being collected by V8 may be less precise in functions that are
419-
deserialized from the code cache. It's recommended to turn this off when
420-
running tests to generate precise coverage.
417+
V8 cannot collect block-level [V8 JavaScript code coverage][] for functions that
418+
are deserialized from the code cache. The compile cache is therefore disabled
419+
when coverage collection starts with the process, through
420+
[`NODE_V8_COVERAGE=dir`][] or [`--experimental-test-coverage`][]. Coverage that
421+
is started later, for example through the inspector, may be less precise in
422+
functions that were already deserialized from the code cache.
421423
422424
Compilation cache generated by one version of Node.js can not be reused by a different
423425
version of Node.js. Cache generated by different versions of Node.js will be stored
@@ -475,7 +477,8 @@ The following constants are returned as the `status` field in the object returne
475477
<td><code>DISABLED</code></td>
476478
<td>
477479
Node.js cannot enable the compile cache because the environment variable
478-
<code>NODE_DISABLE_COMPILE_CACHE=1</code> has been set.
480+
<code>NODE_DISABLE_COMPILE_CACHE=1</code> has been set, or because V8
481+
coverage is being collected.
479482
</td>
480483
</tr>
481484
</table>
@@ -1404,6 +1407,7 @@ returned object contains the following keys:
14041407
[V8 code cache]: https://v8.dev/blog/code-caching-for-devs
14051408
[`"exports"`]: packages.md#exports
14061409
[`--enable-source-maps`]: cli.md#--enable-source-maps
1410+
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
14071411
[`--import`]: cli.md#--importmodule
14081412
[`--require`]: cli.md#-r---require-module
14091413
[`NODE_COMPILE_CACHE=dir`]: cli.md#node_compile_cachedir

‎src/compile_cache.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct CompileCacheEntry {
4949
V(FAILED) /* Failed to enable the cache */ \
5050
V(ENABLED) /* Was not enabled before, and now enabled. */ \
5151
V(ALREADY_ENABLED) /* Was already enabled. */ \
52-
V(DISABLED) /* Has been disabled by NODE_DISABLE_COMPILE_CACHE. */
52+
V(DISABLED) /* Disabled by the environment or by V8 coverage. */
5353

5454
enum class CompileCacheEnableStatus : uint8_t {
5555
#define V(status) status,

‎src/env.cc‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,10 +1425,20 @@ CompileCacheEnableResult Environment::EnableCompileCache(
14251425
const std::string& key_root) {
14261426
CompileCacheEnableResult result;
14271427
std::string disable_env;
1428+
const char* disabled_reason = nullptr;
14281429
if (credentials::SafeGetenv(
14291430
"NODE_DISABLE_COMPILE_CACHE", &disable_env, this)) {
1431+
disabled_reason = "Disabled by NODE_DISABLE_COMPILE_CACHE";
1432+
#if HAVE_INSPECTOR
1433+
} else if (coverage_connection() != nullptr) {
1434+
// V8 cannot collect block coverage for functions deserialized from the
1435+
// cache.
1436+
disabled_reason = "Disabled because V8 coverage is being collected";
1437+
#endif // HAVE_INSPECTOR
1438+
}
1439+
if (disabled_reason != nullptr) {
14301440
result.status = CompileCacheEnableStatus::DISABLED;
1431-
result.message = "Disabled by NODE_DISABLE_COMPILE_CACHE";
1441+
result.message = disabled_reason;
14321442
Debug(this,
14331443
DebugCategory::COMPILE_CACHE,
14341444
"[compile cache] %s.\n",

‎test/common/compile-cache.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('./');
4+
const childProcess = require('./child_process');
5+
6+
// The compile cache is disabled while V8 coverage is being collected. In that
7+
// case, check that the first child says so and skip the rest of the test.
8+
function withCompileCache(spawn) {
9+
return (...args) => {
10+
if (!process.env.NODE_V8_COVERAGE) {
11+
return spawn(...args);
12+
}
13+
args.pop();
14+
childProcess.spawnSyncAndExit(...args, {
15+
stderr: /Disabled because V8 coverage is being collected/,
16+
});
17+
common.skip('the compile cache is disabled by V8 coverage');
18+
};
19+
}
20+
21+
module.exports = {
22+
spawnSyncAndAssert: withCompileCache(childProcess.spawnSyncAndAssert),
23+
spawnSyncAndExit: withCompileCache(childProcess.spawnSyncAndExit),
24+
};

‎test/parallel/test-compile-cache-api-env.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// with a NODE_COMPILE_CACHE environment variable override.
55

66
require('../common');
7-
const { spawnSyncAndAssert } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/compile-cache');
88
const assert = require('assert');
99
const fixtures = require('../common/fixtures');
1010
const tmpdir = require('../common/tmpdir');

‎test/parallel/test-compile-cache-api-flush.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This tests module.flushCompileCache() works as expected.
44

55
require('../common');
6-
const { spawnSyncAndAssert } = require('../common/child_process');
6+
const { spawnSyncAndAssert } = require('../common/compile-cache');
77
const assert = require('assert');
88
const tmpdir = require('../common/tmpdir');
99
const fixtures = require('../common/fixtures');

‎test/parallel/test-compile-cache-api-options-portable-env.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// variable NODE_COMPILE_CACHE overrides.
55

66
require('../common');
7-
const { spawnSyncAndAssert } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/compile-cache');
88
const assert = require('assert');
99
const fixtures = require('../common/fixtures');
1010
const tmpdir = require('../common/tmpdir');

‎test/parallel/test-compile-cache-api-portable.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// and supports portable paths across directory relocations.
55

66
require('../common');
7-
const { spawnSyncAndAssert } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/compile-cache');
88
const assert = require('assert');
99
const fs = require('fs');
1010
const tmpdir = require('../common/tmpdir');

‎test/parallel/test-compile-cache-api-readonly.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// entries are read, nothing is written, and a missing directory is not created.
55

66
const common = require('../common');
7-
const { spawnSyncAndAssert } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/compile-cache');
88
const assert = require('assert');
99
const fs = require('fs');
1010
const path = require('path');

‎test/parallel/test-compile-cache-api-success.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This tests module.enableCompileCache() and module.getCompileCacheDir() work.
44

55
require('../common');
6-
const { spawnSyncAndAssert } = require('../common/child_process');
6+
const { spawnSyncAndAssert } = require('../common/compile-cache');
77
const assert = require('assert');
88
const fixtures = require('../common/fixtures');
99
const tmpdir = require('../common/tmpdir');

0 commit comments

Comments
 (0)