[6.x]: Duplicate key in .env crashes the app when config is cached ("Constant … already defined")

Abierto
#21,305 1 comentario 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@riasvdv ya está trabajando en esto.

Desde el 2/12/2026.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

What happened?
Description

With a cached config (config:cache or optimize), Craft reloads .env through ConstAdapter, whose write() calls define() without checking defined(). A key that appears twice in .env then throws "Constant X already defined" and the app won't boot. Without a cached config, and in plain Laravel, duplicate keys are fine.

We hit this on a Laravel Cloud deploy, where optimize runs during the build:

Constant SESSION_DRIVER already defined, this will be an error in PHP 9
  at vendor/craftcms/cms/src/Config/ConstAdapter.php:54
Steps to reproduce
  1. Add a duplicate key to .env:
    REPRO_DUPLICATE=one
    REPRO_DUPLICATE=two
    
  2. Run php artisan config:cache.
  3. Run any Artisan command, such as php artisan about.
Expected behavior

The app boots and the last value wins, the same as without a cached config and the same as plain Laravel.

Actual behavior

Every Artisan command and request fails with Constant REPRO_DUPLICATE already defined, this will be an error in PHP 9 from ConstAdapter.php:54.

A possible fix is for ConstAdapter::write() to skip (or return false for) names that are already defined().

Craft CMS version

6.0.0-beta.3

PHP version

8.5.13

Operating system and version

macOS (Laravel Herd) locally; Laravel Cloud (production)

Database type and version

MySQL

Image driver and version

Imagick 3.8.1 (ImageMagick 7.1.2-34) on Laravel Cloud

Installed plugins and versions
  • craftcms/ckeditor 6.0.0-beta.2
Lenguaje dominante
PHP
Estrellas
3.6k
Forks
800
Merge medio
15 h 20 min
PR fusionados (30 d)
715

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de craftcms/cms

Todos los issues de craftcms/cms

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.