← Back to Baseline

Why it Matters

Magento uses dependency injection (DI) to manage classes and objects. If DI is not compiled in production, the system falls back to runtime resolution. This is slower, consumes more memory, and may expose error traces if class generation fails.

Compiling DI before deployment improves performance, reduces runtime errors, and ensures that no debug artifacts are created on the fly in the production server.

Verification Steps

Command line

# Check if generated/code directory has compiled classes
ls -l generated/code

# Run DI compile check
bin/magento setup:di:compile --dry-run

Expected

Generated code exists in generated/code/ and compilation passes without errors.

Remediation / Fix Guidance

  1. Before deploying to production, always run:
    bin/magento setup:di:compile
  2. Include DI compilation in CI/CD pipelines, so production servers never compile on the fly.
  3. Clear generated/ directory before compilation to avoid stale classes:
    rm -rf generated/code/* generated/metadata/*

Examples

Fail Example
# Production without DI compiled
generated/code/ is empty
Runtime tries to create classes (slow and risky)
Pass Example
# Production with DI compiled
generated/code/Magento/Catalog/...
# Classes pre-generated, system runs faster

References