finch icon indicating copy to clipboard operation
finch copied to clipboard

Unable to use the "--env" option on finch container run

Open alecthegeek opened this issue 1 year ago • 3 comments

Describe the bug Cannot use --env option

Steps to reproduce

finch container run -it --rm --env="E=v" busybox env

Expected behavior

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
HOME=/root
E=v

Screenshots or logs

Actual result

FATA[0000] unknown shorthand flag: 'e' in -e

Additional context

Note: Updated to Finch 1.1.2 -- error still present

As a test I ran nerdctl (installed via Rancher Desktop) and it worked as expected

$ nerdctl container run -it --rm --env="E=v" busybox env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
E=v
TERM=xterm
HOME=/root

To help debug the issue as quickly as possible, we recommend generating a support bundle with finch support-bundle generate and attaching it to this issue. This packages all Finch-related configs and logs into one file. finch-support-20240222101339.zip

alecthegeek avatar Feb 21 '24 23:02 alecthegeek

Thanks for the issue. The command works properly when you use finch run, but fails with finch container run:

$ finch run -it --rm --env="E=v" busybox env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
E=v
TERM=xterm
HOME=/root
$ finch container run -it --rm --env="E=v" busybox env
FATA[0000] unknown shorthand flag: 'e' in -e            
FATA[0000] exit status 1

The --debug option shows improper ordering of nerdctl arguments, where -e argument is passed before the run command:

$ finch --debug container run -it --rm --env="E=v" busybox env
DEBU[0000] Creating limactl command: ARGUMENTS: [shell finch sudo nerdctl container -e E=v run -it --rm busybox env], LIMA_HOME: /Applications/Finch/lima/data
...

mharwani avatar Feb 24 '24 02:02 mharwani

In the following code:

limaArgs = append(limaArgs, append([]string{nerdctlCmdName}, strings.Fields(cmdName)...)...)

var finalArgs []string
for key, val := range envVars {
	finalArgs = append(finalArgs, "-e", fmt.Sprintf("%s=%s", key, val))
}
finalArgs = append(finalArgs, nerdctlArgs...)
// Add -E to sudo command in order to preserve existing environment variables, more info:
// https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo/8633575#8633575
limaArgs = append(limaArgs, finalArgs...)

The environment variables (such as -e E=v) are appended first, followed by rest of the arguments. For most cases, such as finch run, this should be fine because run is used as the command appended before any arguments. However, for cases like finch container run, run is treated as another argument and appended after the environment variables.

mharwani avatar Feb 24 '24 02:02 mharwani

Tested release 1.1.3

The original bug report is now fixed.

However as explained in the PR the following does not work

finch container run -it --rm -e "E=v" busybox echo -e "hello\tbye"

alecthegeek avatar Apr 02 '24 20:04 alecthegeek

Thank you @chews93319 for this. Waiting for the next release to test

alecthegeek avatar Sep 10 '24 02:09 alecthegeek