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

[v3] Normalize Windows paths on system Dialogs invocations #4188

Open
wants to merge 5 commits into
base: v3-alpha
Choose a base branch
from

Conversation

TheGB0077
Copy link

@TheGB0077 TheGB0077 commented Apr 7, 2025

Description

JS code ends up escaping Windows paths when data gets passed around from backend to the frontend, so those should be normalized beforehand.

Type of change

Please select the option that is relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

If you checked Linux, please specify the distro and version.

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • Bug Fixes

    • Improved file dialog reliability by enhancing error handling.
    • Ensured file paths are consistently cleaned, resulting in a more robust file selection experience.
    • Fixed Dialogs runtime function returning escaped paths on Windows.
  • Tests

    • Introduced new tests for validating path cleaning functionality in Windows environments.

JS code ends up escaping Windows paths when data gets passed around from backend to the frontend, so those should be normalized beforehand.
Copy link
Contributor

coderabbitai bot commented Apr 7, 2025

Walkthrough

This pull request enhances error handling and result processing in the dialogs_windows.go file for file dialogs. The function showCfdDialog now includes error checks after invoking both multi-select and single-select dialog functions. It also processes and cleans the resulting file paths using filepath.Clean before returning them, thereby improving the robustness of dialog operations. Additionally, a new test file has been introduced to validate the cleaning of file paths.

Changes

File Change Summary
v3/.../dialogs_windows.go Enhanced error handling by adding checks after multi.ShowAndGetResults() and dlg.ShowAndGetResult(). Cleaned file paths using filepath.Clean().
docs/.../changelog.mdx Added entry for fixing the Dialogs runtime function that was returning escaped paths on Windows.
v3/.../dialogs_windows_test.go Introduced a new test file with TestCleanPath function to validate filepath.Clean for various input scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller
    participant Dialog as Dialog Function
    participant Multi as multi.ShowAndGetResults
    participant Single as dlg.ShowAndGetResult
    participant Cleaner as filepath.Clean

    Caller->>Dialog: Invoke showCfdDialog(newDlg, isMultiSelect)
    alt Multi-select enabled
        Dialog->>Multi: Call multi.ShowAndGetResults()
        Multi-->>Dialog: Return paths OR error
        alt Error Occurs
            Dialog-->>Caller: Return nil, error
        else
            Dialog->>Cleaner: Clean each path in paths
            Cleaner-->>Dialog: Cleaned paths
            Dialog-->>Caller: Return cleaned paths, no error
        end
    else
        Dialog->>Single: Call dlg.ShowAndGetResult()
        Single-->>Dialog: Return path OR error
        alt Error Occurs
            Dialog-->>Caller: Return nil, error
        else
            Dialog->>Cleaner: Clean the path
            Cleaner-->>Dialog: Cleaned path
            Dialog-->>Caller: Return cleaned path, no error
        end
    end
Loading

Suggested reviewers

  • leaanthony

Poem

I hopped through lines of code today,
Caught errors that tried to scamper away.
Every path now gleams so clean,
Like polished carrots, crisp and green.
With each fix my happiness swells—
A rabbit’s cheer in robust code dwells!
🐇✨

✨ 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.
    • 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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

🧹 Nitpick comments (1)
docs/src/content/docs/changelog.mdx (1)

114-114: Typographical Error in Changelog Entry.
The entry on line 114 reads "Fixed Dialogs rutime function returning escaped paths on Windows". Please correct "rutime" to "runtime" for clarity and accuracy in documentation.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f6750c3 and e62c0b0.

📒 Files selected for processing (1)
  • docs/src/content/docs/changelog.mdx (1 hunks)

@leaanthony
Copy link
Member

Thanks for taking the time to open this. Could we extract this processing into a function and write a test to make sure it does what we think it does? I just want to make sure that Clean is the function we want to use. Thanks 🙏

Copy link

sonarqubecloud bot commented Apr 9, 2025

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

🧹 Nitpick comments (3)
v3/pkg/application/dialogs_windows_test.go (3)

12-57: Test cases cover main Windows path scenarios but could be expanded.

