From b879f6b4cee8bd3b7a0e09677444b186f7515442 Mon Sep 17 00:00:00 2001 From: Aditi Date: Sat, 24 May 2025 07:35:36 +0530 Subject: [PATCH] src: add an option to make compile cache portable Adds an option (NODE_COMPILE_CACHE_PORTABLE) for the built-in compile cache to encode the hashes with relative file paths. On enabling the option, the source directory along with cache directory can be bundled and moved, and the cache continues to work. When enabled, paths encoded in hash are relative to compile cache directory. --- doc/api/cli.md | 5 + doc/api/module.md | 23 ++++ doc/node.1 | 7 ++ lib/internal/modules/helpers.js | 25 ++++- src/compile_cache.cc | 52 ++++++++- src/compile_cache.h | 8 +- src/env.cc | 16 ++- src/env.h | 3 +- src/node_modules.cc | 8 +- src/path.cc | 46 ++++++++ src/path.h | 3 + test/parallel/test-compile-cache-api-error.js | 2 +- .../test-compile-cache-api-portable.js | 106 ++++++++++++++++++ .../test-compile-cache-portable-esm.js | 84 ++++++++++++++ test/parallel/test-compile-cache-portable.js | 75 +++++++++++++ 15 files changed, 448 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-compile-cache-api-portable.js create mode 100644 test/parallel/test-compile-cache-portable-esm.js create mode 100644 test/parallel/test-compile-cache-portable.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 403fd704c32658..331adfb6ae91ae 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3284,6 +3284,11 @@ added: v22.1.0 Enable the [module compile cache][] for the Node.js instance. See the documentation of [module compile cache][] for details. +### `NODE_COMPILE_CACHE_PORTABLE=1` + +When set to 1, the [module compile cache][] can be reused across different directory +locations as long as the module layout relative to the cache directory remains the same. + ### `NODE_DEBUG=module[,…]`