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

Fix offset regression in wshremote #1807

Merged
merged 4 commits into from
Jan 23, 2025
Merged

Fix offset regression in wshremote #1807

merged 4 commits into from
Jan 23, 2025

Conversation

esimkowitz
Copy link
Member

No description provided.

Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Walkthrough

The pull request introduces significant modifications to the RemoteWriteFileCommand method within the pkg/wshrpc/wshremote/wshremote.go file. The changes focus on the handling of file writing options, specifically the truncate, append, and atOffset parameters derived from the data input. New variables for these options are initialized based on data.Info, with added validation to prevent the use of non-zero offsets when truncate or append options are specified, returning an error if violated. The createMode variable is simplified to default to 0644 if unspecified. The logic for determining the offset now checks if atOffset exceeds the current file size, returning an error if it does. The file opening flags are adjusted based on the options, and the writing logic uses atOffset directly. The method signature remains unchanged, but the internal logic has been extensively modified.

Additionally, in frontend/app/view/preview/preview.scss, a new nested style rule for the .markdown class within the .view-preview-markdown class has been added, which includes a margin property set to 10px 5px, enhancing the layout of markdown content.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between edc905a and 9d32f24.

📒 Files selected for processing (1)
  • frontend/app/view/preview/preview.scss (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Build for TestDriver.ai
🔇 Additional comments (1)
frontend/app/view/preview/preview.scss (1)

16-18: LGTM! Consistent spacing implementation.

The added margin (10px vertical, 5px horizontal) aligns well with existing spacing patterns in the stylesheet, providing appropriate content separation while maintaining visual consistency with other preview types.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link
Contributor

@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: 0

🔭 Outside diff range comments (1)
pkg/wshrpc/wshremote/wshremote.go (1)

Critical file operation changes lack test coverage

The verification confirms there are no tests for the RemoteWriteFileCommand functionality. Given that this code handles critical file operations where incorrect offset handling could lead to data corruption, comprehensive test coverage is essential.

Required test scenarios:

  • Writing to a new file (offset = 0)
  • Writing at a specific offset (data.At.Offset > 0)
  • Writing with bounds checking (offset > fileSize)
  • Writing with nil data.At (default behavior)
🔗 Analysis chain

Line range hint 741-746: The offset initialization change looks correct but needs test coverage.

The change to initialize offset to 0 by default and only update it based on data.At is the right approach, as it ensures data is written at the beginning of the file unless explicitly specified otherwise. This fixes the regression where data was being appended by default.

However, I notice that there's no test coverage for this critical file writing behavior in the codebase.

Let's verify the test coverage:

Would you like me to help create comprehensive test cases that cover:

  1. Writing to a new file (offset = 0)
  2. Writing at a specific offset (data.At.Offset > 0)
  3. Writing with append mode
  4. Writing with truncate mode
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for tests related to RemoteWriteFileCommand
rg -l "RemoteWriteFileCommand.*Test|Test.*RemoteWriteFileCommand" 

Length of output: 67


Script:

#!/bin/bash
# Look for all test files in and around the wshremote directory
echo "=== Test files in wshremote directory ==="
fd ".*_test\.go$" pkg/wshrpc/wshremote/

echo -e "\n=== Content of test files mentioning file operations ==="
rg -l "TestRemote.*File|Test.*FileOperation" pkg/wshrpc/

echo -e "\n=== Check if wshremote_test.go exists and show its content ==="
if [ -f pkg/wshrpc/wshremote/wshremote_test.go ]; then
    echo "Found wshremote_test.go:"
    cat pkg/wshrpc/wshremote/wshremote_test.go
fi

Length of output: 515

🧹 Nitpick comments (2)
pkg/wshrpc/wshremote/wshremote.go (2)

Line range hint 741-746: Consider documenting the append mode behavior with offsets.

When append mode is active (data.Info.Opts.Append), the calculated offset is ignored since the data is always appended to the end of the file. This behavior should be documented to prevent confusion for API consumers.

Add a comment explaining this interaction:

 offset := 0
 if data.At != nil {
     if data.At.Offset > 0 {
         offset = min(data.At.Offset, fileSize)
     }
 }
+// Note: When append mode is active, the offset is ignored and data is always appended
 if offset > 0 && !data.Info.Opts.Append {
     n, err = file.WriteAt(dataBytes[:n], offset)
 } else {
     n, err = file.Write(dataBytes[:n])
 }

Also applies to: 754-756, 765-767


Line range hint 765-769: Enhance error messages with operation context.

The error messages could be more descriptive by including the operation type (write at offset vs. append).

 if offset > 0 && !data.Info.Opts.Append {
     n, err = file.WriteAt(dataBytes[:n], offset)
+    if err != nil {
+        return fmt.Errorf("cannot write at offset %d to file %q: %w", offset, path, err)
+    }
 } else {
     n, err = file.Write(dataBytes[:n])
+    if err != nil {
+        if data.Info.Opts.Append {
+            return fmt.Errorf("cannot append to file %q: %w", path, err)
+        }
+        return fmt.Errorf("cannot write to file %q: %w", path, err)
+    }
 }
-if err != nil {
-    return fmt.Errorf("cannot write to file %q: %w", path, err)
-}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 50b80a1 and 8bbc612.

📒 Files selected for processing (1)
  • pkg/wshrpc/wshremote/wshremote.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Build for TestDriver.ai
  • GitHub Check: Analyze (go)

Copy link
Contributor

@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

🧹 Nitpick comments (4)
pkg/wshrpc/wshremote/wshremote.go (4)

737-739: Document the default file mode choice.

Consider adding a comment explaining why 0644 was chosen as the default file mode (standard Unix file permissions for user-writable, world-readable files).

+// Default to 0644: User can read/write, others can read
 createMode := os.FileMode(0644)

755-756: Consider allowing writes at exactly fileSize.

The current check prevents writing at an offset equal to the file size, which might be a valid use case for appending data at the end of the file.

-if atOffset > fileSize {
+if atOffset > fileSize {
   return fmt.Errorf("cannot write at offset %d, file size is %d", atOffset, fileSize)
 }

771-774: Enhance error handling with specific error messages.

Consider providing more specific error messages for different types of write failures.

 if atOffset > 0 && !append {
-  n, err = file.WriteAt(dataBytes[:n], atOffset)
+  n, err = file.WriteAt(dataBytes[:n], atOffset)
+  if err != nil {
+    return fmt.Errorf("failed to write at offset %d: %w", atOffset, err)
+  }
 } else {
-  n, err = file.Write(dataBytes[:n])
+  n, err = file.Write(dataBytes[:n])
+  if err != nil {
+    return fmt.Errorf("failed to write to file: %w", err)
+  }
 }
-if err != nil {
-  return fmt.Errorf("cannot write to file %q: %w", path, err)
-}

779-779: Enhance log message with operation mode.

Consider including the operation mode (truncate/append) in the log message for better debugging.

-log.Printf("wrote %d bytes to file %q at offset %d\n", n, path, atOffset)
+log.Printf("wrote %d bytes to file %q at offset %d (truncate=%v, append=%v)\n", n, path, atOffset, truncate, append)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9820f2f and edc905a.

📒 Files selected for processing (1)
  • pkg/wshrpc/wshremote/wshremote.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Build for TestDriver.ai
  • GitHub Check: Analyze (go)
🔇 Additional comments (2)
pkg/wshrpc/wshremote/wshremote.go (2)

727-732: LGTM! Clear validation of mutually exclusive options.

The validation ensures that truncate and append options cannot be used with a non-zero offset, preventing conflicting file operations.


759-762: LGTM! Correct file open flags for each operation mode.

The flags are properly set based on the truncate and append options, following standard Go file handling practices.

@esimkowitz esimkowitz merged commit 9406967 into main Jan 23, 2025
8 checks passed
@esimkowitz esimkowitz deleted the esimkowitz-patch-2 branch January 23, 2025 06:24
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.

2 participants