fix: read user-dirs.dirs as shell assignments, not INI - #545
Open
darrenhuai wants to merge 4 commits into
Open
Conversation
_get_user_dirs_folder fed ~/.config/user-dirs.dirs to ConfigParser under a fake [top] section. The file is a shell fragment written by xdg-user-dirs-update, and the reference reader in xdg-user-dirs (xdg-user-dir-lookup.c) scans it line by line, so the two disagree in ways that either crash or hand back a wrong directory: a second assignment to the same key raises DuplicateOptionError, any line that is not key=value raises ParsingError, an indented assignment is swallowed as a continuation of the previous value, text after the closing quote stays in the path, and the backslash escapes xdg-user-dirs-update writes for $, `, " and \ are kept literally. tox-dev#542 turned interpolation off to stop percent signs breaking it, which fixed one symptom of the same mismatch. Replace the parser with a line matcher that does what the C reader does: the last assignment wins, escapes inside the quotes are undone, anything after the closing quote is ignored, and a value that is neither $HOME-relative nor absolute is skipped so an earlier valid line still counts. Unquoted values are still accepted, as before, since Debian's xdg-user-dir is a sh script that sources the file and accepts them too.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_get_user_dirs_folderparses~/.config/user-dirs.dirswithConfigParserunder a fake[top]section. The file is a shell fragment written byxdg-user-dirs-update, and the reference reader in xdg-user-dirs (xdg-user-dir-lookup.c) scans it line by line, so the two disagree on files that are perfectly normal for that tool:user-dirs.dirscontainsmainxdg-user-dirDuplicateOptionErrorkey=valueParsingErrorXDG_DOCUMENTS_DIR="$HOME/Docs" # was "$HOME/Old"~/Docs" # was "$HOME/Old~/DocsXDG_DOCUMENTS_DIR="$HOME/My \"Docs\""(xdg-user-dirs-updateescapes$,`,"and\)~/My \"Docs\"~/My "Docs"#542 turned interpolation off to stop
%breaking it, which fixed one symptom of the same mismatch.This replaces the parser with a line matcher that does what the C reader does: the last assignment wins, escapes inside the quotes are undone, anything after the closing quote is ignored, and a value that is neither
$HOME-relative nor absolute is skipped so an earlier valid line still counts. Unquoted values are still accepted, as before; Debian'sxdg-user-diris ashscript that sources the file and accepts them too.I ran the new cases through Ubuntu's
xdg-user-dirand GLib'sg_get_user_special_diron the same files. Wherever those two agree, this now agrees with them; where they differ, it follows the C source. Existing tests pass on Windows and Linux.