Skip to content

Commit

Permalink
wp-env: Set core source to latest when null (#43133)
Browse files Browse the repository at this point in the history
* Set core source to latest if config.core is null to fix a crash

* Cache WordPress version to avoid extra network requests
  • Loading branch information
noahtallen authored Aug 16, 2022
1 parent 92846ed commit 4dbc1a6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
7 changes: 5 additions & 2 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
44 changes: 14 additions & 30 deletions packages/env/lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' ) ||
Expand All @@ -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;
Expand Down
20 changes: 18 additions & 2 deletions packages/env/lib/config/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const os = require( 'os' );
* Internal dependencies
*/
const { ValidationError } = require( './validate-config' );
const { getLatestWordPressVersion } = require( '../wordpress' );

/**
* @typedef {import('./config').WPServiceConfig} WPServiceConfig
Expand All @@ -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 ) =>
Expand All @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 4dbc1a6

Please sign in to comment.