or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configurations.mddependency-checks.mdindex.mdmodule-boundaries.mdplugin-validation.mdworkspace-rules.md

module-boundaries.mddocs/

0

# Module Boundary Enforcement

1

2

The `enforce-module-boundaries` rule is the cornerstone of Nx workspace integrity, ensuring proper separation of concerns and preventing architectural violations through comprehensive dependency analysis.

3

4

## Capabilities

5

6

### Enforce Module Boundaries Rule

7

8

Validates import statements against configured dependency constraints, checking for proper module boundaries, circular dependencies, and tagging compliance.

9

10

```typescript { .api }

11

/**

12

* ESLint rule that enforces module boundaries within Nx workspaces

13

* Rule name: "@nx/enforce-module-boundaries"

14

*/

15

interface EnforceModuleBoundariesRule {

16

name: "enforce-module-boundaries";

17

meta: {

18

type: "suggestion";

19

docs: {

20

description: "Ensure that module boundaries are respected within the monorepo";

21

};

22

fixable: "code";

23

schema: [EnforceModuleBoundariesOptions];

24

messages: Record<MessageIds, string>;

25

};

26

defaultOptions: [EnforceModuleBoundariesOptions];

27

create: (context: RuleContext) => RuleListener;

28

}

29

```

30

31

### Configuration Options

32

33

Complete configuration interface for the enforce-module-boundaries rule.

34

35

```typescript { .api }

36

interface EnforceModuleBoundariesOptions {

37

/** Array of allowed import patterns that bypass all boundary checks */

38

allow: string[];

39

40

/** Build target names used to determine buildable libraries */

41

buildTargets: string[];

42

43

/** Dependency constraints that define allowed relationships between projects */

44

depConstraints: DepConstraint[];

45

46

/** Enforce that libraries can only depend on other buildable libraries */

47

enforceBuildableLibDependency: boolean;

48

49

/** Allow projects to import from themselves (circular self-dependency) */

50

allowCircularSelfDependency: boolean;

51

52

/** Array of [source, target] project pairs to ignore for circular dependency checks */

53

ignoredCircularDependencies: Array<[string, string]>;

54

55

/** Project names exempt from dynamic dependency checks */

56

checkDynamicDependenciesExceptions: string[];

57

58

/** Prevent imports of transitive dependencies not declared in package.json */

59

banTransitiveDependencies: boolean;

60

61

/** Check for nested external imports beyond top-level package imports */

62

checkNestedExternalImports: boolean;

63

}

64

```

65

66

### Dependency Constraints

67

68

Configuration for tag-based dependency rules and external import restrictions.

69

70

```typescript { .api }

71

interface DepConstraint {

72

/** Source tag that this constraint applies to */

73

sourceTag: string;

74

75

/** If specified, projects with sourceTag can only depend on libraries with these tags */

76

onlyDependOnLibsWithTags?: string[];

77

78

/** If specified, projects with sourceTag cannot depend on libraries with these tags */

79

notDependOnLibsWithTags?: string[];

80

81

/** External imports that are banned for projects with this sourceTag */

82

bannedExternalImports?: string[];

83

84

/** Wildcard patterns for banned external imports */

85

bannedExternalImportsPatterns?: string[];

86

87

/** Allow specific external imports despite being in bannedExternalImports */

88

allowedExternalImports?: string[];

89

}

90

91

/** Union type for complex constraint combinations */

92

type ComboDepConstraint = {

93

allOf: DepConstraint[];

94

} | {

95

anyOf: DepConstraint[];

96

};

97

```

98

99

### Default Configuration

100

101

The rule comes with sensible defaults for most Nx workspaces.

102

103

```typescript { .api }

104

const DEFAULT_OPTIONS: EnforceModuleBoundariesOptions = {

105

allow: [],

106

buildTargets: ['build'],

107

depConstraints: [],

108

enforceBuildableLibDependency: false,

109

allowCircularSelfDependency: false,

110

checkDynamicDependenciesExceptions: [],

111

ignoredCircularDependencies: [],

112

banTransitiveDependencies: false,

113

checkNestedExternalImports: false

114

};

115

```

