Skip to content

uv-fs: transparently support reading UTF-16 files - #2283

Merged
BurntSushi merged 2 commits into
mainfrom
ag/read-utf16
Mar 8, 2024
Merged

BurntSushi merged 2 commits into
mainfrom
ag/read-utf16

Conversation

@BurntSushi

Copy link
Copy Markdown
Member

This PR tweaks uv to support reading requirements.txt regardless of
whether it is encoded as UTF-8 or UTF-16. This is particularly relevant
on Windows where uv pip freeze > requirements.txt will likely write a
UTF-16 encoded requirements.txt file.

There is some discussion on #1666 where it's suggested that perhaps
we should explicitly not support this. I didn't see that until I
had already put this PR together, but even so, I think it's worth
considering this. UTF-16 is predominant on Windows. It is very easy
to produce a UTF-16 encoded file. Moreover, there is an easy and well
specified way to recognize and transcode UTF-16 encoded data to UTF-8.

I think the downside of this is that it could encourage the use UTF-16
encoded requirements.txt files in addition to UTF-8 encoded
files, and it would probably be nice to converge and standardize on
one encoding. One possible alternative to this PR is that we provide
a better error message. Another alternative is to ensure that a
-o/--output flag exists for all commands (neither uv pip freeze nor
pip freeze have such a flag) so that users can always write output
to a file without relying on their environment's piping behavior.
(Although this last alternative seems a little sad to me.)

It's also worth noting the PEP-0508 doesn't seem to mention file
encoding at all. So I think from a "do the standards allow this"
perspective, this change is OK.

Finally, pip itself seems to work with UTF-16 encoded
requirements.txt files.

