-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Remove circular dependencies #226
Conversation
FWIW: I like this a lot and inspired by this PR I also tried to integrate madge in one of my own projects. But apart from detected circular dependencies it lacks a lot of other useful dependency linting rules. So I discovered dependency-cruiser which seems to be more advanced than madge. |
@lo1tuma thank you, didn't know about dependency-cruiser
@colinhacks could you please share your view on whether you support this direction? |
@o-alexandrov This is the config I came up with: 'use strict';
module.exports = {
forbidden: [
{
name: 'no-circular',
severity: 'error',
from: {},
to: {
circular: true
}
},
{
name: 'no-orphans',
severity: 'error',
from: {
orphan: true,
pathNot: [
'\\.d\\.ts$',
'stryker.conf.js',
'prettier.config.js',
'dependency-cruiser.config.js',
'webpack.config.js',
'\\.test\\.(js|ts|jsx|tsx)$'
]
},
to: {}
},
{
name: 'no-non-package-json',
severity: 'error',
from: {},
to: {
dependencyTypes: ['npm-no-pkg', 'npm-unknown']
}
},
{
name: 'not-to-unresolvable',
severity: 'error',
from: {},
to: {
couldNotResolve: true
}
},
{
name: 'no-duplicate-dep-types',
severity: 'error',
from: {},
to: {
dependencyTypes: ['npm'],
moreThanOneDependencyType: true
}
},
{
name: 'not-to-spec',
severity: 'error',
from: {},
to: {
path: '\\.test\\.(js|ts|jsx|tsx)$'
}
},
{
name: 'not-to-dev-dep',
severity: 'error',
from: {
path: 'src/',
pathNot: ['\\.test\\.(js|ts|jsx|tsx)$']
},
to: {
dependencyTypes: ['npm-dev'],
moreThanOneDependencyType: false,
pathNot: ['node_modules/@types/', '.d.ts$']
}
}
],
options: {
doNotFollow: {
path: 'node_modules|build/',
dependencyTypes: ['npm', 'npm-dev', 'npm-optional', 'npm-peer', 'npm-bundled', 'npm-no-pkg']
},
exclude: {
path: 'build/'
},
moduleSystems: ['cjs', 'es6', 'tsd'],
tsPreCompilationDeps: true,
tsConfig: {
fileName: 'tsconfig.json'
},
preserveSymlinks: false,
combinedDependencies: true,
reporterOptions: {
dot: {
collapsePattern: 'node_modules/[^/]+'
}
}
}
}; I’m running this as an npm run-script via |
@colinhacks
|
Steps made:
|
This is great stuff! I tried to warn you about those issues here: #266 (comment)
There's no way to get rid of them without removing those methods, which I'm not willing to do. My plan is to fix this issues by controlling the loading order. Like you mentioned, this is a LOT easier now that there are only a few cycles to worry about. Can you explain how to reproduce #215 in detail? I'm trying to bundle Zod with webpack and run some test scripts but I can't reproduce. I'm not comfortable adding the ES module back into the npm package until I've confirmed that those issues are resolved. |
All the tests are failing for me. Were they working on your end?
EDIT: I got this working, see below. |
Okay I got this working again. I'm essentially using a simple version of the internal modules approach. The trick was to import all the circular dependencies from // BEFORE
import { ZodArray } from "../../array";
import { ZodNullable, ZodNullableType } from "../../nullable";
import { ZodOptional, ZodOptionalType } from "../../optional";
import { ZodTransformer } from "../../transformer";
// AFTER
import {
ZodArray,
ZodNullable,
ZodNullableType,
ZodOptional,
ZodOptionalType,
ZodTransformer,
} from "../../../index"; I also had to disable these two rules in eslintrc.json:
They were sorting all the imports by file name which was messing up the loading order every time I saved the file. |
I'm not sure why these issues started appearing, but they appear to be the same ones that were happening in #215. So hopefully this solution will solve the ESM issues as well. I just pushed an updated branch that contains my fixes: https://github.com/colinhacks/zod/tree/o-alexandrov-v2. The next step is to test this with Webpack, Node, and Rollup to see if it works. |
Thank you for resolving remaining issues. If anyone kindly could test this branch with their bundling tools, please do.
// package.json
"dependencies": {
"zod": "link:../path-to-cloned-zod"
}
|
Looks promising! Worked with snowpack 2.18.4 (which uses rollup I gather for cjs modules). |
@o-alexandrov I pushed an updated version to the "o-alexandrov-v2" branch of Zod. Play around with it and see if you discover any issues. I had to re-introduce several circular dependencies but it was necessary to get the tests to run correctly. |
It lets us not to guess where the circular dependencies are.
Instead, we could have a tool that reports it to us (pls see a screenshot below).
I saw your comment and wanted to help: