laravel / envoy

Elegant SSH tasks for PHP.

Home Page:https://laravel.com/docs/envoy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Localhost shell

Tofandel opened this issue · comments

  • Envoy Version: 2.8.2
  • PHP Version: 8.0.17

Description:

The localhost run doesn't use the same shell as the one it's executed on

Consider the following Envoy.blade.php

@servers(['local' => '127.0.0.1', 'dev' => 'root@example.com'])

@task('test')
shopt -s extglob
ls !(git)
@endtask

The script will work fine on root@example.com because the shell is bash (assuming that's the configured shell of the root user)
But it will not work on the local env because it's executed with sh even though my shell is configured to be /bin/bash

Also note that because of the usage of shopt it's not possible to rewrite this script as bash -c "shopt -s extglob && ls !(git)" which would produce a syntax error because shopt hasn't applied yet.

Steps To Reproduce:

Simply composer require --dev laravel/envoy and paste the blade file

@servers(['local' => '127.0.0.1'])

@task('test')
shopt -s extglob
ls !(git)
@endtask

Run echo "$SHELL" && vendor/bin/envoy run test

/bin/bash
[127.0.0.1]:  sh: 1: shopt: not found
[127.0.0.1]:  sh: 2: Syntax error: "(" unexpected
[✗] This task did not complete successfully on one of your servers.

Notice how $SHELL is /bin/bash but envoy uses /bin/sh

Resolution

Ideally envoy should get the name of the shell in which it is running and run the local command using that same shell, though php doesn't seem to give the option with proc_exec so this would require a workaround of spawning a shell within proc_exec

A nice addition when this is fixed would be if we could specify the shell to use in the task arguments as well

This just seems to work for me

Screenshot 2022-03-28 at 10 52 33

Then you probably have a symlink from /bin/sh to some other shell from your OS but that's really platform specific, definitely not the case on Ubuntu (it's dash on ubuntu) and not the point of this issue at all

You could use this which will give you your current shell and the one running in envoy

@servers(['local' => '127.0.0.1'])

@task('test')
echo $SHELL
readlink /proc/$$/exe
shopt -s extglob
ls !(git)
@endtask

I managed to get it working like this

@servers(['local' => '127.0.0.1'])

@task('test')
$SHELL -c "
shopt -s extglob
ls !(git)
"
@endtask

So implementing shell choice and fixing this bug should be easy enough

WIP fork 2.x...Tofandel:2.x

I'll send a PR soon

I noticed there was an as option that's been there since the very begining but never got implemented so I'm also adding that

It obviously requires sudo to be installed though and it's not ported to windows yet

I'm planning to work on the test suite as well which is currently not very furnished