fix: catch ArgumentException from Path.IsPathRooted in IsLocalServerPath#746
Conversation
Path.IsPathRooted throws ArgumentException when the package source is a remote URL containing characters illegal in file paths. Catch the exception and return false since remote URLs are not local paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideWraps Path.IsPathRooted() in IsLocalServerPath() with a try/catch so that invalid path strings (like remote URLs) no longer throw ArgumentException and instead are treated as non-local paths, while preserving existing handling for file:// URLs. Sequence diagram for IsLocalServerPath error handling in MCP window initializationsequenceDiagram
participant MCPWindow
participant TryBuildCommand
participant ShouldForceUvxRefresh
participant AssetPathUtility
MCPWindow->>TryBuildCommand: Execute()
TryBuildCommand->>ShouldForceUvxRefresh: ShouldForceUvxRefresh()
ShouldForceUvxRefresh->>AssetPathUtility: IsLocalServerPath()
alt fromUrl starts with file://
AssetPathUtility-->>ShouldForceUvxRefresh: return true
else fromUrl is candidate local path
AssetPathUtility->>AssetPathUtility: Path.IsPathRooted(fromUrl)
AssetPathUtility-->>ShouldForceUvxRefresh: return result
else fromUrl is remote URL with illegal path chars
AssetPathUtility->>AssetPathUtility: Path.IsPathRooted(fromUrl)
AssetPathUtility-->>AssetPathUtility: catch ArgumentException
AssetPathUtility-->>ShouldForceUvxRefresh: return false
end
ShouldForceUvxRefresh-->>TryBuildCommand: bool shouldRefresh
TryBuildCommand-->>MCPWindow: build result (no crash)
Flow diagram for updated IsLocalServerPath logicflowchart TD
A[IsLocalServerPath fromUrl input] --> B{fromUrl is null or empty}
B -- yes --> C[Return false]
B -- no --> D{fromUrl starts with file://}
D -- yes --> E[Return true]
D -- no --> F[Try Path.IsPathRooted fromUrl]
F --> G{Path.IsPathRooted succeeds}
G -- yes --> H[Return result of Path.IsPathRooted]
G -- no (ArgumentException) --> I[Return false]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Instead of relying on catching
ArgumentExceptionfromPath.IsPathRooted, consider usingUri.TryCreate(fromUrl, UriKind.Absolute, out var uri)and checkinguri.IsFileto distinguish local file paths from remote URLs in a more explicit and exception-free way. - If you keep the exception-based approach, it may be worth adding a small helper method (e.g.
IsRootedPathSafe(string path)) so the try/catch and its rationale are encapsulated and easier to reuse or adjust later.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of relying on catching `ArgumentException` from `Path.IsPathRooted`, consider using `Uri.TryCreate(fromUrl, UriKind.Absolute, out var uri)` and checking `uri.IsFile` to distinguish local file paths from remote URLs in a more explicit and exception-free way.
- If you keep the exception-based approach, it may be worth adding a small helper method (e.g. `IsRootedPathSafe(string path)`) so the try/catch and its rationale are encapsulated and easier to reuse or adjust later.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Summary
Path.IsPathRooted()throwsArgumentExceptionwhen the package source URL contains characters illegal in file paths (e.g. remote PyPI URLs with://)IsLocalServerPath()→ShouldForceUvxRefresh()→TryBuildCommand(), preventing the MCP window from initializingfalsefor non-path strings, since remote URLs are not local pathsTest plan
file://prefix) still correctly detected🤖 Generated with Claude Code
Summary by Sourcery
Bug Fixes:
Summary by CodeRabbit