The table-driven test approach is well-structured and covers several Windows path edge cases. However, consider adding a few additional test cases for completeness:

  1. Paths with spaces (common in Windows)
  2. Paths with dot and double-dot elements (e.g., C:\folder\.\file.txt or C:\folder\..\other\file.txt)
  3. Paths with environment variables or Windows-specific prefixes (e.g., \\?\C:\ for long paths)
	}{
		{
			name:      "path with double separators",
			inputPath: `C:\\temp\\folder`,
			expected:  `C:\temp\folder`,
		},
		{
			name:      "path with forward slashes",
			inputPath: `C://temp//folder`,
			expected:  `C:\temp\folder`,
		},
		{
			name:      "path with trailing separator",
			inputPath: `C:\\temp\\folder\\`,
			expected:  `C:\temp\folder`,
		},
		{
			name:      "path with escaped tab character",
			inputPath: `C:\\Users\\test\\tab.txt`,
			expected:  `C:\Users\test\tab.txt`,
		},
		{
			name:      "newline character",
			inputPath: `C:\\Users\\test\\newline\\n.txt`,
			expected:  `C:\Users\test\newline\n.txt`,
		},
		{
			name:      "UNC path with multiple separators",
			inputPath: `\\\\\\\\host\\share\\test.txt`,
			expected:  `\\\\host\share\test.txt`,
		},
+		{
+			name:      "path with spaces",
+			inputPath: `C:\\Program Files\\My App\\file.txt`,
+			expected:  `C:\Program Files\My App\file.txt`,
+		},
+		{
+			name:      "path with dot element",
+			inputPath: `C:\\folder\\.\\file.txt`,
+			expected:  `C:\folder\file.txt`,
+		},
+		{
+			name:      "path with double-dot element",
+			inputPath: `C:\\folder\\..\\other\\file.txt`,
+			expected:  `C:\other\file.txt`,
+		},
	}

35-38: Misleading test case name for "path with escaped tab character".

This test case doesn't actually contain an escaped tab character (\t) in the path. It's just testing a path with a file named "tab.txt". Consider renaming this test case to better reflect what's being tested.

		{
-			name:      "path with escaped tab character",
+			name:      "path with word 'tab' in filename",
			inputPath: `C:\\Users\\test\\tab.txt`,
			expected:  `C:\Users\test\tab.txt`,
		},

40-43: Misleading test case name for "newline character".

Similar to the previous comment, this test case doesn't contain an actual newline character (\n) in the path. It's testing a path with a file named "newline\n.txt". Consider renaming this test case to better reflect what's being tested.

		{
-			name:      "newline character",
+			name:      "path with 'newline\\n' in filename",
			inputPath: `C:\\Users\\test\\newline\\n.txt`,
			expected:  `C:\Users\test\newline\n.txt`,
		},
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1b00dc9 and f46352d.

📒 Files selected for processing (1)
  • v3/pkg/application/dialogs_windows_test.go (1 hunks)
🔇 Additional comments (2)
v3/pkg/application/dialogs_windows_test.go (2)

1-10: The build constraint and package setup looks good.

This test file is correctly configured to run only on Windows, which is appropriate given the purpose of normalizing Windows paths. The imports are minimal and appropriate for a test file.


12-57:

✅ Verification successful

Verify that filepath.Clean is the appropriate function for this use case.

Based on the PR objectives and comment from leaanthony, ensure that filepath.Clean is the appropriate function for normalizing Windows paths. Your test confirms the behavior of filepath.Clean, but you might want to consider if there are any edge cases specific to your application's dialog paths that might not be handled correctly by this function.


🏁 Script executed:

#!/bin/bash
# Look for other path normalization functions in the codebase to confirm the approach
rg "func\s+.*[Nn]ormalize.*[Pp]ath" --type go
rg "Clean\s*\(" --type go -A 2 -B 2 | grep -i path

Length of output: 2479


Action: Confirmed appropriate use of filepath.Clean

The test cases in v3/pkg/application/dialogs_windows_test.go confirm that filepath.Clean behaves as expected for Windows paths, and a scan of the codebase shows its widespread use for similar normalization tasks. There don’t appear to be any edge cases specific to our dialog paths that require a different function. Continue using filepath.Clean while keeping an eye out for any future path-specific nuances.

@TheGB0077
Copy link
Author

Thanks for taking the time to open this. Could we extract this processing into a function and write a test to make sure it does what we think it does? I just want to make sure that Clean is the function we want to use. Thanks 🙏

Hey there! I started to tackle this, mocking the function within the test file, but ultimately I found it was more straightforward to test the function directly with the sort of data that would cause problems once passed through the JavaScript boundary.

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