Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide UnixPath and WindowsPath (ditto PathBuf) on all platforms #66621

Open
joshtriplett opened this issue Nov 22, 2019 · 6 comments
Open

Provide UnixPath and WindowsPath (ditto PathBuf) on all platforms #66621

joshtriplett opened this issue Nov 22, 2019 · 6 comments
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

Some applications need to manipulate Windows or UNIX paths on different platforms, for a variety of reasons: constructing portable file formats, parsing files from other platforms, handling archive formats, working with certain network protocols, and so on.

We already have the code in the standard library to manipulate both Windows paths and UNIX paths.

I would propose that we either always support both (as UnixPath/UnixPathBuf and WindowsPath/WindowsPathBuf) and alias one of those to Path/PathBuf, or that we at least extract that code and upload it verbatim as a standard crate in the ecosystem that specifically documents itself as containing the exact code from the corresponding version of the Rust standard library.

I personally think this seems useful enough to include in std, but either solution would potentially work.

cc @sunshowers who mentioned needing this.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 22, 2019
@retep998
Copy link
Member

How would they interact with OsStr/OsString from std?

@joshtriplett
Copy link
Member Author

@retep998 Interesting question! The non-native path types wouldn't necessarily be compatible with the native OS string types, which would make it difficult to reuse the code between them. Doing this thoroughly might require providing non-native OsString/OsStr types as well. That seems useful as well, though.

(Also, all of these shouldn't take up any space in programs that don't use them.)

@skilly-lily
Copy link

There's some precedent here with python's stdlib as well. Python's pathlib implementation allows creation of path objects which only exist ephemerally: Pure Paths, and sub-classed paths which can perform real filesystem operations: Concrete Paths.

That said, the biggest difference between the python pathlib and the proposed suggestion is compile-time safety. Python can just throw exceptions on mismatched OS requests and tell the user to fix it, but in rust, we should be able to have compiler-ensured success on most operations. We should even be able to avoid Result's, in nearly all cases, just like the current Path/Pathbuf (except I/O and string parsing/validating, obviously).

As far as actual implementation goes, Path/Pathbuf are currently implemented using OsStr/OsString, respectively. In any case, either cross-platform OsStr/OsString needs to be exposed, or the top-level implementation of Path/Pathbuf needs another layer of indirection on top of OsStr/OsString to allow cross-platform access.

Based on the above statement, I personally think that this justifies an RFC, or perhaps a small third-party crate proving the concept. If accepted, it would make sense to keep this in std, but other than that there's nothing which forces this concept to only exist in std. The RFC process could additionally answer the OsStr/OsString question.

A potential contributor could definitely make a Proof-of-Concept crate at any time, RFC/this issue aside.

@skilly-lily
Copy link

Also, this is being discussed more in the cli-working group repo, although there's a lot of other discussion going on at the same time.

https://github.com/rust-lang-nursery/cli-wg/issues/10

@devsnek
Copy link
Contributor

devsnek commented Oct 16, 2020

This would definitely be nice. In Node.js, for example, we provide path.x for whatever you're running on, but we also provide path.win32.x and path.posix.x. It is especially annoying not to be able to access windows path behaviour on wasm targets.

@chipsenkbeil
Copy link

@joshtriplett I just published typed-path to provide a 3rd-party option to do this to some degree. Needed this for a server of mine that accepts user paths and parses them agnostic of the compiled platform.

@ChrisDenton ChrisDenton added the A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` label Jan 26, 2023
kdy1 pushed a commit to LingyuCoder/swc_plugins that referenced this issue Jul 5, 2024
…t#328)

[Currently](swc-project#327), when
`@swc/plugin-emotion` is used on Windows, it produces invalid CSS class
names. Apparently this is an [unsolved issue for
Rust](rust-lang/rust#66621). In this PR I've
updated the calculation of the `[filename]` `labelFormat` such that it
handles both the Windows path, which includes the full path to the file,
including backslashes, and the Unix path (which was working fine to
begin with).

I have set up a [minimal
repo](https://github.com/iryan2/cra-swc-emotion) for reproducing the
issue and verifying the fix. The issue can be reproduced on [the `main`
branch](https://github.com/iryan2/cra-swc-emotion), and the fix can be
verified on [the `fix`
branch](https://github.com/iryan2/cra-swc-emotion/tree/fix). The `fix`
branch replaces the compiled `swc_plugin_emotion.wasm` from
`@swc/plugin-emotion` npm package with one I built myself via [my fork
of this
repo](https://github.com/iryan2/swc-plugins/tree/iryan2/fix-emotion-plugin-on-windows).

## Before
<img width="612" alt="Screenshot 2024-07-03 at 4 22 41 PM"
src="https://github.com/swc-project/plugins/assets/5858312/04a8d2d0-e82f-44b3-90e9-afdcea9ae832">

## After
<img width="626" alt="Screenshot 2024-07-04 at 7 07 42 AM"
src="https://github.com/swc-project/plugins/assets/5858312/7bf53d34-fecb-47bd-a71e-eabf35eed22f">

---

Fixes swc-project#327
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants