From 9d119d078b7ebb9fa553c5ed036e76099a389855 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 12:40:21 +0300 Subject: [PATCH 01/13] Disable XDebug by default unless specified by user --- packages/env/lib/init-config.js | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 28a17947b2ee4..24935900893b5 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -107,21 +107,29 @@ module.exports = async function initConfig( { * @return {string} The dockerfile contents. */ function dockerFileContents( image, config ) { - let shouldInstallXdebug = true; - // By default, an undefined phpVersion uses the version on the docker image, - // which is supported by Xdebug 3. - if ( config.env.development.phpVersion ) { - const versionTokens = config.env.development.phpVersion.split( '.' ); - const majorVersion = parseInt( versionTokens[ 0 ] ); - const minorVersion = parseInt( versionTokens[ 1 ] ); - if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { - throw new Error( 'Something went wrong parsing the php version.' ); - } - - // Disable Xdebug for PHP < 7.2. Xdebug 3 supports 7.2 and higher. - if ( majorVersion < 7 || ( majorVersion === 7 && minorVersion < 2 ) ) { - shouldInstallXdebug = false; + // Don't install XDebug unless it is explicitly required + let shouldInstallXdebug = false; + + if (config.xdebug !== 'off') { + // By default, an undefined phpVersion uses the version on the docker image, + // which is supported by Xdebug 3. + if ( config.env.development.phpVersion ) { + const versionTokens = config.env.development.phpVersion.split( '.' ); + const majorVersion = parseInt( versionTokens[ 0 ] ); + const minorVersion = parseInt( versionTokens[ 1 ] ); + + if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { + throw new Error( 'Something went wrong parsing the php version.' ); + } + + // Disable Xdebug for PHP < 7.2. Xdebug 3 supports 7.2 and higher. + if ( majorVersion > 7 && minorVersion >= 2 ) { + shouldInstallXdebug = true; + } else { + throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); + } } + } return `FROM ${ image } From d177888fb35b54b2df6ecdb091f5907620887c43 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 12:48:23 +0300 Subject: [PATCH 02/13] Install XDebug when user specifies but phpVersion is null --- packages/env/lib/init-config.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 24935900893b5..72a55aff048b7 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -111,8 +111,6 @@ function dockerFileContents( image, config ) { let shouldInstallXdebug = false; if (config.xdebug !== 'off') { - // By default, an undefined phpVersion uses the version on the docker image, - // which is supported by Xdebug 3. if ( config.env.development.phpVersion ) { const versionTokens = config.env.development.phpVersion.split( '.' ); const majorVersion = parseInt( versionTokens[ 0 ] ); @@ -128,6 +126,10 @@ function dockerFileContents( image, config ) { } else { throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); } + } else { + // By default, an undefined phpVersion uses the version on the docker image, + // which is supported by Xdebug 3. + shouldInstallXdebug = true; } } From ee14767fc040ccd8dc77315544355a0a820746a0 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 12:52:52 +0300 Subject: [PATCH 03/13] Use explnatory variable; clarify comments --- packages/env/lib/init-config.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 72a55aff048b7..d3006f3ab2d2e 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -119,9 +119,12 @@ function dockerFileContents( image, config ) { if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { throw new Error( 'Something went wrong parsing the php version.' ); } - - // Disable Xdebug for PHP < 7.2. Xdebug 3 supports 7.2 and higher. - if ( majorVersion > 7 && minorVersion >= 2 ) { + + // Xdebug 3 supports 7.2 and higher. + // Enable Xdebug for PHP >= 7.2. + const usingCompatiblePhpVersion = majorVersion > 7 && minorVersion >= 2; + + if usingCompatiblePhpVersion { shouldInstallXdebug = true; } else { throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); From 912e3e7ba495ae5cb505d5021506dde6412ab427 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 13:03:02 +0300 Subject: [PATCH 04/13] Fix syntax --- packages/env/lib/init-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index d3006f3ab2d2e..0087825cec14f 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -124,7 +124,7 @@ function dockerFileContents( image, config ) { // Enable Xdebug for PHP >= 7.2. const usingCompatiblePhpVersion = majorVersion > 7 && minorVersion >= 2; - if usingCompatiblePhpVersion { + if ( usingCompatiblePhpVersion ) { shouldInstallXdebug = true; } else { throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); From 056846261fa80bdab390bbeeed38008138393227 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 13:04:16 +0300 Subject: [PATCH 05/13] Lint --- packages/env/lib/init-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 0087825cec14f..1da35af78b7d2 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -110,7 +110,7 @@ function dockerFileContents( image, config ) { // Don't install XDebug unless it is explicitly required let shouldInstallXdebug = false; - if (config.xdebug !== 'off') { + if ( config.xdebug !== 'off' ) { if ( config.env.development.phpVersion ) { const versionTokens = config.env.development.phpVersion.split( '.' ); const majorVersion = parseInt( versionTokens[ 0 ] ); From 03df51d06300488d8646b1b4ebf8e5e0e35a545d Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 26 Aug 2021 13:56:52 +0300 Subject: [PATCH 06/13] Prettify --- packages/env/lib/init-config.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 1da35af78b7d2..ac892a16c5624 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -109,21 +109,21 @@ module.exports = async function initConfig( { function dockerFileContents( image, config ) { // Don't install XDebug unless it is explicitly required let shouldInstallXdebug = false; - + if ( config.xdebug !== 'off' ) { if ( config.env.development.phpVersion ) { const versionTokens = config.env.development.phpVersion.split( '.' ); const majorVersion = parseInt( versionTokens[ 0 ] ); const minorVersion = parseInt( versionTokens[ 1 ] ); - + if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { throw new Error( 'Something went wrong parsing the php version.' ); } - + // Xdebug 3 supports 7.2 and higher. // Enable Xdebug for PHP >= 7.2. const usingCompatiblePhpVersion = majorVersion > 7 && minorVersion >= 2; - + if ( usingCompatiblePhpVersion ) { shouldInstallXdebug = true; } else { @@ -134,7 +134,7 @@ function dockerFileContents( image, config ) { // which is supported by Xdebug 3. shouldInstallXdebug = true; } - + } return `FROM ${ image } From 73169f2a801fd4ebef2f3b7642ee97b199135c27 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 28 Oct 2021 12:33:49 +0300 Subject: [PATCH 07/13] Simplify logic checking PHP version for XDebug --- packages/env/lib/init-config.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index ac892a16c5624..0693e8f4e1e08 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -117,24 +117,21 @@ function dockerFileContents( image, config ) { const minorVersion = parseInt( versionTokens[ 1 ] ); if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { - throw new Error( 'Something went wrong parsing the php version.' ); + throw new Error( 'Something went wrong when parsing the PHP version.' ); } - // Xdebug 3 supports 7.2 and higher. - // Enable Xdebug for PHP >= 7.2. - const usingCompatiblePhpVersion = majorVersion > 7 && minorVersion >= 2; + // Xdebug 3 supports 7.2 and higher + // Ensure user has specified a compatible PHP version + const usingInompatiblePhpVersion = majorVersion < 7 || ( majorVersion === 7 && minorVersion < 2 ); - if ( usingCompatiblePhpVersion ) { - shouldInstallXdebug = true; - } else { + if ( usingInompatiblePhpVersion ) { throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); } - } else { - // By default, an undefined phpVersion uses the version on the docker image, - // which is supported by Xdebug 3. - shouldInstallXdebug = true; } + // By default, an undefined phpVersion uses the version on the docker image, + // which is supported by Xdebug 3. + shouldInstallXdebug = true; } return `FROM ${ image } From 3827e8d9510f2a9e853fc668bbe4715365821b34 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 28 Oct 2021 12:35:50 +0300 Subject: [PATCH 08/13] Add safety check before installing XDebug --- packages/env/lib/init-config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 0693e8f4e1e08..3c096f2a3673c 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -150,7 +150,8 @@ function installXdebug( enableXdebug ) { return ` # Install Xdebug: -RUN pecl install xdebug && docker-php-ext-enable xdebug +RUN if [ -z "$(pecl list | grep xdebug)" ] ; then pecl install xdebug ; fi +RUN docker-php-ext-enable xdebug RUN echo 'xdebug.start_with_request=yes' >> /usr/local/etc/php/php.ini RUN echo 'xdebug.mode=${ enableXdebug }' >> /usr/local/etc/php/php.ini RUN echo '${ clientDetectSettings }' >> /usr/local/etc/php/php.ini From 7cb3402ec92a2aec6d796aef86b720999dba3dce Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 29 Oct 2021 09:50:38 +0300 Subject: [PATCH 09/13] Lint --- packages/env/lib/init-config.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 3c096f2a3673c..2cbbcf890800c 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -112,19 +112,23 @@ function dockerFileContents( image, config ) { if ( config.xdebug !== 'off' ) { if ( config.env.development.phpVersion ) { - const versionTokens = config.env.development.phpVersion.split( '.' ); + const versionTokens = config.env.development.phpVersion.split( + '.' + ); const majorVersion = parseInt( versionTokens[ 0 ] ); const minorVersion = parseInt( versionTokens[ 1 ] ); if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { - throw new Error( 'Something went wrong when parsing the PHP version.' ); + throw new Error( + 'Something went wrong when parsing the PHP version.' + ); } // Xdebug 3 supports 7.2 and higher // Ensure user has specified a compatible PHP version - const usingInompatiblePhpVersion = majorVersion < 7 || ( majorVersion === 7 && minorVersion < 2 ); + const inompatiblePhp = majorVersion < 7 || ( majorVersion === 7 && minorVersion < 2 ); - if ( usingInompatiblePhpVersion ) { + if ( inompatiblePhp ) { throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); } } From 80d016a315960e14255224838f8df3793da64945 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 29 Oct 2021 09:55:59 +0300 Subject: [PATCH 10/13] Add Xdebug bug fix note --- packages/env/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/env/CHANGELOG.md b/packages/env/CHANGELOG.md index d9159569675cb..9a0fa8557a178 100644 --- a/packages/env/CHANGELOG.md +++ b/packages/env/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fix + +- Fix Xdebug installation code to ensure it would fail gracefully + ## 4.0.3 (2021-04-29) ### Bug Fix From 0ee3120cfcef898766bdda3cd63488ab779a1f9e Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 29 Oct 2021 10:34:55 +0300 Subject: [PATCH 11/13] Move PHP compatibility check to own function --- packages/env/lib/init-config.js | 65 +++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 2cbbcf890800c..c567f2b8d3c7c 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -98,6 +98,43 @@ module.exports = async function initConfig( { return config; }; +/** + * Checks the configured PHP version + * against the minimum version supported by Xdebug + * + * @param {WPConfig} config + * @return {bool} Whether the PHP version is supported by Xdebug + */ +function checkXdebugPhpCompatibility( config ) { + // By default, an undefined phpVersion uses the version on the docker image, + // which is supported by Xdebug 3. + let phpCompatibility = true; + + // If PHP version is defined + // ensure it meets the Xdebug minimum compatibility requirment + if ( config.env.development.phpVersion ) { + const versionTokens = config.env.development.phpVersion.split( + '.' + ); + const majorVer = parseInt( versionTokens[ 0 ] ); + const minorVer = parseInt( versionTokens[ 1 ] ); + + if ( isNaN( majorVer ) || isNaN( minorVer ) ) { + throw new Error( + 'Something went wrong when parsing the PHP version.' + ); + } + + // Xdebug 3 supports 7.2 and higher + // Ensure user has specified a compatible PHP version + if ( majorVer < 7 || ( majorVer === 7 && minorVer < 2 ) ) { + throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); + } + } + + return phpCompatibility; +} + /** * Generates the Dockerfile used by wp-env's development instance. * @@ -111,31 +148,11 @@ function dockerFileContents( image, config ) { let shouldInstallXdebug = false; if ( config.xdebug !== 'off' ) { - if ( config.env.development.phpVersion ) { - const versionTokens = config.env.development.phpVersion.split( - '.' - ); - const majorVersion = parseInt( versionTokens[ 0 ] ); - const minorVersion = parseInt( versionTokens[ 1 ] ); - - if ( isNaN( majorVersion ) || isNaN( minorVersion ) ) { - throw new Error( - 'Something went wrong when parsing the PHP version.' - ); - } - - // Xdebug 3 supports 7.2 and higher - // Ensure user has specified a compatible PHP version - const inompatiblePhp = majorVersion < 7 || ( majorVersion === 7 && minorVersion < 2 ); - - if ( inompatiblePhp ) { - throw new Error( 'Cannot use XDebug 3 on PHP < 7.2.' ); - } - } + const usingCompatiblePhp = checkXdebugPhpCompatibility(config); - // By default, an undefined phpVersion uses the version on the docker image, - // which is supported by Xdebug 3. - shouldInstallXdebug = true; + if ( usingCompatiblePhp ) { + shouldInstallXdebug = true; + } } return `FROM ${ image } From eca5421faf9c80a2f47f32b36d08f0b9fd05d8cc Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 29 Oct 2021 10:50:18 +0300 Subject: [PATCH 12/13] Fix docstring return data type --- packages/env/lib/init-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index c567f2b8d3c7c..741bbe2df5c8e 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -103,7 +103,7 @@ module.exports = async function initConfig( { * against the minimum version supported by Xdebug * * @param {WPConfig} config - * @return {bool} Whether the PHP version is supported by Xdebug + * @return {boolean} Whether the PHP version is supported by Xdebug */ function checkXdebugPhpCompatibility( config ) { // By default, an undefined phpVersion uses the version on the docker image, From c8318f500c0f5b2d274b64c62437a24ca6bf3df2 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 29 Oct 2021 11:34:33 +0300 Subject: [PATCH 13/13] Format code according to JavaScript CI task --- packages/env/lib/init-config.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 741bbe2df5c8e..88b9f403eed88 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -99,23 +99,21 @@ module.exports = async function initConfig( { }; /** - * Checks the configured PHP version + * Checks the configured PHP version * against the minimum version supported by Xdebug - * - * @param {WPConfig} config + * + * @param {WPConfig} config * @return {boolean} Whether the PHP version is supported by Xdebug */ function checkXdebugPhpCompatibility( config ) { // By default, an undefined phpVersion uses the version on the docker image, // which is supported by Xdebug 3. - let phpCompatibility = true; + const phpCompatibility = true; // If PHP version is defined // ensure it meets the Xdebug minimum compatibility requirment if ( config.env.development.phpVersion ) { - const versionTokens = config.env.development.phpVersion.split( - '.' - ); + const versionTokens = config.env.development.phpVersion.split( '.' ); const majorVer = parseInt( versionTokens[ 0 ] ); const minorVer = parseInt( versionTokens[ 1 ] ); @@ -148,7 +146,7 @@ function dockerFileContents( image, config ) { let shouldInstallXdebug = false; if ( config.xdebug !== 'off' ) { - const usingCompatiblePhp = checkXdebugPhpCompatibility(config); + const usingCompatiblePhp = checkXdebugPhpCompatibility( config ); if ( usingCompatiblePhp ) { shouldInstallXdebug = true;