From 4dbc1a6b104ea1b706b4dd5ef4607f2b553090a5 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Mon, 15 Aug 2022 17:51:28 -0700 Subject: [PATCH] wp-env: Set core source to latest when null (#43133) * Set core source to latest if config.core is null to fix a crash * Cache WordPress version to avoid extra network requests --- packages/env/CHANGELOG.md | 7 ++-- packages/env/lib/config/config.js | 44 ++++++++----------------- packages/env/lib/config/parse-config.js | 20 +++++++++-- packages/env/lib/wordpress.js | 7 ++++ 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/packages/env/CHANGELOG.md b/packages/env/CHANGELOG.md index 19005dfb172208..42afafa66fdec7 100644 --- a/packages/env/CHANGELOG.md +++ b/packages/env/CHANGELOG.md @@ -2,13 +2,16 @@ ## Unreleased +### Bug Fix +- Fix a crash when "core" was set to `null` in a `.wp-env.json` file. We now use the latest stable WordPress version in that case. This also restores the previous behavior of `"core": null` in `.wp-env.override.json`, which was to use the latest stable WordPress version. + ## 5.1.0 (2022-08-10) ### Enhancement -- Previously, wp-env used the WordPress version provided by Docker in the WordPress image for installations which don't specify a WordPress version. Now, wp-env will find the latest stable version on WordPress.org and check out the https://github.com/WordPress/WordPress repository at the tag matching that version. In most cases, this will match what Docker provides. The benefit is that wp-env (and WordPress.org) now controls the default WordPress version rather than Docker. +- Previously, wp-env used the WordPress version provided by Docker in the WordPress image for installations which don't specify a WordPress version. Now, wp-env will find the latest stable version on WordPress.org and check out the https://github.com/WordPress/WordPress repository at the tag matching that version. In most cases, this will match what Docker provides. The benefit is that wp-env (and WordPress.org) now controls the default WordPress version rather than Docker. ### Bug Fix -- Downloading a default WordPress version also resolves a bug where the wrong WordPress test files were used if no core source was specified in wp-env.json. The current trunk test files were downloaded rather than the stable version. Now, the test files will match the default stable version. +- Downloading a default WordPress version also resolves a bug where the wrong WordPress test files were used if no core source was specified in wp-env.json. The current trunk test files were downloaded rather than the stable version. Now, the test files will match the default stable version. ## 5.0.0 (2022-07-27) diff --git a/packages/env/lib/config/config.js b/packages/env/lib/config/config.js index ae3d0a4ed364ae..ca00aebacf4ba9 100644 --- a/packages/env/lib/config/config.js +++ b/packages/env/lib/config/config.js @@ -15,7 +15,6 @@ const readRawConfigFile = require( './read-raw-config-file' ); const parseConfig = require( './parse-config' ); const { includeTestsPath, parseSourceString } = parseConfig; const md5 = require( '../md5' ); -const { getLatestWordPressVersion } = require( '../wordpress' ); /** * wp-env configuration. @@ -85,21 +84,9 @@ module.exports = async function readConfig( configPath ) { const detectedLocalConfig = Object.keys( { ...baseConfig, ...overrideConfig } ).length > 0; - // If there is no local WordPress version, use the latest stable version from GitHub. - let coreRef; - if ( ! overrideConfig.core && ! baseConfig.core ) { - const wpVersion = await getLatestWordPressVersion(); - if ( ! wpVersion ) { - throw new ValidationError( - 'Could not find the latest WordPress version. There may be a network issue.' - ); - } - coreRef = `WordPress/WordPress#${ wpVersion }`; - } - // Default configuration which is overridden by .wp-env.json files. const defaultConfiguration = { - core: coreRef, + core: null, // Indicates that the latest stable version should ultimately be used. phpVersion: null, plugins: [], themes: [], @@ -155,23 +142,21 @@ module.exports = async function readConfig( configPath ) { // Merge each of the specified environment-level overrides. const allPorts = new Set(); // Keep track of unique ports for validation. - const env = allEnvs.reduce( ( result, environment ) => { - result[ environment ] = parseConfig( + const env = {}; + for ( const envName of allEnvs ) { + env[ envName ] = await parseConfig( validateConfig( mergeWpServiceConfigs( [ - ...getEnvConfig( defaultConfiguration, environment ), - ...getEnvConfig( baseConfig, environment ), - ...getEnvConfig( overrideConfig, environment ), + ...getEnvConfig( defaultConfiguration, envName ), + ...getEnvConfig( baseConfig, envName ), + ...getEnvConfig( overrideConfig, envName ), ] ), - environment + envName ), - { - workDirectoryPath, - } + { workDirectoryPath } ); - allPorts.add( result[ environment ].port ); - return result; - }, {} ); + allPorts.add( env[ envName ].port ); + } if ( allPorts.size !== allEnvs.length ) { throw new ValidationError( @@ -262,6 +247,7 @@ async function getDefaultBaseConfig( configPath ) { * @return {WPConfig} configuration object with overrides applied. */ function withOverrides( config ) { + const workDirectoryPath = config.workDirectoryPath; // Override port numbers with environment variables. config.env.development.port = getNumberFromEnvVariable( 'WP_ENV_PORT' ) || @@ -273,10 +259,8 @@ function withOverrides( config ) { // Override WordPress core with environment variable. if ( process.env.WP_ENV_CORE ) { const coreSource = includeTestsPath( - parseSourceString( process.env.WP_ENV_CORE, { - workDirectoryPath: config.workDirectoryPath, - } ), - { workDirectoryPath: config.workDirectoryPath } + parseSourceString( process.env.WP_ENV_CORE, { workDirectoryPath } ), + { workDirectoryPath } ); config.env.development.coreSource = coreSource; config.env.tests.coreSource = coreSource; diff --git a/packages/env/lib/config/parse-config.js b/packages/env/lib/config/parse-config.js index 0a36ceae0abc7b..9c7426e6601a69 100644 --- a/packages/env/lib/config/parse-config.js +++ b/packages/env/lib/config/parse-config.js @@ -9,6 +9,7 @@ const os = require( 'os' ); * Internal dependencies */ const { ValidationError } = require( './validate-config' ); +const { getLatestWordPressVersion } = require( '../wordpress' ); /** * @typedef {import('./config').WPServiceConfig} WPServiceConfig @@ -32,12 +33,12 @@ const HOME_PATH_PREFIX = `~${ path.sep }`; * @param {string} options.workDirectoryPath Path to the work directory located in ~/.wp-env. * @return {WPServiceConfig} Parsed environment-level configuration. */ -module.exports = function parseConfig( config, options ) { +module.exports = async function parseConfig( config, options ) { return { port: config.port, phpVersion: config.phpVersion, coreSource: includeTestsPath( - parseSourceString( config.core, options ), + await parseCoreSource( config.core, options ), options ), pluginSources: config.plugins.map( ( sourceString ) => @@ -58,6 +59,21 @@ module.exports = function parseConfig( config, options ) { }; }; +async function parseCoreSource( coreSource, options ) { + // An empty source means we should use the latest version of WordPress. + if ( ! coreSource ) { + const wpVersion = await getLatestWordPressVersion(); + if ( ! wpVersion ) { + throw new ValidationError( + 'Could not find the latest WordPress version. There may be a network issue.' + ); + } + + coreSource = `WordPress/WordPress#${ wpVersion }`; + } + return parseSourceString( coreSource, options ); +} + /** * Parses a source string into a source object. * diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index b1f54fb0e85d80..0d8f571cce71c1 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -277,13 +277,20 @@ async function readWordPressVersion( coreSource, spinner, debug ) { * * @return {string} The latest stable version of WordPress, like "6.0.1" */ +let CACHED_WP_VERSION; async function getLatestWordPressVersion() { + // Avoid extra network requests. + if ( CACHED_WP_VERSION ) { + return CACHED_WP_VERSION; + } + const versions = await got( 'https://api.wordpress.org/core/stable-check/1.0/' ).json(); for ( const [ version, status ] of Object.entries( versions ) ) { if ( status === 'latest' ) { + CACHED_WP_VERSION = version; return version; } }