Skip to content
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

feat: Add support for executing processes with Windows ConPty #311

Merged
merged 22 commits into from
Feb 17, 2022
Merged

Conversation

kylecarbs
Copy link
Member

@kylecarbs kylecarbs commented Feb 17, 2022

I didn't think we'd need to execute Windows processes inside an emulated TTY, but it's required to make the SSH experience pleasant.

The console package @bryphe-coder was so extremely helpful to making this work, it's crazy! I minified our usage of the console package as well to be entirely bare-bones. It's almost unrecognizable.

For the start function, I primarily read through the documentation linked above the function to make it concise. The library we were using did a lot of bespoke things that are in the Go stdlib (assuming that's because it's old).

I took the liberty of stealing the pty name. It felt reasonable, since nothing in our code should use another external pty library, otherwise it will lack cross-platform support.

This is the precursor to the workspace agent, which should allow us to complete the entire user-flow.

kylecarbs and others added 14 commits February 15, 2022 20:35
This prevents a io.ErrShortBuffer from occurring when the byte
slice being read is smaller than the chunks sent from the opposite
pipe.

This makes sense for unordered connections, where transmission is
not guarunteed, but does not make sense for TCP-like connections.

We use a bufio.Reader when ordered to ensure data isn't lost.
@kylecarbs kylecarbs self-assigned this Feb 17, 2022
@codecov
Copy link

codecov bot commented Feb 17, 2022

Codecov Report

Merging #311 (e2d1f95) into main (c2ad91b) will decrease coverage by 0.06%.
The diff coverage is 61.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #311      +/-   ##
==========================================
- Coverage   67.76%   67.70%   -0.07%     
==========================================
  Files         135      138       +3     
  Lines        7161     7256      +95     
  Branches       74       74              
==========================================
+ Hits         4853     4913      +60     
- Misses       1815     1841      +26     
- Partials      493      502       +9     
Flag Coverage Δ
unittest-go-macos-latest 66.26% <74.19%> (+0.20%) ⬆️
unittest-go-ubuntu-latest 67.49% <69.89%> (+0.21%) ⬆️
unittest-go-windows-latest 66.00% <62.17%> (+0.11%) ⬆️
unittest-js 64.81% <ø> (ø)
Impacted Files Coverage Δ
peer/channel.go 82.45% <0.00%> (-0.98%) ⬇️
pty/pty.go 0.00% <0.00%> (ø)
pty/pty_other.go 26.47% <42.85%> (ø)
pty/start_windows.go 52.25% <52.25%> (ø)
pty/pty_windows.go 59.01% <59.01%> (ø)
pty/start_other.go 66.66% <66.66%> (ø)
pty/ptytest/ptytest.go 89.79% <89.79%> (ø)
cli/root.go 83.45% <100.00%> (+4.16%) ⬆️
pty/start.go 100.00% <100.00%> (ø)
peer/conn.go 78.46% <0.00%> (-0.52%) ⬇️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c2ad91b...e2d1f95. Read the comment docs.

if err != nil {
return nil, err
}
pathPtr, err := windows.UTF16PtrFromString(fullPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on these 👍 Always confusing that Win32 APIs take UTF16 strings

Comment on lines +107 to +109
// dedupEnvCase is dedupEnv with a case option for testing.
// If caseInsensitive is true, the case of keys is ignored.
func dedupEnvCase(caseInsensitive bool, env []string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ya... this is a confusing part of dealing with Windows APIs too (coming from Linux). Thanks for implementing this

Comment on lines +19 to +23
t.Run("Echo", func(t *testing.T) {
t.Parallel()
pty := ptytest.Start(t, exec.Command("cmd.exe", "/c", "echo", "test"))
pty.ExpectMatch("test")
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool - thanks for adding a test for this!


import "os/exec"

func Start(cmd *exec.Cmd) (PTY, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice cross-platform abstraction. This should be a go package - would've made our life easier!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. It would be a good stdlib addition!

Comment on lines +70 to +82
func (p *ptyWindows) Output() io.ReadWriter {
return readWriter{
Reader: p.outputRead,
Writer: p.outputWrite,
}
}

func (p *ptyWindows) Input() io.ReadWriter {
return readWriter{
Reader: p.inputRead,
Writer: p.inputWrite,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement to the interface! 💯

Comment on lines +29 to +33
func (p *otherPty) Input() io.ReadWriter {
return readWriter{
Reader: p.tty,
Writer: p.pty,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so much cleaner than it was before

Comment on lines +17 to +18
pty := ptytest.Start(t, exec.Command("echo", "test"))
pty.ExpectMatch("test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this test too. Both of these pty tests here and on Windows add a lot of confidence in this abstraction

@@ -47,6 +47,7 @@ func startPty(cmd *exec.Cmd) (PTY, error) {
if err != nil {
return nil, err
}
// Taken from: https://github.com/microsoft/hcsshim/blob/2314362e977aa03b3ed245a4beb12d00422af0e2/internal/winapi/process.go#L6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this too!

Copy link
Contributor

@bryphe-coder bryphe-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you for improving the pty / conpty interface and adding tests that cover the pty execution!

@bryphe-coder
Copy link
Contributor

And nice job figuring out those tricky Win32 API issues - like the magic const, the UTF16 string conversion, the environment variable handling, getting the console hooked up to the process, etc.

@kylecarbs kylecarbs merged commit 503d09c into main Feb 17, 2022
@kylecarbs kylecarbs deleted the pty branch February 17, 2022 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants