Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-env: run command with double brackets not working #58253

Open
Luc-cpl opened this issue Jan 25, 2024 · 0 comments
Open

wp-env: run command with double brackets not working #58253

Luc-cpl opened this issue Jan 25, 2024 · 0 comments
Labels
[Package] Env /packages/env [Type] Bug An existing feature does not function as intended

Comments

@Luc-cpl
Copy link

Luc-cpl commented Jan 25, 2024

Description

In various instances throughout the @wordpress/env documentation, the run commands are illustrated with double quotes, as demonstrated in the following example from here:

wp-env run cli "wp rewrite structure /%postname%/"

This format is also used in libraries such as @10up/cypress-wp-utils for wp Cli commands with wp-env. However, this approach ceased functioning since wp-env 7.0.0.

Upon investigation, I identified the issue in the /packages/env/lib/commands/run.js file, specifically within the showCommandTips function, which stems from modifications introduced in the following pull requests:

#50559 - Reworked run Command Parsing
#50007 - Improve run command execution speed

Step-by-step reproduction instructions

  1. Install wp-env in a new project with vevrsion 7.0.0 or newer
  2. Start the env with npm run env:start
  3. Run any command with double quotes, as npm run env run tests-cli "plugin list", then you should receive the following error:
$ npm run env run tests-cli "plugin list"

> test@1.0.0 env
> wp-env run tests-cli plugin list

ℹ Starting 'plugin list' on the tests-cli container. 

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "plugin list": executable file not found in $PATH: unknown
✖ Command failed with exit code 126
Command failed with exit code 126

Screenshots, screen recording, code snippet

After modifying the showCommandTips function in /packages/env/lib/commands/run.js to the following code snippet, the run command starts to work correctly. However, I do not see this as a fix at this time, as it negates the performance improvements introduced by utilizing exec instead of run in #50007.

/**
 * Runs an arbitrary command on the given Docker container.
 *
 * @param {WPConfig} config    The wp-env configuration.
 * @param {string}   container The Docker container to run the command on.
 * @param {string[]} command   The command to run.
 * @param {string}   envCwd    The working directory for the command to be executed from.
 * @param {Object}   spinner   A CLI spinner which indicates progress.
 */
function spawnCommandDirectly( config, container, command, envCwd, spinner ) {
	// Both the `wordpress` and `tests-wordpress` containers have the host's
	// user so that they can maintain ownership parity with the host OS.
	// We should run any commands as that user so that they are able
	// to interact with the files mounted from the host.
	const hostUser = getHostUser();

	// Since Docker requires absolute paths, we should resolve the input to a POSIX path.
	// This is needed because Windows resolves relative paths from the C: drive.
	envCwd = path.posix.resolve(
		// Not all containers have the same starting working directory.
		container === 'mysql' || container === 'tests-mysql'
			? '/'
			: '/var/www/html',
		// Remove spaces and single quotes from both ends of the path.
		// This is needed because Windows treats single quotes as a literal character.
		envCwd.trim().replace( /^'|'$/g, '' )
	);

	const composeCommand = [
		'compose',
		'-f',
		config.dockerComposeConfigPath,
		'run',
		'-w',
		envCwd,
		'--user',
		hostUser.fullUser,
		'--rm',
	];

	if ( ! process.stdout.isTTY ) {
		composeCommand.push( '-T' );
	}

	composeCommand.push( container, ...command );

	return new Promise( ( resolve, reject ) => {
		// Note: since the npm docker-compose package uses the -T option, we
		// cannot use it to spawn an interactive command. Thus, we run docker-
		// compose on the CLI directly.
		const childProc = spawn(
			'docker',
			composeCommand,
			{
				stdio: 'inherit',
				shell: true,
			},
			spinner
		);
		childProc.on( 'error', reject );
		childProc.on( 'exit', ( code ) => {
			// Code 130 is set if the user tries to exit with ctrl-c before using
			// ctrl-d (so it is not an error which should fail the script.)
			if ( code === 0 || code === 130 ) {
				resolve();
			} else {
				reject( `Command failed with exit code ${ code }` );
			}
		} );
	} );
}

Environment info

  • Node v16.20.2
  • Linux - Pop!_OS 22.04 LTS (also tested on a mac M1)

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

No

@Luc-cpl Luc-cpl added the [Type] Bug An existing feature does not function as intended label Jan 25, 2024
@t-hamano t-hamano added the [Package] Env /packages/env label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Env /packages/env [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

2 participants