setup-node icon indicating copy to clipboard operation
setup-node copied to clipboard

Enable caching by default if `packageManager` field is defined in package.json

Open trivikr opened this issue 1 year ago • 3 comments

Description: The action allows caching global packages data as per documentation in https://github.com/actions/setup-node#caching-global-packages-data

It's an amazing feature and reduces action execution time, but it has to be enabled in configuration. Caching is not enabled in default setup. Users may not discover caching feature by default, and miss on the faster executions times enabled by caching.

The feature request is to enable caching by default if packageManager field is defined. This can be enabled in a major version bump, say v4, to not affect existing users.

Justification: Just want all users to get benefit of faster executions times allowed by caching without explicitly opting in, if they have set up packageManager field in their package.json

They can always explicitly opt-out by setting cache to empty string.

Are you willing to submit a PR? Yes

trivikr avatar Jan 31 '23 15:01 trivikr

I think the implementation can be as simple as

-    if (cache && isCacheFeatureAvailable()) {
+    const packageManagerFromManifest = getNameFromPackageManagerField();
+    if ((cache !== "" || packageManagerFromManifest) && isCacheFeatureAvailable()) {
       const cacheDependencyPath = core.getInput('cache-dependency-path');
-      await restoreCache(cache, cacheDependencyPath);
+      const packageManager = cache !== "" ? cache : packageManagerFromManifest;
+      await restoreCache(packageManager, cacheDependencyPath);
     }

where getNameFromPackageManagerField returns the package manager name from packageManager field. That is:

Other package managers can be added, once they are supported

trivikr avatar Jan 31 '23 16:01 trivikr

Hello @trivikr ! Thank you for the suggested idea! We will consider adding this feature and will let you know as soon as we have any decision.

MaksimZhukov avatar Feb 01 '23 08:02 MaksimZhukov

This is what I doing before this feature landed:

jobs:
  steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Detect package manager
      shell: bash
      run: node -p "'PACKAGE_MANAGER='+require('./package.json').packageManager?.split?.('@')?.[0]" >> "$GITHUB_ENV"

    - name: Install pnpm
      run: corepack enable pnpm
      if: ${{ env.PACKAGE_MANAGER == 'pnpm' }}
      # install pnpm before setup-node

    - name: Setup node
      uses: actions/setup-node@v3
      with:
        node-version: lts
        cache: ${{ env.PACKAGE_MANAGER }}

Airkro avatar Sep 16 '23 09:09 Airkro