Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yarn workspaces: packages found in node_modules are not excluded when using list/fix-mismatches #68

Closed
ohana54 opened this issue Feb 23, 2022 · 8 comments

Comments

@ohana54
Copy link

ohana54 commented Feb 23, 2022

Description

Hi,
We're using Yarn Workspaces with the glob pattern packages/**, since we have a deep package tree.
Some of the packages have their own node_modules folder with packages inside.
Yarn is ok with it and automatically doesn't search for packages in node_modules.

However, syncpack has its own resolving mechanism and does include packages from node_modules.

Suggested Solution

Automatically exclude node_modules when searching for packages.

Is there a way maybe for a user to exclude it via config? I couldn't find one.

Thanks!!

@tom-fletcher
Copy link
Contributor

I ran into the same issue with pnpm as my pnpm-workspace.yaml has similar glob patterns such as:

packages:
  - 'apps/**'

As a workaround, you can create a .syncpackrc.js in your project root and perform your own package.json resolution.

For pnpm:

const fs = require('fs')
const glob = require('glob')
const yaml = require('yaml')

let source = ['package.json'] // Include the root package.json

const pnpmWorkspaceFile = fs.readFileSync('pnpm-workspace.yaml', 'utf8')
const workspaces = yaml.parse(pnpmWorkspaceFile)

workspaces.packages.map((workspacePackage) => {
	source = source.concat(
		glob.sync(`${workspacePackage}/package.json`, {
			ignore: `**/node_modules/**`,
		})
	)
})

module.exports = {
	source,
}

And similar for yarn:

const glob = require('glob')

let source = ['package.json'] // Include the root package.json

const packageJson = require('./package.json')
const workspaces = packageJson.workspaces

workspaces.map((workspacePackage) => {
	source = source.concat(
		glob.sync(`${workspacePackage}/package.json`, {
			ignore: `**/node_modules/**`,
		})
	)
})

module.exports = {
	source,
}

However, it would be much nicer if node_modules were always automatically excluded so I will also submit a PR for this.

@JamieMason
Copy link
Owner

Thanks @ohana54 and @tom-fletcher – happy to do this but something I want to check, do you have packages you develop which are nested within other packages you develop? So rather than

- apps
  - app1
  - app2

You potentially have something like this?

- apps
  - app1
  - app2
  - app2/packages/app3

@tom-fletcher
Copy link
Contributor

tom-fletcher commented Mar 24, 2022

It is pattern I do use @JamieMason, although the structure tends to look more like this:

- apps
     - app1
          - backend
          - frontend
               - android
               - ios
               - web
     - app2
          - ...

Edit to add: So, app1 might have a package.json with some tooling, scripts etc specific to that app, and then backend, android, ios, web would have their own package.json beneath that.

@ohana54
Copy link
Author

ohana54 commented Mar 24, 2022

@JamieMason I've seen cases with nesting in my company (app2/packages/app3) although it wasn't in the repo that caused me to open this issue.
I think this type of nesting can still be supported, just need to ignore node_modules in the path.

@tom-fletcher
Copy link
Contributor

@ohana54 - yes, the nesting app2/packages/app3 would be supported after #70 if anyone did want to use that particular pattern.

As Syncpack is just globbing apps/**/package.json it doesn't care what the underlying structure is, which is ideal (except for the node_modules folders as raised in this issue).

@tom-fletcher
Copy link
Contributor

@JamieMason Wondering if it might be possible to merge and release #70? It would be nice to remove the workaround (and associated dependencies) from my monorepo root. Cheers!

@JamieMason
Copy link
Owner

Released in 6.2.1, thanks a lot @tom-fletcher

@tom-fletcher
Copy link
Contributor

Thank you @JamieMason - and thanks for this package, it is great for enforcing a single version policy. 👏

Upgraded to 6.2.1 and verified working. 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants