Skip to content

ValueError from min() when the file is not under an open folder (or is on another drive) #4

Description

@dpc00

"Copy Relative Path" raises ValueError and copies nothing when the current file is not inside an open folder of the window (e.g. a single file opened on its own), or when the file and the folder are on different drives on Windows.

sublime.set_clipboard(
    min(
        (relpath(filename, folder) for folder in sublime.active_window().folders()),
        key=len,
    )
)

Reproduced with the package code (Sublime stubbed, no folders):

ValueError: min() iterable argument is empty

is_enabled() only checks that the view has a file name, so the command is offered and then fails silently (traceback in the console, no status message). With a folder on another drive, os.path.relpath raises ValueError: path is on mount 'C:', start on mount 'D:'.

Fix: skip folders that don't contain the file (catch ValueError), and fall back to the absolute path (or a status message) when the list is empty: min(candidates, key=len, default=filename).

Found while testing packages from the Package Control catalog.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions