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

未关闭
#21,305 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@riasvdv 已经在做这个了。

开始于 2026年12月2日。

评估

这个 Issue 还没有评估数据。

描述

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
主要语言
PHP
星标
3.6k
派生
800
平均合并
15 小时 20 分钟
30 天内合并 PR
715

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

craftcms/cms 的其他 Issue

查看 craftcms/cms 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。