Am I meant to commit yarn's `.pnp.js` file?

Viewed 5428

Yarn includes an optional "Plug'n'Play" feature which moves node_modules out of the project directory. In doing so it creates a .pnp.js file with references to various dependency paths on the hard drive.

The file is auto-generated, is several thousand lines long, and appears to reference paths specific to the machine that ran yarn install.

Is .pnp.js meant to be commited with the rest of the code? I can't seem to find any information about this despite the file appearing to be useless to commit. What is conisdered best practice and why?

2 Answers

I found this comment by Arcanis (the lead maintainer of Yarn) on an issue asking about what to include in a .gitignore. It may answer your question:

  • .yarn/plugins and .yarn/releases contain the Yarn releases used in the current repository (as defined by yarn set version). You will want to keep them versioned (this prevents potential issues if, say, two engineers use different Yarn versions with different features).

  • .yarn/unplugged should likely always be ignored, since it may contain native builds

  • .yarn/build-state.yml should likely be ignored as well, as it contains the build infos

    • If for some reason you version unplugged, it may make sense to keep build-state as well
  • .yarn/cache may be ignored, but you'll need to run yarn install to regenerate it

    • Versioning it unlocks what we call Zero-Installs - it's optional, though
  • .pnp.js (and potentially .pnp.data.json) are in the same boat as the cache. Add it to your repository if you keep your cache in your repository, and ignore it otherwise.

  • yarn.lock should always be stored within your repository (even if you develop a library)

So to summarize:

If you're using Zero-Installs:

.yarn/unplugged
.yarn/build-state.yml

If you're not using Zero-Installs:

.yarn/*
!.yarn/releases
!.yarn/plugins
.pnp.*

As one of the commenters noted, the official .gitignore recommendations are here, along with excellent commentary. The only critique I have is in the way they showed it there, I think they should have combined the common ways into one, and then separated the special cases for zero-install vs not. Here then is how I have it. But again, note that article, as some of these (e.g. .yarn/sdk) are optionally added or not.

# ------- yarn -------
# see excellent notes at: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
# Also: `yarn.lock` and `.yarnrc.yml` (or it's older counterpart .yarnrc) "should always be stored in your repo"
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

## --> ADD if using zero-install, otherwise do NOT:
#!.yarn/cache

## --> ELSE ADD if NOT using yarn's zero-install:
.pnp.*

# ------- end yarn -------
Related