116

117

### Error Messages

118

119

All possible violation messages returned by the rule.

120

121

```typescript { .api }

122

type MessageIds =

123

| "noRelativeOrAbsoluteImportsAcrossLibraries"

124

| "noRelativeOrAbsoluteExternals"

125

| "noSelfCircularDependencies"

126

| "noCircularDependencies"

127

| "noImportsOfApps"

128

| "noImportsOfE2e"

129

| "noImportOfNonBuildableLibraries"

130

| "noImportsOfLazyLoadedLibraries"

131

| "projectWithoutTagsCannotHaveDependencies"

132

| "bannedExternalImportsViolation"

133

| "nestedBannedExternalImportsViolation"

134

| "noTransitiveDependencies"

135

| "onlyTagsConstraintViolation"

136

| "emptyOnlyTagsConstraintViolation"

137

| "notTagsConstraintViolation";

138

```

139

140

**Usage Examples:**

141

142

```typescript

143

// Basic usage in ESLint config

144

{

145

rules: {

146

"@nx/enforce-module-boundaries": [

147

"error",

148

{

149

depConstraints: [

150

{

151

sourceTag: "scope:shared",

152

onlyDependOnLibsWithTags: ["scope:shared"]

153

},

154

{

155

sourceTag: "type:feature",

156

onlyDependOnLibsWithTags: ["type:ui", "type:data-access", "type:util"]

157

}

158

],

159

enforceBuildableLibDependency: true,

160

allow: ["^@myorg/shared-.*"]

161

}

162

]

163

}

164

}

165

166

// Complex constraints with banned imports

167

{

168

rules: {

169

"@nx/enforce-module-boundaries": [

170

"error",

171

{

172

depConstraints: [

173

{

174

sourceTag: "platform:web",

175

bannedExternalImports: ["react-native", "@react-native/*"],

176

onlyDependOnLibsWithTags: ["platform:web", "platform:shared"]

177

},

178

{

179

sourceTag: "scope:admin",

180

notDependOnLibsWithTags: ["scope:customer"],

181

bannedExternalImports: ["lodash"]

182

}

183

],

184

banTransitiveDependencies: true,

185

checkNestedExternalImports: true

186

}

187

]

188

}

189

}

190

191

// Ignoring specific circular dependencies

192

{

193

rules: {

194

"@nx/enforce-module-boundaries": [

195

"error",

196

{

197

allowCircularSelfDependency: true,

198

ignoredCircularDependencies: [

199

["project-a", "project-b"],

200

["legacy-lib", "legacy-utils"]

201

]

202

}

203

]

204

}

205

}

206

```

207

208

### Validation Behaviors

209

210

The rule performs several types of validation:

211

212

1. **Import Path Validation**: Ensures imports follow proper patterns (no relative imports across project boundaries)

213

2. **Tag Constraint Validation**: Enforces tag-based dependency rules

214

3. **Circular Dependency Detection**: Identifies and prevents circular dependencies

215

4. **Buildable Library Enforcement**: Ensures non-buildable libraries don't import buildable ones

216

5. **External Import Control**: Manages allowed/banned external package imports

217

6. **Transitive Dependency Checks**: Validates against importing undeclared transitive dependencies

218

7. **App Import Prevention**: Prevents libraries from importing application code

219

8. **Dynamic Import Validation**: Checks dynamic imports for compliance

220

221

### Integration with Nx Project Graph

222

223

The rule integrates deeply with Nx's project graph system:

224

225

- Reads project configurations and dependency relationships

226

- Analyzes build targets to determine buildable libraries

227

- Uses project tags for constraint matching

228

- Leverages workspace layout for path resolution

229

- Caches project graph data for performance