changesets / action

709 stars 251 forks source link

Fails when using Yarn v2 + PnP #66

Open jdb8 opened 3 years ago

jdb8 commented 3 years ago

It looks like there's a line that explicitly references node_modules here: https://github.com/changesets/action/blob/master/src/run.ts#L186. When using Yarn PnP, this fails, since node_modules doesn't exist.

Error: Cannot find module '/home/runner/work/<pkg>/<pkg>/node_modules/@changesets/cli/package.json'
Require stack:
- /home/runner/work/_actions/changesets/action/master/dist/index.js
Require stack:
- /home/runner/work/_actions/changesets/action/master/dist/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at f (/home/runner/work/_actions/changesets/action/master/dist/index.js:1:293)
    at f (/home/runner/work/_actions/changesets/action/master/dist/index.js:1:233)
    at p (/home/runner/work/_actions/changesets/action/master/dist/index.js:1:544)
    at Object.w [as runVersion] (/home/runner/work/_actions/changesets/action/master/dist/index.js:906:3310)
    at async /home/runner/work/_actions/changesets/action/master/dist/index.js:908:1597 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/runner/work/_actions/changesets/action/master/dist/index.js'
  ]
}
Andarist commented 3 years ago

Does require.resolve work with PnP in a way that we could use the resolved path to read the package.json file using fs APIs?

merceyz commented 3 years ago

Yes, you use require.resolve and all other node methods just like you normally would

const path = require('path');

const cliPath = require.resolve('@changesets/cli/package.json', { paths: [cwd] });

const changesetsCliPkgJson = require(cliPath); // or JSON.parse(fs.read*(cliPath) )

await exec('node', [path.resolve(cliPath, '../cli/bin.js'), cmd], { cwd });

You should always use something like this and not make assumptions about the node_modules layout, then the user can set NODE_OPTIONS="-r <cwd>./.pnp.(c)js" as an env variable in their CI to enable PnP

Andarist commented 3 years ago

Would you like to prepare a PR for this?

jacobwgillespie commented 3 years ago

@Andarist I've opened a PR for this here: #67

I can confirm that using that PR + the following GitHub Actions config, it works for us using Yarn 2 + PnP:

- uses: jacobwgillespie/changesets-action@resolve-from
  with:
    publish: ...
  env:
    NODE_OPTIONS: -r /home/runner/work/repo/repo/.pnp.js
    ...
elpddev commented 3 years ago

This issue can be closed maybe? For me it works now with:

with:
  version: yarn changeset version