Skip to content

fix: don't truncate filenames containing "?" or "#" in local paths - #227

Closed
rahul05ranjan wants to merge 1 commit into
fabiocaccamo:mainfrom
rahul05ranjan:fix/issue-226
Closed

rahul05ranjan wants to merge 1 commit into
fabiocaccamo:mainfrom
rahul05ranjan:fix/issue-226

Conversation

@rahul05ranjan

Copy link
Copy Markdown
Contributor

What Problem This Solves

get_filename ran every input through urllib.parse.urlsplit, so the characters ? and # — legal in filenames on Linux and macOS — were treated as URL query/fragment delimiters even for local paths, silently truncating the filename and dropping its extension:

fsutil.get_filename("/tmp/file?v1.txt")   # -> 'file'      (expected 'file?v1.txt')
fsutil.split_filename("/tmp/file#2.txt") # -> ('file', '') (expected ('file#2', 'txt'))

Because operations like move_file, rename_file, rename_file_extension and transform_filepath build on get_filename, moving file?v1.txt actually renames the file to file, losing the extension.

Why This Change Was Made

urlsplit is now applied only when the input actually parses as a URL with a scheme; plain filesystem paths are handled by os.path.basename only. URL behavior is unchanged (e.g. get_filename("https://domain.com/Document.txt?p=1") == "Document.txt" still holds, including when the string has already been normalized by callers like split_filename).

User Impact

  • Files whose names contain ? or # keep their name and extension through move_file / rename_* / transform_filepath.
  • No behavior change for URLs or for ordinary paths without those characters.

Evidence

New assertions in test_get_filename and the new regression test test_move_file_with_special_chars_in_filename fail on main and pass with this change; the full test suite passes (the two umask-dependent failures on the dev machine are pre-existing and unrelated).

Fixes #226

get_filename applied urlsplit to every input, so "?" and "#"
were treated as URL query/fragment delimiters even for filesystem
paths, truncating the filename and dropping its extension.

Apply urlsplit only when the input parses as a URL with a scheme,
so local paths keep their "?" / "#" characters while URL handling
remains unchanged.

Fixes fabiocaccamo#226

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.52%. Comparing base (5c88544) to head (92613e2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #227   +/-   ##
=======================================
  Coverage   98.52%   98.52%           
=======================================
  Files          13       13           
  Lines         812      815    +3     
=======================================
+ Hits          800      803    +3     
  Misses         12       12           
Flag Coverage Δ
unittests 98.52% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fabiocaccamo

Copy link
Copy Markdown
Owner

Closing as duplicate of #204. This approach also misdetects Windows drive paths as URLs (C:/x/file#1.txt yields scheme c and filename file). Thanks!

@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Understood — thanks for the pointer to #204, and fair point on the Windows drive-path misdetection; parsing a drive as a scheme is exactly the kind of case the URL-marker approach shouldn't have to guess. I'll follow #204 for the direction you prefer here.

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.

get_filename truncates local paths containing "?" or "#" (query/fragment chars misapplied to filesystem paths)

2 participants