Skip to content

rustfmt_lib = { git = "https://github.com/rust-lang/rustfmt" } #4388

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

Closed
ctaggart opened this issue Aug 16, 2020 · 4 comments
Closed
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@ctaggart
Copy link
Contributor

In Cargo.toml, I have

[dependencies]
rustfmt_lib = "2.0.0-rc.2"

But that version does not compile and I'd like to update to the latest 2.0.0 code, but this does not work:

[dependencies]
rustfmt_lib = { git = "https://github.com/rust-lang/rustfmt" }
Camerons-MacBook-Pro:openapi_to_rust cameron$ cargo update
    Updating crates.io index
    Updating git repository `https://github.com/ctaggart/openapi`
    Updating git repository `https://github.com/rust-lang/rustfmt`
error: no matching package named `rustfmt_lib` found
location searched: https://github.com/rust-lang/rustfmt
required by package `openapi_to_rust v0.1.0 (/Users/cameron/rs/openapi_to_rust)`
@ctaggart ctaggart added the bug Panic, non-idempotency, invalid code, etc. label Aug 16, 2020
@ctaggart
Copy link
Contributor Author

ctaggart commented Aug 16, 2020

It would be nice to be able to work from git. An alternative would be to publish a 2.0.0-rc.3 which has bumped the rustc-ap-* crates to 671.0.0 (#4354).

Current error with 2.0.0-rc.2 from crates.io and latest rustc nightly is:

   Compiling rustc-ap-rustc_data_structures v659.0.0
   Compiling rustc-ap-arena v659.0.0
error[E0599]: no method named `reserve_in_place` found for struct `alloc::raw_vec::RawVec<T>` in the current scope
   --> /Users/cameron/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-ap-arena-659.0.0/lib.rs:228:39
    |
228 |                 if last_chunk.storage.reserve_in_place(currently_used_cap, n) {
    |                                       ^^^^^^^^^^^^^^^^ method not found in `alloc::raw_vec::RawVec<T>`

error[E0599]: no method named `reserve_in_place` found for struct `alloc::raw_vec::RawVec<u8>` in the current scope
   --> /Users/cameron/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-ap-arena-659.0.0/lib.rs:356:39
    |
356 |                 if last_chunk.storage.reserve_in_place(used_bytes, needed_bytes) {
    |                                       ^^^^^^^^^^^^^^^^ method not found in `alloc::raw_vec::RawVec<u8>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0599`.
error: could not compile `rustc-ap-arena`.

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
Camerons-MacBook-Pro:openapi_to_rust cameron$ rustc --version
rustc 1.47.0-nightly (9b88e0a86 2020-08-15)

@calebcartwright
Copy link
Member

but this does not work:

[dependencies]
rustfmt_lib = { git = "https://github.com/rust-lang/rustfmt" }

If you're going to try to build from source, make sure to adjust the references to the latest in source (https://github.com/rust-lang/rustfmt/blob/master/Cargo.toml#L3) as some things were restructured a bit since the previous tag.

Current error with 2.0.0-rc.2 and latest rustc nightly is:

   Compiling rustc-ap-rustc_data_structures v659.0.0
   Compiling rustc-ap-arena v659.0.0

I don't think you're working with the latest in source. Perhaps you need to run a cargo clean or otherwise adjust your local env? The rustc-ap-* crates in source are all on v671, e.g. https://github.com/rust-lang/rustfmt/blob/master/Cargo.toml#L110

It would be nice to be able to work from git. An alternative would be to publish a 2.0.0-rc.3 which has bumped the rustc-ap-* crates to 671.0.0 (#4354)

Sorry, but I don't want to get in the habit of having to publish tags to support git based consumption. To be honest, I'm not sure that making rustfmt available as a lib for outside consumers will be part of the strategy going forward, particularly with git-based references.

The auto publish process for consuming rustc internals is already challenging enough within rustfmt itself, and I don't want to put more work on the rustfmt plate.

@ctaggart
Copy link
Contributor Author

ctaggart commented Aug 16, 2020

I'm not sure that making rustfmt available as a lib for outside consumers will be part of the strategy going forward

It is super useful to be able to format from a wasm/wasi libary. You can't run external processes such as rustfmt from wasi. Please keep this as a use case.

Sorry, but I don't want to get in the habit of having to publish tags to support git based consumption

I would prefer to use a published version, rather than git. I first tried to use the 2.0.0-rc.2 published binary, but it did not work. It gave me the errors I posted above, because it is using the older rustc-ap-* crates.

Is rustfmt_lib a renamed rustfmt-nightly? This did work:

rustfmt-nightly = { git = "https://github.com/rust-lang/rustfmt" }

Here is the code I'm using. If there is an easier way, please let me know. Thank you a lot for your rustfmt contributions!

fn format_code(unformatted: String) -> String {
    let mut config = rustfmt_nightly::Config::default();
    config.set().edition(rustfmt_nightly::Edition::Edition2018);
    config.set().max_width(140);
    let setting = rustfmt_nightly::OperationSetting {
        verbosity: rustfmt_nightly::emitter::Verbosity::Quiet,
        ..rustfmt_nightly::OperationSetting::default()
    };
    match rustfmt_nightly::format(rustfmt_nightly::Input::Text(unformatted.clone()), &config, setting){
        Ok(report) => {
            match report.format_result().next() {
                Some((_, format_result)) => {
                    let formatted = format_result.formatted_text();
                    if formatted.is_empty() {
                        unformatted
                    } else {
                        formatted.to_owned()
                    }
                }
                _ => unformatted
            }
        }
        Err(_err) => {
            unformatted
        }
    }
}

@calebcartwright
Copy link
Member

Is rustfmt_lib a renamed rustfmt-nightly? This did work:

Yup, exactly.

I would prefer to use a published version, rather than git. I first tried to use the 2.0.0-rc.2 published binary, but it did not work

I know there's a handful of crates out on crates.io from some experimental things several months ago, but the only official rustfmt crate is https://crates.io/crates/rustfmt-nightly. v2.0 has not been officially published and that's very much be design.

It gave me the errors I posted above, because it is using the older rustc-ap-* crates.
Please keep this as a use case.

No decisions have been made yet, but the rustc auto publish process is the inherent problem here. rustfmt is deeply reliant on the rustc internals, and in order to be able to publish rustfmt to crates.io, those rustc internals have to be consumed via the auto publish process (the rustc-ap crates).

This approach has some notable downsides, primarily that breaking rustc changes are regularly merged upstream which breaks rustfmt and makes rustfmt unavailable for long periods of time through any medium, including rustup.

It's not that we don't see value in rustfmt being available as a lib, but with the current dependencies on rustc internals, it is a question of tradeoffs and whether it's worth continuing to have long bouts of rustfmt not being available for anyone just to support a handful of rustfmt-as-a-lib use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

2 participants