-
Notifications
You must be signed in to change notification settings - Fork 243
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
fix (shell) : Improve CRC shell detection on unix environments (#3767) #4526
Conversation
Skipping CI for Draft Pull Request. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
9e8ca7c
to
ae1fd94
Compare
pkg/os/shell/shell_unix.go
Outdated
shell := os.Getenv("SHELL") | ||
|
||
if shell == "" { | ||
detectedShell := detectShellByInvokingCommand("", "ps", []string{"-o", "pid,comm"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see that in that function you are then trying to split the line and ignoring 0 index which can be done using following so we don't have to ignore it later.
$ ps -o pid=,comm=
57609 bash
242709 ps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. Thanks for the hint! I didn't know about this.
return detectedShell | ||
} | ||
|
||
func inspectProcessOutputForRecentlyUsedShell(psCommandOutput string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is doing lot of things, may be add a comment what actually this is doing with example so would be easy for review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll update it as per your comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added comments to this method and other related methods.
pkg/os/shell/shell.go
Outdated
} | ||
sort.Slice(processOutputs, func(i, j int) bool { | ||
return processOutputs[i].processID > processOutputs[j].processID | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we are implementing this sorting function for slice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we want to get the shell process with most recent process id (latest). Therefore I am sorting the arraylist in reverse order and getting first element for detecting currently active shell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this sorting method. We can achieve same thing using --sort
flag in ps
command.
a61a8b1
to
5f4abd9
Compare
pkg/os/shell/shell.go
Outdated
// underlying shell type, it returns and empty string. | ||
// | ||
// This method tries to check all processes open and filters out shell sessions (one of `zsh`, `bash` or `fish) | ||
// It then tries to sort these processes by process ids and returns the most recent one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this method you are not performing any sorting because the ps
command which is used already performing the sort with pid so better to update that info. https://play.golang.com/p/QhNeqa5tnEO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@praveenkumar : Sorry, I forgot to update the method comment after making changes to the method.
Should be okay now.
cc31fe3
to
5dbace3
Compare
crc.exe
shell detection on windows for linux systems (#3767)- Currently we rely on `SHELL` environment variable for detecting active shell type. This will work when the environment variable is set. As per my observations, this environment variable is not set explicitly by various linux shell environments (`bash`,`zsh`,`fish`). We should detect currently active shell by checking currently active processes instead. - While generating statements for export statements on Windows, we shall make sure that we have converted windows paths to linux paths. Signed-off-by: Rohan Kumar <[email protected]>
5dbace3
to
4314e47
Compare
@rohanKanojia: The following tests failed, say
Full PR test history. Your PR dashboard. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
As mentioned in the first message, and related to #4537:
Have tests on MacOS been performed by reviewer(s)? |
No, I did the review and didn't perform any test on mac for same, my understanding was since it have unit tests which run for each platform but those were mocked one :( |
@praveenkumar @gbraad : I apologize for introducing this regression. In unit tests, I had mocked the command output that's why they were not able to catch this failure :-( |
I don't understand how the most recent process id relates to the shell I'm running crc in? I could have launched a bash shell at the beginning of my session, then spawn a zsh shell to do some testing, and get back to the initial bash shell to use crc? If we want to do some more extensive shell guessing by iterating over processes, could we not limit ourselves to the parents of the crc process? crc/pkg/os/shell/shell_windows.go Lines 79 to 96 in cebafa4
shellType we run again ps in some cases.I'd prefer that we extend this approach and drop the calls to an external command, and the parsing which comes with it. |
@cfergeau : Could you please elaborate on this failing scenario? I tried creating a new terminal instance from the current $ ps -o pid,comm
PID COMMAND
218741 bash
290485 ps
From what I remember this We had tried other options like |
Ah, I always use
I can get this to misbehave though with 2 ssh sessions to a mac machine, run
In the scenario described above on macos, with
(I'm not suggesting to use |
Description
Fixes: #3797
Relates to: #3797
SHELL
environment variable for detecting active shell type. This will work when the environment variable is set. As per my observations, this environment variable is not set explicitly by various Linux shell environments (bash
,zsh
,fish
). We should detect the currently active shell by checking the active processes instead.Type of change
test, version modification, documentation, etc.)
Proposed changes
shell_test.go
to not run on Windows, move unix-specific tests toshell_unix_test.go
, add generic platform-independent tests toshell_test.go
bash
andzsh
to the list of supported shell environments on Windows.--shell
flag to print shell specific instructions, detect shell automatically by inspecting currently open processes:ps
command to get list of active shell processes and pick the most recent process id.wsl
tool to execute linuxps
process to get list of active shell processes and return the one with most recent process id.wslpath
utility tool to perform this conversion.With these changes, we should see the correct output of
crc podman-env
andcrc oc-env
on:Running
crc oc-env
on Git Bash:Old Behavior
New Behavior
Testing
crc podman-env
/crc oc-env
commands. They should not print output with respect to current shell in use.Contribution Checklist