question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to always open files at their canonical path

See original GitHub issue

Problem

Symlinks can sometimes lead to the same files being opened more than once in VSCode. An example in which this happens is in a bazel run sandbox, which symlinks files together into a sandbox environment. Another example I’ve seen is when python modules (spread over various folders in a git repo) are symlinked into 1 virtual env python path tree. More generally speaking, symlinks are sometimes used by build tooling to create one consolidated environment out of files spread over different places.

Unfortunately, this can lead to a poor user experience in VS Code. Debuggers can start opening multiple copies of the same file (the one you’d like to edit, which likely is under source control, and one at the path at which it appears in the sandbox) and the UI/UX experience is clumsy at best (breakpoints can be set in each copy of the file and actually work for both, but visually aren’t synced). Language servers like pylance can resolve imports to the symlinked venv. So opening the definition of a function can open the symlinked copy, rather than the original (which is likely under source control and the one that you’d actually like to edit)… Again: clumsy UI/UX, such as that that symlinked copy doesn’t show source control change info (lines added/rm/changed, gitlens).

Proposed solution

I’ve seen various tickets related to this and generally things seem to get stuck in the discussion on defining a solution. I’d like to take another attempt at that.

How about we introduce a boolean option openFilesAtCanonicalPath (which people can set on a user or a workspace level)?

The “canonical path” would be the path that readlink -f {some_path} would give on linux: i.e. the path with all symlinks resolved. This is the ‘original’ path for the file. This would:

  1. Make sure there’s always only 1 copy of a file opened
  2. Have the file opened at the path that the user is most likely interested in. If it’s part of the workspace, then this would be the path that is under version control (and all things like git/gitlens would work). If it’s not part of the workspace, so be it, it’s likely some system level dependency or so, still you’d be looking at the original file, which is great).

I wonder what VS Code devs thinks of this.

Related tickets: https://github.com/microsoft/pylance-release/issues/478, https://github.com/microsoft/vscode/issues/34627, https://github.com/microsoft/vscode/issues/100533, https://github.com/microsoft/vscode-python/discussions/15897

Final remarks

We could even expand this idea to extensions if desired (extensions could retrieve both the requested path and canonical path of a file through extensions api), though I think many of the discussions around better extension support for symlinked files are maybe already solved by just opening the canonical file instead.

Please note that I’m focussing purely on this problem as introduced by symlinks in macOS and Linux. I’m not sure about Windows or other types of links like hard links, macOS aliases, etc. So far, I’ve seen build tooling use symlinks for these kind of things so addressing that can hopefully solve the majority of issues out there.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:58
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

18reactions
vscode-triage-botcommented, Aug 4, 2021

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

12reactions
jll63commented, Mar 25, 2022

This has not been fixed yet? This is so annoying.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to allow only opening files in current directory in Python3?
Convert the filename to a canonical path using os.path.realpath , get the directory portion, and see if the current directory (in canonical ...
Read more >
Java File Path, Absolute Path and Canonical Path | DigitalOcean
If the file object is created using a relative path, the absolute pathname is resolved in a system-dependent way.
Read more >
File path formats on Windows systems | Microsoft Learn
In this article, learn about file path formats on Windows systems, such as traditional DOS paths, DOS device paths, and universal naming ...
Read more >
File and Directory Names: File, Path, Paths (Java Files Tutorial)
As seen in the table, absolute paths on Windows start with a drive letter and a colon; directories are separated by a backslash...
Read more >
Adobe Flash Platform * Working with File objects in AIR
Finding the relative path between two files ... The File class adds properties and methods that are not exposed in Flash Player (in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found