Heroku 不为 Yarn v3 使用缓存

Heroku not using cache for Yarn v3

尝试让 Heroku 使用工作区为 Yarn v3 使用缓存模块。我有以下内容:

package.json:

"engines": {
    "node": "16.x",
    "yarn": "3.x"
  },
  "cacheDirectories": [
    "node_modules",
    "packages/components/node_modules",
    "packages/lib/node_modules",
    "packages/schema/node_modules",
    "packages/web/node_modules",
    "packages/web/.next/cache"
  ]

heroku-buildpack-features:

cache-native-yarn-cache=true

Heroku 输出:

-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       USE_YARN_CACHE=true
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
        !     You don't need to specify Yarn engine. Heroku will install the latest Yarn 1.x, so that per project version can be used. More information here: https://yarnpkg.com/getting-started/install#global-install
              https://devcenter.heroku.com/articles/nodejs-support
       
       engines.node (package.json):  16.x
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.7.0...
       Using default npm version: 7.20.3
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.11)
       Using yarn 3.0.0

-----> Installing binaries
        !     You don't need to specify Yarn engine. Heroku will install the latest Yarn 1.x, so that per project version can be used. More information here: https://yarnpkg.com/getting-started/install#global-install
              https://devcenter.heroku.com/articles/nodejs-support
       
       engines.node (package.json):  16.x
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.7.0...
       Using default npm version: 7.20.3
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.11)
       Using yarn 3.0.0
       
-----> Restoring cache
       Loading 6 from cacheDirectories (package.json):
       - node_modules
       - packages/components/node_modules (not cached - skipping)
       - packages/lib/node_modules
       - packages/schema/node_modules
       - packages/web/node_modules
       - packages/web/.next/cache

<a bunch of resolve stuff>

       ➤ YN0000: ┌ Fetch step
       ➤ YN0013: │ @apollo/client@npm:3.4.7 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @apollo/protobufjs@npm:1.2.2 can't be found in the cache and will be fetched from the remote registry

<and so on>

我试过在 package.json 中将 ".yarn/cache" 添加到 cacheDirectories,但它总是说它是空的。

不太确定从这里到哪里去。

截至 2021 年 9 月 2 日,Heroku 的 nodeJS 构建不完全支持 yarn 2。他们开始在 v173 中添加支持,但缓存尚未完全解决。

根本问题是 buildpack 没有正确检测到 $BUILD_DIR/.yarn/cache 存在并将环境变量 YARN_CACHE_FOLDER 设置为 .yarn/cache 以外的值。(请参阅 https://github.com/heroku/heroku-buildpack-nodejs/blob/752f1d5a139a800920cf5bb1bf70aad1a2954525/bin/compile#L140)

当 buildpack 到达缓存步骤时,.yarn/cache 目录实际上并不存在。

解决方法:

我可以通过在 config vars 中手动将 YARN_CACHE_FOLDER 设置为 .yarn/cache 来解决这个问题。您可以通过 运行 heroku config:set YARN_CACHE_FOLDER=.yarn/cache.

通过 CLI 执行此操作

确保您在 cacheDirectories 中也列出了 .yarn/cache

当构建日志显示时,您将知道它有效:

-----> Caching build
       Saving 1 cacheDirectories (package.json):
       - .yarn/cache

如果它不起作用,.yarn/cache 旁边会有一个 (nothing to cache) 注释。