Skip to content

Commit

Permalink
Initial fix for home detection for csh/tcsh shell (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoofsKelvin committed Oct 6, 2021
1 parent e326a16 commit 7605237
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

## Unreleased

### Hotfix
- Fix the issue with failing home detecting for `csh`/`tcsh` shells (#295)

### Development changes
- More improvements in logging, especially regarding async stack tracing
- Properly "set boundaries", detect/analyze and log `toPromise`/`catchingPromise` calls
Expand Down
14 changes: 10 additions & 4 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export interface Connection {
}

async function tryGetHome(ssh: Client): Promise<string | null> {
const exec = await toPromise<ClientChannel>(cb => ssh.exec('echo "::sshfs:home:$(echo ~)\n"', cb));
const exec = await toPromise<ClientChannel>(cb => ssh.exec('echo "::sshfs:home:`echo ~`:"', cb));
let home = '';
exec.stdout.on('data', (chunk: any) => home += chunk);
await toPromise(cb => exec.on('close', cb));
if (!home) return null;
return home.match(/::sshfs:home:(.*?)\n/)?.[1] || null;
return home.match(/::sshfs:home:(.*?):/)?.[1] || null;
}

const TMP_PROFILE_SCRIPT = `
Expand Down Expand Up @@ -80,8 +80,9 @@ export class ConnectionManager {
protected async _createCommandTerminal(client: Client, authority: string, debugLogging: boolean): Promise<string> {
const logging = Logging.scope(`CmdTerm(${authority})`);
const shell = await toPromise<ClientChannel>(cb => client.shell({}, cb));
shell.write('echo ::sshfs:TTY:$(tty)\n');
shell.write('echo ::sshfs:TTY:`tty`\n');
return new Promise((resolvePath, rejectPath) => {
setTimeout(() => rejectPath(new Error('Timeout fetching command path')), 10e3);
const rl = readline.createInterface(shell.stdout);
shell.stdout.once('error', rejectPath);
shell.once('close', () => rejectPath());
Expand Down Expand Up @@ -168,6 +169,7 @@ export class ConnectionManager {
home = '';
}
}
logging.debug`Home path: ${home}`;
// Calculate the environment
const environment: EnvironmentVariable[] = mergeEnvironment([], config.environment);
// Set up stuff for receiving remote commands
Expand All @@ -179,8 +181,12 @@ export class ConnectionManager {
const cmdPath = await this._createCommandTerminal(client, name, flagRCDV);
environment.push({ key: 'KELVIN_SSHFS_CMD_PATH', value: cmdPath });
const sftp = await toPromise<SFTPWrapper>(cb => client.sftp(cb));
await toPromise(cb => sftp.writeFile('/tmp/.Kelvin_sshfs', TMP_PROFILE_SCRIPT, { mode: 0o666 }, cb));
await toPromise(cb => sftp.writeFile('/tmp/.Kelvin_sshfs', TMP_PROFILE_SCRIPT, { mode: 0o666 }, cb)).catch(e => {
logging.error`Failed to write profile script to /tmp/.Kelvin_sshfs:\n${e}\nDisabling REMOTE_COMMANDS flag`;
actualConfig.flags = ['-REMOTE_COMMANDS', ...(actualConfig.flags || [])];
});
}
logging.debug`Environment: ${environment}`;
// Set up the Connection object
let timeoutCounter = 0;
const con: Connection = {
Expand Down
4 changes: 3 additions & 1 deletion src/pseudoTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ export async function createTerminal(options: TerminalOptions): Promise<SSHPseud
commands.unshift(`cd ${workingDirectory}`);
}
const pseudoTtyOptions: PseudoTtyOptions = { ...PSEUDO_TTY_OPTIONS, cols: dims?.columns, rows: dims?.rows };
const channel = await toPromise<ClientChannel | undefined>(cb => client.exec(joinCommands(commands, separator)!, { pty: pseudoTtyOptions }, cb));
const cmd = joinCommands(commands, separator)!;
Logging.debug(`Starting shell for ${connection.actualConfig.name}: ${cmd}`);
const channel = await toPromise<ClientChannel | undefined>(cb => client.exec(cmd, { pty: pseudoTtyOptions }, cb));
if (!channel) throw new Error('Could not create remote terminal');
pseudo.channel = channel;
channel.on('exit', onDidClose.fire);
Expand Down

0 comments on commit 7605237

Please sign in to comment.