Make WordPress Core

Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#65716 closed task (blessed) (fixed)

Local environment startup can load .env before its initial copy completes

Reported by: lancewillett Owned by: lancewillett
Priority: normal Milestone: 7.1
Component: Build/Test Tools Version:
Severity: normal Keywords: has-patch commit
Cc: Focuses:

Description

On a fresh checkout, npm run env:start copies .env.example using asynchronous copyFile(). It then calls synchronous dotenv.config() immediately.

The read can win the race, leaving configuration values unset for that Node process.

This is a latent correctness bug on trunk. On 6.9, it can break PHP 7.2 or 7.3 with MySQL 8.4.

When LOCAL_DB_VERSION is missing, the compatibility helper selects --default-authentication-plugin=mysql_native_password. MySQL 8.4 removed that option, so the database exits before installation.

The proposed fix uses copyFileSync() with COPYFILE_EXCL. Existing .env files remain untouched, and a new .env exists before dotenv.config() runs.

The change should land on trunk first, then be backported to 6.9. This relates to the MySQL 8.4 compatibility work in #61218 (https://core.trac.wordpress.org/ticket/61218).

Change History (8)

This ticket was mentioned in ​PR #12697 on ​WordPress/wordpress-develop by ​@lancewillett.


3 months ago
#1

  • Keywords has-patch added

## Summary

On a fresh checkout, start.js begins copying .env.example asynchronously, then immediately loads .env. The read can finish first, leaving configuration values unset for that process.

This changes the initial copy to copyFileSync(), ensuring .env exists before dotenv.config() runs. Existing .env files remain untouched because COPYFILE_EXCL is preserved.

## Branch behavior

The race is latent on trunk because the old-PHP authentication helper is no longer present.

On 6.9, missing LOCAL_DB_VERSION can make PHP 7.2 or 7.3 select --default-authentication-plugin=mysql_native_password. MySQL 8.4 removed this option and exits during startup.

This should be backported to 6.9 after landing on trunk.

## Testing

  • Confirmed a fresh trunk fixture copied and loaded .env before Docker startup.
  • Reproduced the 6.9 delayed-copy path and wrong authentication option.
  • Confirmed MySQL 8.4 rejects the removed option with exit code 1.
  • Confirmed the patched 6.9 environment started with PHP 7.2 and healthy MySQL 8.4.
  • Confirmed npm run env:install completed.

#2 @lancewillett
3 months ago

Please backport to 6.9: the race is latent on trunk but breaks PHP 7.2/7.3 startup on 6.9, which still defaults to MySQL 8.4 (verified fixed there).

​@lucasbustamante commented on ​PR #12697:


3 months ago
#3

Reviewing.

#4 @mukesh27
3 months ago

  • Keywords commit added
  • Type defect (bug) → task (blessed)

Marked this as ready for commit.

This won't affect any production code, so it can be committed at any time. I also setting the ticket type to task.

#5 @lancewillett
3 months ago

  • Owner set to lancewillett
  • Status new → accepted

@mukesh27 When a backport goes to older branch, which milestone should the Trac ticket carry?

#6 @lancewillett
3 months ago

In 62871:

Build/Test Tools: Copy local environment configuration synchronously.

On a fresh checkout, start.js copied .env.example asynchronously and
then immediately loaded .env, so the read could win the race and leave
configuration unset for that process. Copy synchronously, guarded by an
existsSync check, so .env exists before dotenv.config() runs and an
existing file is left untouched. Dropping the swallow-all callback also
lets a real copy error surface instead of being logged as "already
exists".

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/12697

Props jonsurrell, lucasbustamante, mukesh27, adrianmoldovanwp.
See #65716.

#7 @lancewillett
3 months ago

  • Resolution → fixed
  • Status accepted → closed

In 62872:

Build/Test Tools: Copy local environment configuration synchronously.

Merges [62871] to the 6.9 branch.

Props jonsurrell, lucasbustamante, mukesh27, adrianmoldovanwp.
Fixes #65716.

#8 @lancewillett
3 months ago

  • Milestone Awaiting Review → 7.1
Note: See TracTickets for help on using tickets.