-
-
Notifications
You must be signed in to change notification settings - Fork 64
fix: resolve shell tabs via OS user shell #58
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
Merged
johannesjo
merged 4 commits into
johannesjo:main
from
ASRagab:fix/resolve-user-shell-selection
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { resolveUserShell } from './user-shell.js'; | ||
|
|
||
| const mockUserInfo = { | ||
| username: 'test-user', | ||
| uid: 501, | ||
| gid: 20, | ||
| homedir: '/home/test-user', | ||
| }; | ||
|
|
||
| const allowShells = | ||
| (...shells: string[]) => | ||
| (shell: string) => | ||
| shells.includes(shell); | ||
|
|
||
| describe('resolveUserShell', () => { | ||
| it('prefers the OS account shell over the inherited SHELL env var', () => { | ||
| const shell = resolveUserShell({ | ||
| userInfo: () => ({ | ||
| ...mockUserInfo, | ||
| shell: '/bin/zsh', | ||
| }), | ||
| env: { SHELL: '/bin/bash' }, | ||
| platform: 'darwin', | ||
| canUseShell: allowShells('/bin/zsh', '/bin/bash'), | ||
| }); | ||
|
|
||
| expect(shell).toBe('/bin/zsh'); | ||
| }); | ||
|
|
||
| it('falls back to SHELL when the OS lookup has no shell', () => { | ||
| const shell = resolveUserShell({ | ||
| userInfo: () => ({ | ||
| ...mockUserInfo, | ||
| shell: '', | ||
| }), | ||
| env: { SHELL: '/opt/homebrew/bin/bash' }, | ||
| platform: 'darwin', | ||
| canUseShell: allowShells('/opt/homebrew/bin/bash'), | ||
| }); | ||
|
|
||
| expect(shell).toBe('/opt/homebrew/bin/bash'); | ||
| }); | ||
|
|
||
| it('falls back to SHELL when the OS lookup throws', () => { | ||
| const shell = resolveUserShell({ | ||
| userInfo: () => { | ||
| throw new Error('unavailable'); | ||
| }, | ||
| env: { SHELL: '/bin/zsh' }, | ||
| platform: 'linux', | ||
| canUseShell: allowShells('/bin/zsh'), | ||
| }); | ||
|
|
||
| expect(shell).toBe('/bin/zsh'); | ||
| }); | ||
|
|
||
| it('falls back to SHELL when the OS shell is not executable', () => { | ||
| const shell = resolveUserShell({ | ||
| userInfo: () => ({ | ||
| ...mockUserInfo, | ||
| shell: '/missing/zsh', | ||
| }), | ||
| env: { SHELL: '/bin/bash' }, | ||
| platform: 'linux', | ||
| canUseShell: allowShells('/bin/bash'), | ||
| }); | ||
|
|
||
| expect(shell).toBe('/bin/bash'); | ||
| }); | ||
|
|
||
| it('falls back to /bin/sh on POSIX when neither OS nor env provides a usable shell', () => { | ||
| const shell = resolveUserShell({ | ||
| userInfo: () => ({ | ||
| ...mockUserInfo, | ||
| shell: '/missing/zsh', | ||
| }), | ||
| env: { SHELL: '/missing/bash' }, | ||
| platform: 'linux', | ||
| canUseShell: allowShells(), | ||
| }); | ||
|
|
||
| expect(shell).toBe('/bin/sh'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import fs from 'fs'; | ||
| import * as os from 'os'; | ||
|
|
||
| interface ResolveUserShellDeps { | ||
| userInfo?: () => { shell: string | null | undefined }; | ||
| env?: NodeJS.ProcessEnv; | ||
| platform?: NodeJS.Platform; | ||
| canUseShell?: (shell: string) => boolean; | ||
| } | ||
|
|
||
| function normalizeShell(shell: string | null | undefined): string | null { | ||
| const value = shell?.trim(); | ||
| return value ? value : null; | ||
| } | ||
|
|
||
| function isExecutablePosixShell(shell: string): boolean { | ||
| try { | ||
| fs.accessSync(shell, fs.constants.X_OK); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function resolveUserShell(deps: ResolveUserShellDeps = {}): string { | ||
| const env = deps.env ?? process.env; | ||
| const platform = deps.platform ?? process.platform; | ||
| const userInfo = deps.userInfo ?? os.userInfo; | ||
| const canUseShell = | ||
| deps.canUseShell ?? ((shell: string) => platform === 'win32' || isExecutablePosixShell(shell)); | ||
|
|
||
| try { | ||
| const osShell = normalizeShell(userInfo().shell); | ||
| if (osShell && canUseShell(osShell)) return osShell; | ||
| } catch { | ||
| // Fall back to inherited environment if the OS lookup is unavailable. | ||
| } | ||
|
|
||
| const envShell = normalizeShell(env.SHELL); | ||
| if (envShell && canUseShell(envShell)) return envShell; | ||
|
|
||
| return platform === 'win32' ? 'cmd.exe' : '/bin/sh'; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
resolveUserShell()returns the OS-reported shell path without verifying it exists/is executable. If a user's account shell is misconfigured (or points to a removed binary), this will now cause shell tabs to fail (viavalidateCommand) even ifenv.SHELLor/bin/shwould work. Consider checking executability before returningosShell, and fall back toenv.SHELL//bin/shwhen the OS shell isn't runnable.