Skip to content

Fix #133 SanitizeCommand removes comments #134

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
May 15, 2025
Merged

Conversation

mostlygeek
Copy link
Owner

@mostlygeek mostlygeek commented May 15, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of command input to ignore comment lines and extra whitespace when processing commands.
  • Tests

    • Updated tests to verify that comment lines and blank lines are correctly ignored during command processing.

@mostlygeek mostlygeek added the bug Something isn't working label May 15, 2025
Copy link

coderabbitai bot commented May 15, 2025

Walkthrough

The SanitizeCommand function in proxy/config.go was refactored to process command strings line-by-line, trimming whitespace, removing comment lines, and handling line continuations marked by trailing backslashes. Corresponding tests in both POSIX and Windows test files were updated to include comment and whitespace scenarios, ensuring the sanitizer correctly ignores comments and blank lines.

Changes

File(s) Change Summary
proxy/config.go Refactored SanitizeCommand to process input line-by-line, trim whitespace, skip comments, and handle line continuations.
proxy/config_posix_test.go Updated test input to include comments and blank lines; verified that output arguments are unaffected.
proxy/config_windows_test.go Modified test input to add comments, tabs, and blank lines; ensured sanitizer strips these as expected.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant SanitizeCommand

    Caller->>SanitizeCommand: Provide multi-line command string
    SanitizeCommand->>SanitizeCommand: Split input into lines
    loop For each line
        SanitizeCommand->>SanitizeCommand: Trim whitespace
        alt Line is comment or blank
            SanitizeCommand->>SanitizeCommand: Skip line
        else Line ends with backslash
            SanitizeCommand->>SanitizeCommand: Remove backslash, append space
        else
            SanitizeCommand->>SanitizeCommand: Append line as-is
        end
    end
    SanitizeCommand->>SanitizeCommand: Rejoin cleaned lines
    SanitizeCommand->>SanitizeCommand: Split into arguments (shlex)
    SanitizeCommand-->>Caller: Return argument array or error
Loading

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mostlygeek mostlygeek self-assigned this May 15, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between d7b390d and bf4e86b.

📒 Files selected for processing (3)
  • proxy/config.go (1 hunks)
  • proxy/config_posix_test.go (1 hunks)
  • proxy/config_windows_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests
🔇 Additional comments (2)
proxy/config_posix_test.go (1)

17-25: Good test additions for comment handling and whitespace!

The test has been appropriately expanded to include various comment lines and whitespace formatting that should be handled by the updated sanitizer. This ensures the function correctly removes comment lines while preserving the actual command arguments.

proxy/config_windows_test.go (1)

14-25: Test modifications look good for Windows platform

The changes to the Windows test properly mirror the improvements made in the POSIX test, ensuring consistent behavior across platforms. The test now verifies that comments and whitespace are correctly handled in the Windows environment as well.

Comment on lines 231 to 249
func SanitizeCommand(cmdStr string) ([]string, error) {
// Remove trailing backslashes
cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ")
cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ")
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
cleanedLines = append(cleanedLines, line)
}
}

// put it back together
cmdStr = strings.Join(cleanedLines, "\n")

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Good implementation for handling comments and line continuations

The refactored implementation properly addresses the issue by processing the command string line-by-line, which is more robust than a global replacement approach. The function now correctly:

  1. Skips lines that are comments (start with #)
  2. Handles trailing backslashes as line continuations
  3. Preserves original lines that don't need special handling

However, there's one inconsistency in how whitespace is handled:

- cleanedLines = append(cleanedLines, line)
+ cleanedLines = append(cleanedLines, trimmed)

For non-continuation lines, you're preserving the original line with its whitespace, but for continuation lines, you're using the trimmed version. This inconsistency could lead to unexpected behavior with whitespace. Since you're trimming the whitespace for checking comments and backslashes, it would be better to consistently use the trimmed version for all lines.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func SanitizeCommand(cmdStr string) ([]string, error) {
// Remove trailing backslashes
cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ")
cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ")
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
cleanedLines = append(cleanedLines, line)
}
}
// put it back together
cmdStr = strings.Join(cleanedLines, "\n")
func SanitizeCommand(cmdStr string) ([]string, error) {
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
- cleanedLines = append(cleanedLines, line)
+ cleanedLines = append(cleanedLines, trimmed)
}
}
// put it back together
cmdStr = strings.Join(cleanedLines, "\n")
🤖 Prompt for AI Agents
In proxy/config.go around lines 231 to 249, the SanitizeCommand function
inconsistently handles whitespace by appending trimmed lines for continuation
lines but original lines for others. To fix this, always use the trimmed version
of each line when appending to cleanedLines, ensuring consistent whitespace
handling throughout the function.

@mostlygeek mostlygeek merged commit afc9aef into main May 15, 2025
3 checks passed
mostlygeek added a commit that referenced this pull request May 15, 2025
@mostlygeek mostlygeek deleted the comments-in-cmd branch May 15, 2025 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant