@@ -44,16 +44,23 @@ uint32_t GetHash(const char* data, size_t size) { |
44 | 44 | return crc32(crc, reinterpret_cast<const Bytef*>(data), size); |
45 | 45 | } |
46 | 46 |
|
47 | | -std::string GetCacheVersionTag() { |
| 47 | +std::string GetCacheVersionTag(EnableOption option) { |
| 48 | + std::string tag = std::string(NODE_VERSION) + '-' + std::string(NODE_ARCH) + |
| 49 | + '-' + Uint32ToHex(ScriptCompiler::CachedDataVersionTag()); |
| 50 | +#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS |
48 | 51 | // On platforms where uids are available, use different folders for |
49 | 52 | // different users to avoid cache miss due to permission incompatibility. |
50 | 53 | // On platforms where uids are not available, bare with the cache miss. |
51 | 54 | // This should be fine on Windows, as there local directories tend to be |
52 | 55 | // user-specific. |
53 | | - std::string tag = std::string(NODE_VERSION) + '-' + std::string(NODE_ARCH) + |
54 | | - '-' + Uint32ToHex(ScriptCompiler::CachedDataVersionTag()); |
55 | | -#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS |
56 | | - tag += '-' + std::to_string(getuid()); |
| 56 | + // A portable cache is meant to be reused wherever the same layout is |
| 57 | + // found, including by other users (e.g. a cache generated at build time |
| 58 | + // and shipped read-only with an application), so it is not split by uid: |
| 59 | + // a user who cannot write to it still reads it, and a failed write is |
| 60 | + // only a cache miss. |
| 61 | + if (option != EnableOption::PORTABLE) { |
| 62 | + tag += '-' + std::to_string(getuid()); |
| 63 | + } |
57 | 64 | #endif |
58 | 65 | return tag; |
59 | 66 | } |
@@ -532,11 +539,12 @@ CompileCacheHandler::CompileCacheHandler(Environment* env) |
532 | 539 | // Directory structure: |
533 | 540 | // - Compile cache directory (from NODE_COMPILE_CACHE) |
534 | 541 | // - $NODE_VERSION-$ARCH-$CACHE_DATA_VERSION_TAG-$UID |
| 542 | +// ($UID is omitted for a portable cache) |
535 | 543 | // - $FILENAME_AND_MODULE_TYPE_HASH.cache: a hash of filename + module type |
536 | 544 | CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env, |
537 | 545 | const std::string& dir, |
538 | 546 | EnableOption option) { |
539 | | - std::string cache_tag = GetCacheVersionTag(); |
| 547 | + std::string cache_tag = GetCacheVersionTag(option); |
540 | 548 | std::string absolute_cache_dir_base = PathResolve(env, {dir}); |
541 | 549 | std::string cache_dir_with_tag = |
542 | 550 | absolute_cache_dir_base + kPathSeparator + cache_tag; |
|