8000 Fix: Properly handle paths with spaces in generated command by pkashin · Pull Request #749 · air-verse/air · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: Properly handle paths with spaces in generated command #749

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
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ func (e *Engine) runBin() error {
case <-killCh:
return
default:
command := strings.Join(append([]string{e.config.Build.Bin}, e.runArgs...), " ")
formattedBin := formatPath(e.config.Build.Bin)
command := strings.Join(append([]string{formattedBin}, e.runArgs...), " ")
cmd, stdout, stderr, err := e.startCmd(command)
if err != nil {
e.mainLog("failed to start %s, error: %s", e.config.rel(e.config.binPath()), err.Error())
Expand Down
14 changes: 14 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,17 @@

return filepath.Join(root, path)
}

func formatPath(path string) string {
if !filepath.IsAbs(path) || !strings.Contains(path, " ") {
return path
}

quotedPath := fmt.Sprintf(`"%s"`, path)

if runtime.GOOS == PlatformWindows {
return fmt.Sprintf(`& %s`, quotedPath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

im not using Windows, what does & mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding & before the path in quotation marks ("path") is necessary for PowerShell or the Windows command prompt to correctly interpret the path with spaces as a command to execute. Without &, a path with spaces can be interpreted as a string, not as a command.
Usage examples:

  1. Running an executable file with spaces in the path:
    If the path to the executable file contains spaces, PowerShell will not be able to execute it without &.
& "C:\Program Files\MyApp\myapp.exe"
  1. Running commands with arguments:
    You can use & to run commands with arguments:
& "C:\Program Files\MyApp\myapp.exe" --arg1 value1

Copy link
Contributor Author
@pkashin pkashin Mar 4, 2025

Choose a reason for hiding this comment

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

Notes:

  1. If the path to an executable file or script is called without &, PowerShell interprets it as a string, not as a command. For example:
"C:\Program Files\MyApp\myapp.exe"

Result:
It just returns the string C:\Program Files\MyApp\myapp.exe , but the command is not executed.

  1. If the path contains spaces and is not enclosed in quotation marks, PowerShell or the command line will not be able to process it correctly. For example:
C:\Program Files\MyApp\myapp.exe

Result:
PowerShell will try to interpret C:\Program As a command, eh Files\MyApp\myapp.exe as an argument, which will lead to an error.

  1. I ran the tests manually locally on Windows, then the coverage reaches 100% for the formatPath() function.

}

Check warning on line 442 in runner/util.go

View check run for this annotation

Codecov / codecov/patch

runner/util.go#L441-L442

Added lines #L441 - L442 were not covered by tests

return quotedPath
}
84 changes: 84 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,87 @@ func TestJoinPathAbsolute(t *testing.T) {

assert.Equal(t, result, path)
}

func TestFormatPath(t *testing.T) {
type testCase struct {
name string
path string
expected string
}

runTests := func(t *testing.T, tests []testCase) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := formatPath(tt.path)
if result != tt.expected {
t.Errorf("formatPath(%q) = %q, want %q", tt.path, result, tt.expected)
}
})
}
}

t.Run("PathPlatformSpecific", func(t *testing.T) {
if runtime.GOOS == PlatformWindows {
// Windows-specific tests
tests := []testCase{
{
name: "Windows style absolute path with spaces",
path: `C:\My Documents\My Project\tmp\app.exe`,
expected: `& "C:\My Documents\My Project\tmp\app.exe"`,
},
{
name: "Windows style relative path with spaces",
path: `My Project\tmp\app.exe`,
expected: `My Project\tmp\app.exe`,
},
{
name: "Windows style absolute path without spaces",
path: `C:\Documents\Project\tmp\app.exe`,
expected: `C:\Documents\Project\tmp\app.exe`,
},
}
runTests(t, tests)
} else {
// Unix-specific tests
tests := []testCase{
{
name: "Unix style absolute path with spaces",
path: `/usr/local/my project/tmp/main`,
expected: `"/usr/local/my project/tmp/main"`,
},
{
name: "Unix style relative path with spaces",
path: "./my project/tmp/main",
expected: "./my project/tmp/main",
},
{
name: "Unix style absolute path without spaces",
path: `/usr/local/project/tmp/main`,
expected: `/usr/local/project/tmp/main`,
},
}
runTests(t, tests)
}
})

t.Run("CommonCases", func(t *testing.T) {
tests := []testCase{
{
name: "Empty path",
path: "",
expected: "",
},
{
name: "Simple path",
path: "main.go",
expected: "main.go",
},
{
name: "TestShouldIncludeIncludedFile",
path: "sh main.sh",
expected: "sh main.sh",
},
}
runTests(t, tests)
})
}
Loading
0