Skip to content

Fix OSError when saving CSS files whose URL has a query string - #58

Open
agu2347 wants to merge 1 commit into
peterbe:masterfrom
agu2347:fix-css-filename-query-string-issue-53
Open

Fix OSError when saving CSS files whose URL has a query string#58
agu2347 wants to merge 1 commit into
peterbe:masterfrom
agu2347:fix-css-filename-query-string-issue-53

Conversation

@agu2347

@agu2347 agu2347 commented Aug 24, 2026

Copy link
Copy Markdown

Problem

run() derives the local output filename for each downloaded/minified CSS file directly from the URL:

orig_name = link.href.split('/')[-1]

If the URL includes a query string (e.g. a cache-busting param like ?v=1516052760.0, common on production sites), that query string ends up as part of the filename. On Windows, ? and = are not valid filename characters, so io.open() raises:

OSError: [Errno 22] Invalid argument: '...\\banner-styles.css?v=1516052760.0'

Fixes #53.

Fix

Added a filename_from_href() helper that parses the URL properly with urllib.parse.urlparse() and uses only os.path.basename(parsed.path), dropping the query string and fragment. Both the orig_name and before_name filename computations in run() now go through this helper.

Testing

Added tests/test_main.py (plain unittest, since the existing tests/test_mincss.py depends on nose, which is unmaintained/unavailable on current Python) with 4 cases covering: a URL with a query string (reproducing the exact reported bug), a query string + fragment, a plain URL (no regression), and a relative path with a query string.

Confirmed the new test fails against the pre-fix logic (reproducing the exact query-string-retained-in-filename defect) and passes after the fix.

link.href.split('/')[-1] was used directly as the local output
filename, which includes the URL's query string verbatim (e.g.
'?v=1516052760.0'). Characters like '?' and '=' are invalid in
Windows filenames, causing OSError: [Errno 22] Invalid argument
when io.open() tried to create the file.

Add filename_from_href() which parses the URL and uses only the
path's basename, dropping the query string and fragment.

Fixes peterbe#53
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.

mincss crashes when stylesheet links have query strings

1 participant