Fix WSL agent setup to use the distro login shell (#5285)
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
buildWslInteractiveLoginShellCommand,
|
||||
buildWslLoginShellCommand,
|
||||
quotePosixShell
|
||||
} from './wsl-login-shell-command'
|
||||
|
||||
describe('wsl login shell command helpers', () => {
|
||||
it('quotes single quotes for POSIX shell arguments', () => {
|
||||
expect(quotePosixShell("a'b")).toBe("'a'\\''b'")
|
||||
})
|
||||
|
||||
it('runs commands through the distro user login shell', () => {
|
||||
const command = buildWslLoginShellCommand("printf 'hello'")
|
||||
|
||||
expect(command).toContain('getent passwd')
|
||||
expect(command).toContain('exec "$_orca_wsl_shell" -ilc')
|
||||
expect(command).toContain("printf '\\''hello'\\''")
|
||||
})
|
||||
|
||||
it('starts an interactive login shell without assuming bash', () => {
|
||||
const command = buildWslInteractiveLoginShellCommand()
|
||||
|
||||
expect(command).toContain('getent passwd')
|
||||
expect(command).toContain('if [ -z "$_orca_wsl_shell" ] || [ ! -x "$_orca_wsl_shell" ]; then')
|
||||
expect(command).toContain('exec "$_orca_wsl_shell" -l')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,37 @@
|
||||
export function quotePosixShell(value: string): string {
|
||||
return `'${value.replace(/'/g, "'\\''")}'`
|
||||
}
|
||||
|
||||
export function escapeWslShCommandForWindows(command: string): string {
|
||||
return command.replace(/\$/g, '\\$')
|
||||
}
|
||||
|
||||
export function buildWslLoginShellCommand(command: string): string {
|
||||
const quotedCommand = quotePosixShell(command)
|
||||
return [
|
||||
'_orca_wsl_shell=$(getent passwd "$(id -un)" 2>/dev/null | cut -d: -f7)',
|
||||
'if [ -z "$_orca_wsl_shell" ] || [ ! -x "$_orca_wsl_shell" ]; then',
|
||||
' _orca_wsl_shell="${SHELL:-/bin/bash}"',
|
||||
'fi',
|
||||
'if [ -z "$_orca_wsl_shell" ] || [ ! -x "$_orca_wsl_shell" ]; then',
|
||||
' _orca_wsl_shell=/bin/sh',
|
||||
'fi',
|
||||
'case "$(basename "$_orca_wsl_shell")" in',
|
||||
` sh|dash) exec "$_orca_wsl_shell" -lc ${quotedCommand} ;;`,
|
||||
` *) exec "$_orca_wsl_shell" -ilc ${quotedCommand} ;;`,
|
||||
'esac'
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
export function buildWslInteractiveLoginShellCommand(): string {
|
||||
return [
|
||||
'_orca_wsl_shell=$(getent passwd "$(id -un)" 2>/dev/null | cut -d: -f7)',
|
||||
'if [ -z "$_orca_wsl_shell" ] || [ ! -x "$_orca_wsl_shell" ]; then',
|
||||
' _orca_wsl_shell="${SHELL:-/bin/bash}"',
|
||||
'fi',
|
||||
'if [ -z "$_orca_wsl_shell" ] || [ ! -x "$_orca_wsl_shell" ]; then',
|
||||
' _orca_wsl_shell=/bin/sh',
|
||||
'fi',
|
||||
'exec "$_orca_wsl_shell" -l'
|
||||
].join('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user