module: fix directory option in the enableCompileCache() API · nodejs/node@36da413 · GitHub
Skip to content

Commit 36da413

Browse files
joyeecheungtargos
authored andcommitted
module: fix directory option in the enableCompileCache() API
The option name should be `directory` to be consistent with the returned result. It should also allow environment variable overrides. This also adds documentation for the new options and improves it. PR-URL: #59931 Reviewed-By: Aditi Singh <aditisingh1400@gmail.com>
1 parent a7f7d10 commit 36da413

5 files changed

Lines changed: 186 additions & 62 deletions

File tree

doc/api/module.md

Lines changed: 49 additions & 35 deletions

lib/internal/modules/helpers.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,28 +405,32 @@ function stringify(body) {
405405
* Enable on-disk compiled cache for all user modules being compiled in the current Node.js instance
406406
* after this method is called.
407407
* This method accepts either:
408-
* - A string `cacheDir`: the path to the cache directory.
409-
* - An options object `{path?: string, portable?: boolean}`:
410-
* - `path`: A string path to the cache directory.
411-
* - `portable`: If `portable` is true, the cache directory will be considered relative. Defaults to false.
412-
* If cache path is undefined, it defaults to the NODE_MODULE_CACHE environment variable.
413-
* If `NODE_MODULE_CACHE` isn't set, it defaults to `path.join(os.tmpdir(), 'node-compile-cache')`.
414-
* @param {string | { path?: string, portable?: boolean } | undefined} options
408+
* - A string: path to the cache directory.
409+
* - An options object `{directory?: string, portable?: boolean}`:
410+
* - `directory`: A string path to the cache directory.
411+
* - `portable`: If `portable` is true, the cache directory will be considered relative.
412+
* Defaults to `NODE_COMPILE_CACHE_PORTABLE === '1'`.
413+
* If cache directory is undefined, it defaults to the `NODE_COMPILE_CACHE` environment variable.
414+
* If `NODE_COMPILE_CACHE` isn't set, it defaults to `path.join(os.tmpdir(), 'node-compile-cache')`.
415+
* @param {string | { directory?: string, portable?: boolean } | undefined} options
415416
* @returns {{status: number, message?: string, directory?: string}}
416417
*/
417418
function enableCompileCache(options) {
418-
let cacheDir;
419-
let portable = false;
419+
let portable;
420+
let directory;
420421

421422
if (typeof options === 'object' && options !== null) {
422-
({ path: cacheDir, portable = false } = options);
423+
({ directory, portable } = options);
423424
} else {
424-
cacheDir = options;
425+
directory = options;
425426
}
426-
if (cacheDir === undefined) {
427-
cacheDir = join(lazyTmpdir(), 'node-compile-cache');
427+
if (directory === undefined) {
428+
directory = process.env.NODE_COMPILE_CACHE || join(lazyTmpdir(), 'node-compile-cache');
428429
}
429-
const nativeResult = _enableCompileCache(cacheDir, portable);
430+
if (portable === undefined) {
431+
portable = process.env.NODE_COMPILE_CACHE_PORTABLE === '1';
432+
}
433+
const nativeResult = _enableCompileCache(directory, portable);
430434
const result = { status: nativeResult[0] };
431435
if (nativeResult[1]) {
432436
result.message = nativeResult[1];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const { enableCompileCache, getCompileCacheDir, constants } = require('module');
4+
5+
console.log('dir before enableCompileCache:', getCompileCacheDir());
6+
const options = JSON.parse(process.env.NODE_TEST_COMPILE_CACHE_OPTIONS);
7+
console.log('options:', options);
8+
const result = enableCompileCache(options);
9+
switch (result.status) {
10+
case constants.compileCacheStatus.FAILED:
11+
console.log('Compile cache failed. ' + result.message);
12+
break;
13+
case constants.compileCacheStatus.ENABLED:
14+
console.log('Compile cache enabled. ' + result.directory);
15+
break;
16+
case constants.compileCacheStatus.ALREADY_ENABLED:
17+
console.log('Compile cache already enabled.');
18+
break;
19+
case constants.compileCacheStatus.DISABLED:
20+
console.log('Compile cache already disabled.');
21+
break;
22+
}
23+
console.log('dir after enableCompileCache:', getCompileCacheDir());
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
// This tests module.enableCompileCache() with an options object still works with environment
4+
// variable NODE_COMPILE_CACHE overrides.
5+
6+
require('../common');
7+
const { spawnSyncAndAssert } = require('../common/child_process');
8+
const assert = require('assert');
9+
const fixtures = require('../common/fixtures');
10+
const tmpdir = require('../common/tmpdir');
11+
const fs = require('fs');
12+
const path = require('path');
13+
14+
const wrapper = fixtures.path('compile-cache-wrapper-options.js');
15+
tmpdir.refresh();
16+
17+
// Create a build directory and copy the entry point file.
18+
const buildDir = tmpdir.resolve('build');
19+
fs.mkdirSync(buildDir);
20+
const entryPoint = path.join(buildDir, 'empty.js');
21+
fs.copyFileSync(fixtures.path('empty.js'), entryPoint);
22+
23+
// Check that the portable option can be overridden by NODE_COMPILE_CACHE_PORTABLE.
24+
// We don't override NODE_COMPILE_CACHE because it will enable the cache before
25+
// the wrapper is loaded.
26+
spawnSyncAndAssert(
27+
process.execPath,
28+
['-r', wrapper, entryPoint],
29+
{
30+
env: {
31+
...process.env,
32+
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
33+
NODE_COMPILE_CACHE_PORTABLE: '1',
34+
NODE_TEST_COMPILE_CACHE_OPTIONS: JSON.stringify({ directory: 'build/.compile_cache' }),
35+
},
36+
cwd: tmpdir.path
37+
},
38+
{
39+
stdout(output) {
40+
console.log(output); // Logging for debugging.
41+
assert.match(output, /dir before enableCompileCache: undefined/);
42+
assert.match(output, /Compile cache enabled/);
43+
assert.match(output, /dir after enableCompileCache: .*build[/\\]\.compile_cache/);
44+
return true;
45+
},
46+
stderr(output) {
47+
console.log(output); // Logging for debugging.
48+
assert.match(output, /reading cache from .*build[/\\]\.compile_cache.* for CommonJS .*empty\.js/);
49+
assert.match(output, /empty\.js was not initialized, initializing the in-memory entry/);
50+
assert.match(output, /writing cache for .*empty\.js.*success/);
51+
return true;
52+
}
53+
});
54+
55+
assert(fs.existsSync(tmpdir.resolve('build/.compile_cache')));
56+
57+
const movedDir = buildDir + '_moved';
58+
fs.renameSync(buildDir, movedDir);
59+
const movedEntryPoint = path.join(movedDir, 'empty.js');
60+
61+
// When portable is undefined, it should use the env var NODE_COMPILE_CACHE_PORTABLE.
62+
spawnSyncAndAssert(
63+
process.execPath,
64+
['-r', wrapper, movedEntryPoint],
65+
{
66+
env: {
67+
...process.env,
68+
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
69+
NODE_COMPILE_CACHE_PORTABLE: '1',
70+
NODE_TEST_COMPILE_CACHE_OPTIONS: JSON.stringify({ directory: 'build_moved/.compile_cache' }),
71+
},
72+
cwd: tmpdir.path
73+
},
74+
{
75+
stdout(output) {
76+
console.log(output); // Logging for debugging.
77+
assert.match(output, /dir before enableCompileCache: undefined/);
78+
assert.match(output, /Compile cache enabled/);
79+
assert.match(output, /dir after enableCompileCache: .*build_moved[/\\]\.compile_cache/);
80+
return true;
81+
},
82+
stderr(output) {
83+
console.log(output); // Logging for debugging.
84+
assert.match(output, /reading cache from .*build_moved[/\\]\.compile_cache.* for CommonJS .*empty\.js/);
85+
assert.match(output, /cache for .*empty\.js was accepted, keeping the in-memory entry/);
86+
assert.match(output, /.*skip .*empty\.js because cache was the same/);
87+
return true;
88+
}
89+
});

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

Lines changed: 7 additions & 13 deletions

0 commit comments

Comments
 (0)