I think I personally overall lean towards supporting UTF-16 for
requirements.txt files. In part because I think it smoothes out the
UX a little bit, in part because there is no obvious specification
(that I'm aware of) that mandates that these files are UTF-8, and
finally in part because pip supports it too.

Fixes #1666, Fixes #2276

@BurntSushi

Copy link
Copy Markdown
Member Author

cc @zanieb @mitsuhiko given discussion in #1666 about supporting UTF-16 encoded requirements.txt files.

cc @yashranjan1 per request.

@zanieb zanieb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I agree with your principles

@BurntSushi
BurntSushi requested review from konstin and mitsuhiko March 7, 2024 18:21
@charliermarsh

Copy link
Copy Markdown
Member

I think it's wise to support these as long as PowerShell is going to use UTF-16 by default when piping -- that's a very common use-case.

Comment thread crates/uv-fs/src/lib.rs Resolved
Comment thread Cargo.toml Resolved
Comment thread crates/uv-fs/src/lib.rs Resolved
@mitsuhiko

mitsuhiko commented Mar 7, 2024 •

Copy link
Copy Markdown
Contributor

I believe that Microsoft is slowly phasing out UTF-16 output on the console. On my machine it only outputs UTF-8 (Windows 11). Might be worthwhile figuring out if this is really something that happens regularly and will continue to do so.

Now I realize that this is because i use cmd.exe and not powershell.

I'm not sure what to do here. It does seem weird to me that uv would support this encoding since nothing else does, but maybe this can be useful for the user. I do fully expect that this will create questions though about why those files don't work with other tools.

@BurntSushi

Copy link
Copy Markdown
Member Author

I believe that Microsoft is slowly phasing out UTF-16 output on the console. On my machine it only outputs UTF-8 (Windows 11). Might be worthwhile figuring out if this is really something that happens regularly and will continue to do so.

//EDIT: now I realize that this is because i use cmd.exe and not powershell

This is interesting because I think Rust is the one who is writing UTF-16 to the console. Is that somehow getting transcoded back to UTF-8 for you?

@mitsuhiko

mitsuhiko commented Mar 7, 2024 •

Copy link
Copy Markdown
Contributor

@BurntSushi this is what it does on my machine:

C:\Users\armin\Development\foo>cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\foo.exe`
Fußball

C:\Users\armin\Development\foo>cargo run > foo.txt
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\foo.exe`

C:\Users\armin\Development\foo>python -c "print(open('foo.txt', 'rb').read())"
b'Fu\xc3\x9fball\n'

I think on this machine the default code page is 65001 but I also just managed to get a shell running earlier that had an ISO encoding. Not sure how that happened.

Comment thread crates/uv-fs/src/lib.rs Resolved
Comment thread crates/uv/tests/pip_install.rs Resolved
Comment thread crates/uv/tests/pip_install.rs Resolved
Comment thread crates/uv/tests/pip_install.rs Resolved
@mitsuhiko

mitsuhiko commented Mar 8, 2024 •

Copy link
Copy Markdown
Contributor

This is interesting because I think Rust is the one who is writing UTF-16 to the console. Is that somehow getting transcoded back to UTF-8 for you?

So this machine is a few months old and I'm really not sure how any of this works. What I can see on my machine:

  • cmd.exe: code page 65001
  • powershell.exe: code page 65001
  • cmd.exe launched from powershell: code page 1252

No clue why it does that. I tried on a VM and it always shows me code page 850. No matter though how, powershell always transcodes to UTF-16 but that is also reconfigurable with a flag (that probably nobody ever sets). Whatever API Python and Rust use to write, it clearly transcodes somehow somewhere.

EDIT: i unticked a beta box for utf-8 support in the intl.cpl and now getting consistently code page 850 in all situations. So whatever I did, that was a thing that most people probably don't have.

@MichaReiser

Copy link
Copy Markdown
Member

(that probably nobody ever sets).

I did 😆

This PR tweaks uv to support reading `requirements.txt` regardless of
whether it is encoded as UTF-8 or UTF-16. This is particularly relevant
on Windows where `uv pip freeze > requirements.txt` will likely write a
UTF-16 encoded `requirements.txt` file.

There is some discussion on #1666 where it's suggested that perhaps
we should explicitly not support this. I didn't see that until I
had already put this PR together, but even so, I think it's worth
considering this. UTF-16 is predominant on Windows. It is very easy
to produce a UTF-16 encoded file. Moreover, there is an easy and well
specified way to recognize and transcode UTF-16 encoded data to UTF-8.

I think the downside of this is that it could encourage the use UTF-16
encoded `requirements.txt` files *in addition* to UTF-8 encoded
files, and it would probably be nice to converge and standardize on
one encoding. One possible alternative to this PR is that we provide
a better error message. Another alternative is to ensure that a
`-o/--output` flag exists for all commands (neither `uv pip freeze` nor
`pip freeze` have such a flag) so that users can always write output
to a file without relying on their environment's piping behavior.
(Although this last alternative seems a little sad to me.)

It's also worth noting the [PEP-0508] doesn't seem to mention file
encoding at all. So I think from a "do the standards allow this"
perspective, this change is OK.

Finally, `pip` itself seems to work with UTF-16 encoded
`requirements.txt` files.

I think I personally overall lean towards supporting UTF-16 for
`requirements.txt` files. In part because I think it smoothes out the
UX a little bit, in part because there is no obvious specification
(that I'm aware of) that mandates that these files are UTF-8, and
finally in part because `pip` supports it too.

Fixes #1666, Fixes #2276

[PEP-0508]: https://peps.python.org/pep-0508/
This was causing `cargo test --doc` to fail for me locally.
@BurntSushi

Copy link
Copy Markdown
Member Author

I'm going to bring this in given most folks seem to be in support of it. Moreover, it addresses an easy footgun for an existing common pip workflow: uv pip freeze > requirements.txt is likely to produce a UTF-16 file on Windows, although there are perhaps configurations and environments where that is not true. Still, it doesn't cost us much to support UTF-16 here.

@BurntSushi
BurntSushi merged commit 41c911f into main Mar 8, 2024
@BurntSushi
BurntSushi deleted the ag/read-utf16 branch March 8, 2024 14:10
@BurntSushi BurntSushi added bug Something isn't working area:windows Specific to the Windows platform labels Mar 8, 2024
charliermarsh added a commit that referenced this pull request Sep 25, 2026
A UTF-16 requirements file containing only its byte-order mark was
passed through by the decoder and rejected as invalid UTF-8. We now
recognize both complete UTF-16 BOMs as empty input in the shared
transcoding helper used by file and stdin reads. Every other input
continues through the existing decoder. The in-module tests cover UTF-8
and UTF-16, including non-BMP text, interior BOMs, and empty input.
Follow-up to #2283.

---------

Co-authored-by: Zanie Blue <[email protected]>
Co-authored-by: Charlie Marsh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:windows Specific to the Windows platform bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv pip wont accept my requirements file Support alternative requirements file encodings

6 participants