Skip to content

Fix line navigation when opening files with line fragments#293733

Open
MrToph wants to merge 1 commit intomicrosoft:mainfrom
MrToph:feature/fix-line-navigation-when-opening-files
Open

Fix line navigation when opening files with line fragments#293733
MrToph wants to merge 1 commit intomicrosoft:mainfrom
MrToph:feature/fix-line-navigation-when-opening-files

Conversation

@MrToph
Copy link

@MrToph MrToph commented Feb 8, 2026

This PR fixes two line-navigation regressions for file links that include a fragment like #L99.

Related issue: #170405

Rationale

File deep-linking is increasingly important with external AI coding tools that use VS Code as the editor/diff viewer. In these workflows, opening a file at an exact location (and, in related scenarios, specific ranges) is core to review and iteration speed. Preserving URI fragments such as #L99 or #L99-L105 avoids making users manually browse large files and lose the exact location being referenced.

Problem summary

VS Code supports opening file links with line fragments (e.g. #L99), but the fragment was getting dropped in two important paths:

  1. Inside VS Code when clicking a relative file:// link.
  2. Outside VS Code when opening via protocol URL (vscode://file/...#L99 or code-oss://file/...#L99).

Reproduction matrix (with concrete paths)

Target file for all cases:

  • /tmp/vscode-link-repro/target.txt

Contents:

Line 1 ...
Line 2 ...
<continues 100 times>

Source file used for in-editor link clicks:

  • /tmp/vscode-link-repro/source.txt

Contents:

relative: file://./target.txt#L99
absolute: file:///tmp/vscode-link-repro/target.txt#L99

Cases:

  1. Inside VS Code × relative file://
  • Link: file://./target.txt#L99 (from /tmp/vscode-link-repro/source.txt)
  • Before: opens file, does not go to line 99
  • After this PR: opens file and goes to line 99
  1. Inside VS Code × absolute file://
  • Link: file:///tmp/vscode-link-repro/target.txt#L99
  • Before: goes to line 99
  • After: unchanged (still works)
  1. Outside VS Code (macOS open) × vscode:// URL
  • Code - OSS command:
    • open -b com.visualstudio.code.oss -u "code-oss://file/tmp/vscode-link-repro/target.txt#L99"
  • VS Code stable command:
    • open -b com.microsoft.VSCode -u "vscode://file/tmp/vscode-link-repro/target.txt#L99"
  • Before: opens file, does not go to line 99
  • After this PR: opens file and goes to line 99
  1. Outside VS Code (macOS open) × file:// URL
  • Command:
    • open -b com.visualstudio.code.oss -u "file:///tmp/vscode-link-repro/target.txt#L99"
  • Behavior: opens file, does not go to line 99
  • Status: not fixable in VS Code. macOS delivers this as open-file with a filesystem path and strips the URI fragment (#L99) before VS Code receives it.

What changed

  1. Editor link handling (src/vs/editor/contrib/links/browser/links.ts)
  • Preserve query/fragment when resolving relative file://./... links against the active model URI.
  • This fixes in-editor relative links losing #L....
  1. Protocol URL handling in main process (src/vs/code/electron-main/app.ts)
  • Preserve query and fragment when converting protocol file URLs into fileUri openables.
  1. Openable resolution (src/vs/platform/windows/electron-main/windowsMainService.ts)
  • Extract line selection from URI fragment (e.g. #L99) before resolving local file path.
  • Carry selection into editor open options, while still using filesystem path for file existence checks.

Tests

Added regression tests:

  • src/vs/editor/contrib/links/test/browser/linkDetectorUtils.test.ts

Validated:

  • Relative file://./...#L99 now preserves fragment and resolves to line selection.
  • Existing absolute in-editor behavior remains unchanged.

@vs-code-engineering
Copy link

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@bpasero

Matched files:

  • src/vs/code/electron-main/app.ts
  • src/vs/platform/windows/electron-main/windowsMainService.ts

@deepak1556

Matched files:

  • src/vs/code/electron-main/app.ts

@MrToph
Copy link
Author

MrToph commented Feb 8, 2026

@microsoft-github-policy-service agree

/**
* Resolve file links of the shape `file://./relativeFile.txt` against the model URI.
*/
export function resolveRelativeFileLink(uri: string | URI, modelUri: URI): string | URI {
Copy link
Author

Choose a reason for hiding this comment

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

this is a refactor of the old code from src/vs/editor/contrib/links/browser/links.ts, I kept it functionally equivalent except the final with query + fragment.

}

private async resolveOpenable(openable: IWindowOpenable, options: IPathResolveOptions = Object.create(null)): Promise<IPathToOpen | undefined> {
private async resolveOpenable(openable: IWindowOpenable, options: IPathResolveOptions = Object.create(null)): Promise<IPathToOpen<ITextEditorOptions> | undefined> {
Copy link
Author

Choose a reason for hiding this comment

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

had to specify this because of a type error when using options.selection below.

@bpasero bpasero self-assigned this Feb 8, 2026
@bpasero bpasero added this to the Backlog milestone Feb 8, 2026
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