From 94422e8a40bcb380d7f2633c4dde19839ba71d5e Mon Sep 17 00:00:00 2001 From: Aditi <62544124+Aditi-1400@users.noreply.github.com> Date: Fri, 12 Sep 2025 16:30:39 +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. PR-URL: https://github.com/nodejs/node/pull/58797 Fixes: https://github.com/nodejs/node/issues/58755 Refs: https://github.com/nodejs/node/issues/52696 Reviewed-By: Joyee Cheung Reviewed-By: James M Snell --- 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 d4bcbe9cace..9f9c8faa7d1 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3341,6 +3341,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[,…]`