ANTI PATTERN FIX 2025 10 21
Documentation for Developers
Anti-Pattern Fix: Relative Imports Documentation Update
October 21, 2025
Status: ✅ COMPLETE
🎯 Problem Identified
The common-errors/index.md documentation was incorrectly suggesting using relative imports as a "fix" for library import errors. This was promoting an anti-pattern that causes:
- ❌ Build system bypassing
- ❌ Type checking issues
- ❌ Broken IDE features
- ❌ Maintenance nightmares
- ❌ Inconsistent codebase
🔧 What Was Fixed
1. Removed Bad Advice About Relative Imports
Before (WRONG):
// ✅ Fixed (THIS WAS WRONG!)
import { EditorUiComponent } from 'libs/editor-ui/src/lib';
After (CORRECT):
// ❌ Wrong - Never use relative paths
import { EditorUiComponent } from 'libs/editor-ui/src/lib';
// ✅ Correct - Always use NX aliases
import { EditorUiComponent } from '@editor-ui';
2. Added Prominent Warnings
Added critical warning at the top of the document:
⚠️ CRITICAL: Never use relative imports (e.g.,
libs/my-lib/src/...) as a workaround for import errors. Always fix the root cause by properly configuringtsconfig.base.json,ng-package.json,project.json, andtsconfig.lib.json.
3. Added Comprehensive Configuration Guide
Added new section: "How to Properly Fix Library Import Errors" with 5 steps:
- ✅ Verify
tsconfig.base.jsonpath aliases - ✅ Ensure
ng-package.jsonexists and is configured - ✅ Verify
src/index.tsexports public API - ✅ Check
project.jsonbuild configuration - ✅ Verify
tsconfig.lib.jsoncompiler options
4. Updated Prevention Guidelines
Before:
- Generic advice about using NX aliases
After:
- 🚨 Most Important Rules section with explicit warnings
- Clear hierarchy: Configuration fixes come FIRST
- Specific files to check and configure
5. Enhanced Quick Reference
Before:
- Import errors: Check tsconfig.json path mappings
After:
- Import errors: Fix configuration files (tsconfig.base.json, ng-package.json, project.json) - NEVER use relative imports
📝 Sections Updated
Updated Sections
Error:
Cannot find module '@editor-ui'- Changed from suggesting relative imports to configuration fixes
- Added configuration examples
Error:
Static imports of lazy-loaded libraries are forbidden- Changed from suggesting relative imports to configuration fixes
- Added proper configuration steps
Prevention Guidelines
- Restructured with priority hierarchy
- Added configuration checklist
Quick Reference
- Made configuration-first approach explicit
- Added "NEVER use relative imports" warnings
New Sections Added
How to Properly Fix Library Import Errors
- Complete 5-step configuration guide
- Code examples for each configuration file
- Proper import patterns
Related Documentation
- Links to library configuration guides
- Links to import/export patterns
- Links to NX library setup
✅ Key Messages Now Clear
DO ✅
- Always use NX library aliases (
@my-lib) - Fix configuration files when imports fail
- Follow the 5-step configuration guide
- Export through
src/index.ts - Configure
tsconfig.base.jsonpath aliases
DON'T ❌
- Never use relative imports (
libs/my-lib/src/...) - Don't bypass the build system
- Don't use workarounds instead of proper fixes
- Don't skip configuration files
📊 Impact
| Aspect | Before | After |
|---|---|---|
| Bad advice removed | 2 examples | 0 examples |
| Configuration guidance | Minimal | Comprehensive |
| Warnings about anti-patterns | None | Prominent |
| Step-by-step fixes | None | 5-step guide |
| Related documentation links | 0 | 4 links |
🎓 Why This Matters
The Problem with Relative Imports
When developers use relative imports like libs/my-lib/src/...:
- Bypasses Build System: The library's build process is skipped
- Type Issues: TypeScript declarations may not be generated correctly
- IDE Problems: Autocomplete and go-to-definition break
- Inconsistent: Different import styles across codebase
- Maintenance: Harder to refactor and reorganize code
The Correct Approach
When using NX aliases like @my-lib:
- ✅ Build System Works: Library is properly built and packaged
- ✅ Types Correct: TypeScript declarations are generated and used
- ✅ IDE Happy: Full IDE support with autocomplete
- ✅ Consistent: Same import style everywhere
- ✅ Maintainable: Easy to refactor and reorganize
📚 Related Documentation
This fix complements:
- public-api.ts → index.ts Migration
- Library Configuration Guide
- Import/Export Patterns
- NX Library Setup
✅ Verification
# No relative import suggestions in documentation
grep -n "libs/.*src" docs/english/public/development/common-errors/index.md
# Result: Only in "Wrong" examples marked with ❌
🎉 Summary
The documentation has been completely rewritten to:
- ❌ Remove bad advice about using relative imports
- ✅ Add proper guidance on configuration files
- ⚠️ Add prominent warnings against anti-patterns
- 📚 Add comprehensive guide with 5 configuration steps
- 🔗 Link to related docs for complete context
Developers will now be guided to the CORRECT solution (fix configuration) instead of the WRONG workaround (use relative imports).
Updated By: AI Assistant
Review Date: October 21, 2025
Status: ✅ Complete and verified