Repository navigation
fix(shell): keep spaced command paths intact and explain Windows launch errors #1643
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
Draft
skevetter
wants to merge
10
commits into
main
Choose a base branch
from
fix/issue-1638-ssh-init-windows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4077ddc
test(shell): cover command variables whose path contains spaces
skevetter 195b0ef
ci: run emulated shell launch tests on Windows
skevetter c800de1
fix(shell): keep spaced executable paths intact and explain Windows l…
skevetter 0a495bd
fix(client): ask for diagnostics when Windows rejects a provider launch
skevetter 9854510
fix(shell): decide spaced executable joining when the command runs
skevetter ebe3676
fix(shell): tie spaced executable joining to the command that used th…
skevetter 50518ea
test(shell): order suite helpers after tests
skevetter 666b9a5
fix(shell): resolve relative command variables against the shell dire…
skevetter 485ac34
fix(client): limit the Windows launch hint to process-start errors
skevetter ee47850
fix(shell): split command variables only on shell IFS characters
skevetter 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //go:build !windows | ||
|
|
||
| package clientimplementation | ||
|
|
||
| func isInvalidProcessParameter(error) bool { | ||
| return false | ||
| } |
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,19 @@ | ||
| //go:build windows | ||
|
|
||
| package clientimplementation | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io/fs" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| // isInvalidProcessParameter reports only process-creation failures, which os/exec wraps as | ||
| // "fork/exec"; the same errno from an output writer after the process started is not one. | ||
| func isInvalidProcessParameter(err error) bool { | ||
| var pathErr *fs.PathError | ||
| return errors.As(err, &pathErr) && | ||
| pathErr.Op == "fork/exec" && | ||
| errors.Is(pathErr.Err, windows.ERROR_INVALID_PARAMETER) | ||
| } |
45 changes: 45 additions & 0 deletions
45
pkg/client/clientimplementation/provider_command_error_test.go
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,45 @@ | ||
| package clientimplementation | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io/fs" | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/provider" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type ProviderCommandErrorTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func TestProviderCommandErrorTestSuite(t *testing.T) { | ||
| suite.Run(t, new(ProviderCommandErrorTestSuite)) | ||
| } | ||
|
|
||
| func (s *ProviderCommandErrorTestSuite) TestOmitsEmptyAction() { | ||
| cause := errors.New("exit status 255") | ||
|
|
||
| err := providerCommandError("${SSH_PROVIDER} init", nil, cause) | ||
|
|
||
| s.Equal(`run provider command "${SSH_PROVIDER} init": exit status 255`, err.Error()) | ||
| s.ErrorIs(err, cause) | ||
| } | ||
|
|
||
| func (s *ProviderCommandErrorTestSuite) TestIncludesAction() { | ||
| cause := errors.New("exit status 1") | ||
| environ := []string{"UNRELATED=value", provider.CommandEnv + "=ls"} | ||
|
|
||
| err := providerCommandError("${SSH_PROVIDER} command", environ, cause) | ||
|
|
||
| s.Equal(`run provider command "${SSH_PROVIDER} command" (ls): exit status 1`, err.Error()) | ||
| } | ||
|
|
||
| func (s *ProviderCommandErrorTestSuite) TestPlainStartFailureHasNoWindowsHint() { | ||
| cause := &fs.PathError{Op: "fork/exec", Path: "provider", Err: fs.ErrPermission} | ||
|
|
||
| err := providerCommandError("provider", nil, cause) | ||
|
|
||
| s.NotContains(err.Error(), "ERROR_INVALID_PARAMETER") | ||
| s.ErrorIs(err, fs.ErrPermission) | ||
| } |
40 changes: 40 additions & 0 deletions
40
pkg/client/clientimplementation/provider_command_error_windows_test.go
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,40 @@ | ||
| //go:build windows | ||
|
|
||
| package clientimplementation | ||
|
|
||
| import ( | ||
| "io/fs" | ||
| "os" | ||
| "os/exec" | ||
| "syscall" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| // Combining DETACHED_PROCESS with CREATE_NEW_CONSOLE makes the real CreateProcess | ||
| // call fail with ERROR_INVALID_PARAMETER, the error reported in issue 1638. | ||
| func (s *ProviderCommandErrorTestSuite) TestRealInvalidParameterStartFailureGetsHint() { | ||
| self, err := os.Executable() | ||
| s.Require().NoError(err) | ||
| cmd := exec.Command(self) | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
| CreationFlags: windows.DETACHED_PROCESS | windows.CREATE_NEW_CONSOLE, | ||
| } | ||
| startErr := cmd.Start() | ||
| s.Require().ErrorIs(startErr, windows.ERROR_INVALID_PARAMETER) | ||
|
|
||
| err = providerCommandError(self, nil, startErr) | ||
|
|
||
| s.ErrorIs(err, windows.ERROR_INVALID_PARAMETER) | ||
| s.Contains(err.Error(), invalidProcessParameterHint) | ||
| s.Contains(err.Error(), "The parameter is incorrect") | ||
| } | ||
|
|
||
| func (s *ProviderCommandErrorTestSuite) TestInvalidParameterFromOutputWriterGetsNoHint() { | ||
| writeErr := &fs.PathError{Op: "write", Path: "stdout", Err: windows.ERROR_INVALID_PARAMETER} | ||
|
|
||
| err := providerCommandError("provider", nil, writeErr) | ||
|
|
||
| s.ErrorIs(err, windows.ERROR_INVALID_PARAMETER) | ||
| s.NotContains(err.Error(), invalidProcessParameterHint) | ||
| } |
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,197 @@ | ||
| package shell | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| const helperProcessEnv = "DEVSY_SHELL_TEST_HELPER_PROCESS" | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| if os.Getenv(helperProcessEnv) == "1" { | ||
| _, _ = os.Stdout.WriteString("helper ran with " + os.Args[len(os.Args)-1]) | ||
| os.Exit(0) | ||
| } | ||
| os.Exit(m.Run()) | ||
| } | ||
|
|
||
| type ExecutablePathTestSuite struct { | ||
| suite.Suite | ||
| executable string | ||
| } | ||
|
|
||
| func TestExecutablePathTestSuite(t *testing.T) { | ||
| suite.Run(t, new(ExecutablePathTestSuite)) | ||
| } | ||
|
|
||
| // SetupTest installs a copy of the test binary under a directory containing a | ||
| // space, like the binaries directory of a Windows profile such as "Jane Doe". | ||
| func (s *ExecutablePathTestSuite) SetupTest() { | ||
| dir := filepath.Join(s.T().TempDir(), "Jane Doe", "binaries") | ||
| s.Require().NoError(os.MkdirAll(dir, 0o750)) | ||
|
|
||
| name := "provider" | ||
| if runtime.GOOS == "windows" { | ||
| name += ".exe" | ||
| } | ||
| s.executable = filepath.Join(dir, name) | ||
|
|
||
| s.copyTestBinary(s.executable) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestCommandVariableWithSpacesInPathIsNotSplit() { | ||
| output, err := s.run( | ||
| "${SSH_PROVIDER} init", | ||
| "SSH_PROVIDER="+s.executable, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestShortVariableFormWithSpacesInPathIsNotSplit() { | ||
| output, err := s.run( | ||
| "$SSH_PROVIDER init", | ||
| "SSH_PROVIDER="+s.executable, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestCommandVariableWithArgumentsStillSplits() { | ||
| output, err := s.run("${CMD} world", "CMD=echo hello") | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("hello world\n", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestReassignedCommandVariableStillSplits() { | ||
| output, err := s.run("CMD='echo hello'; $CMD world", "CMD="+s.executable) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("hello world\n", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestCommandVariableAssignedInScriptIsNotSplit() { | ||
| output, err := s.run( | ||
| "PROVIDER='"+s.executable+"'; ${PROVIDER} init", | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestLiteralCommandIsNotJoinedWithUnrelatedVariable() { | ||
| dir := s.T().TempDir() | ||
| ext := filepath.Ext(s.executable) | ||
| tool := filepath.Join(dir, "tool"+ext) | ||
| spacedTool := filepath.Join(dir, "tool status"+ext) | ||
| s.copyTestBinary(tool) | ||
| s.copyTestBinary(spacedTool) | ||
|
|
||
| output, err := s.run( | ||
| "'"+tool+"' status; $CMD", | ||
| "CMD="+spacedTool, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with status"+"helper ran with "+spacedTool, output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestRelativePathIsResolvedAgainstShellDirectory() { | ||
| shellDir := s.T().TempDir() | ||
| relative := filepath.Join("spaced dir", filepath.Base(s.executable)) | ||
| s.Require().NoError(os.MkdirAll(filepath.Join(shellDir, "spaced dir"), 0o750)) | ||
| s.copyTestBinary(filepath.Join(shellDir, relative)) | ||
|
|
||
| output, err := s.run( | ||
| "cd '"+shellDir+"'; ${PROVIDER} init", | ||
| "PROVIDER="+relative, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestRelativePathExistingOnlyInProcessDirectoryIsNotJoined() { | ||
| processDir := s.T().TempDir() | ||
| shellDir := s.T().TempDir() | ||
| name := "spaced" + filepath.Ext(s.executable) | ||
| relative := "." + string(filepath.Separator) + name + " dir" + | ||
| string(filepath.Separator) + "provider" | ||
| s.Require().NoError(os.MkdirAll(filepath.Join(processDir, name+" dir"), 0o750)) | ||
| s.copyTestBinary(filepath.Join(processDir, name+" dir", "provider")) | ||
| s.copyTestBinary(filepath.Join(shellDir, name)) | ||
| s.T().Chdir(processDir) | ||
|
|
||
| output, err := s.run( | ||
| "cd '"+shellDir+"'; ${PROVIDER} init", | ||
| "PROVIDER="+relative, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestNonBreakingSpaceStaysInsideTheJoinedPath() { | ||
| dir := filepath.Join(s.T().TempDir(), "Jane\u00a0Doe and co") | ||
| s.Require().NoError(os.MkdirAll(dir, 0o750)) | ||
| executable := filepath.Join(dir, filepath.Base(s.executable)) | ||
| s.copyTestBinary(executable) | ||
|
|
||
| output, err := s.run( | ||
| "${PROVIDER} init", | ||
| "PROVIDER="+executable, | ||
| helperProcessEnv+"=1", | ||
| ) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("helper ran with init", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) TestVariableArgumentWithSpacesStillSplits() { | ||
| output, err := s.run("echo ${ARGS}", "ARGS=a b") | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("a b\n", output) | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) run(command string, environ ...string) (string, error) { | ||
| var stdout, stderr bytes.Buffer | ||
| err := RunEmulatedShell(context.Background(), &CommandRunner{ | ||
| Command: command, | ||
| Stdout: &stdout, | ||
| Stderr: &stderr, | ||
| Environ: append(os.Environ(), environ...), | ||
| }) | ||
| return stdout.String(), err | ||
| } | ||
|
|
||
| func (s *ExecutablePathTestSuite) copyTestBinary(target string) { | ||
| self, err := os.Executable() | ||
| s.Require().NoError(err) | ||
| source, err := os.Open(self) //nolint:gosec // copies this test binary | ||
| s.Require().NoError(err) | ||
| defer func() { _ = source.Close() }() | ||
| file, err := os.OpenFile( //nolint:gosec // the copy must be executable | ||
| target, os.O_CREATE|os.O_WRONLY, 0o700, | ||
| ) | ||
| s.Require().NoError(err) | ||
| _, err = io.Copy(file, source) | ||
| s.Require().NoError(err) | ||
| s.Require().NoError(file.Close()) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.