checking yarn -v
without a doubt is the most robust way. Gitignore'd files are slightly unreliable in that they may live on through branch changes/etc. For example, I tried doing a yarn 2 upgrade a few weeks ago, but we have things in jenkins that will not be easy to upgrade, so we are still on yarn 1. Funny enough, I still have a .yarn/cache/.gitignore in my yarn 1 project folder. It's basically cruft I can get rid of, but I didn't know it was there.
This is my commit diff stat for my yarn 2 upgrade:
.gitignore | 8 ++++++++
.yarn/releases/yarn-berry.cjs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
.yarnrc.yml | 2 ++
The .cjs filename can change if you do something like yarn set version latest
, then we'll see this diff:
develop ➤ git status git:feature-yarn-v2-berry-upgrade-simple-approach*
On branch feature-yarn-v2-berry-upgrade-simple-approach
Your branch is up to date with 'origin/feature-yarn-v2-berry-upgrade-simple-approach'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: .yarn/releases/yarn-berry.cjs
modified: .yarnrc.yml
Untracked files:
(use "git add <file>..." to include in what will be committed)
.yarn/releases/yarn-2.3.3.cjs
no changes added to commit (use "git add" and/or "git commit -a")
develop ➤ git diff .yarnrc.yml git:feature-yarn-v2-berry-upgrade-simple-approach*
diff --git a/.yarnrc.yml b/.yarnrc.yml
index 0549c6b..40cfff9 100644
--- a/.yarnrc.yml
+++ b/.yarnrc.yml
@@ -1,2 +1,3 @@
-yarnPath: '.yarn/releases/yarn-berry.cjs'
nodeLinker: node-modules
+
+yarnPath: .yarn/releases/yarn-2.3.3.cjs
A file inside .yarn/releases containing 'yarn' and 'js' seems pretty ok, but this would be a bit naive (imo) since .yarnrc.yml actually has the path to the .cjs file. I think checking if a .yarnrc.yml file exists, that will be most robust.
I recall from some random issue I opened with yarn, that .yarnrc is for yarn1, and .yarnrc.yml is for yarn 2, and there's no overlap. Not all yarn 1 projects would have a .yarnrc, but all yarn 2 projects I think should have a .yarnrc.yml file... unless somehow there's a global config