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

Enable incremental rustfmt adoption #65939

Merged
merged 7 commits into from
Dec 22, 2019

Conversation

anp
Copy link
Member

@anp anp commented Oct 29, 2019

Enables an incremental rollout of rustfmt usage within the compiler via a granular ignore configuration and automated enforcement. The decision to format the repository was approved in rust-lang/compiler-team#80 (comment).

This PR includes:

  • an [ignore] section to rustfmt.toml including most of the repository
  • ./x.py downloads rustfmt from a specific nightly (we do not pin to beta or stable as we need unstable features)
  • an ./x.py fmt [--check] command which runs cargo-fmt
  • ./x.py fmt --check runs during the same test step as src/tools/tidy, on master, but not on beta or stable as we don't want to depend on nightly rustfmt there.
  • a commit to format src/librustc_fs_util as an initial target and to ensure enforcement is working from the start

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 29, 2019
@nikomatsakis
Copy link
Contributor

Big 👍 to this approach overall, I have no idea about the question of integrating into tidy though -- maybe we can somehow find it in path?

@Mark-Simulacrum
Copy link
Member

I'm going to r? @Mark-Simulacrum here and try and find some time this week to get back to you here on the overall approach and such. I'm sure we'll want sign off from T-compiler (and perhaps T-libs) as the primary "owners" of the code in this repository but for now we can likely figure out a game plan to bring to fcp.

@Mark-Simulacrum
Copy link
Member

Would it make more sense to use the bootstrap toolchain's rustfmt binary? What's the best way to experiment with that?

I suspect that the best thing is indeed to use the bootstrap binary, as that'll be a preset way that'll change at the right points and is guaranteed to exist and such. The way to do this today is probably to add another artifact to the things we download (in particular, in src/bootstrap/bootstrap.py).

Let me know if you'd like further details on how to do that, I can look into it, though I've not recently explored that piece of code.

I agree with @nikomatsakis that this is the right approach, seems like the best way to phase this in.

If we want to test this out on a particular subdirectory I think src/bootstrap is perhaps a good candidate as it's fairly small and changes not too often. However, we can also land this I think without enabling it and then enable it for a specific directory in a separate PR.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 29, 2019
@anp
Copy link
Member Author

anp commented Oct 30, 2019

Cool! I think I have bootstrap.py fetching the binary (see pushed commit). Next step is to figure out the places to run it. I see two:

  1. modify bootstrap+tidy to know how to use x.py's rustfmt as part of its style checking (working on this)
  2. add a documented command somewhere (maybe a bootstrap subcommand?) that can be used to format one's code locally

@anp anp force-pushed the incremental-rustfmt-rollout branch from 78794d1 to 7a52437 Compare October 30, 2019 02:59
@Centril
Copy link
Contributor

Centril commented Oct 30, 2019

I believe the compiler team already approved of formatting the repo in rust-lang/compiler-team#80.

@anp take a look at Centril/rfcs#21

@Mark-Simulacrum
Copy link
Member

Cool! I think I have bootstrap.py fetching the binary (see pushed commit).

Yep, this looks good.

I think we should add it as an x.py command, e.g., x.py fmt to mirror cargo fmt, which would have a --check flag for CI (similar to cargo fmt I believe), it would also know how to run rustfmt/cargo fmt with the appropriate flags and such.

I think tidy is probably not quite the right place as at least I know that it's sort of not the thing you run by default, i.e., x.py fmt should be runnable "on save" or so from editors I hope whereas tidy is possibly too slow or not a good fit for that. The tidy test step in bootstrap might want to run the fmt check as well though (not the tidy binary, but the impl Step for Tidy (not sure on naming) in src/bootstrap/test.rs).

@Mark-Simulacrum
Copy link
Member

Last two commits look a little different from what I had expected but actually seem like a better approach, so continue on!

We should try to keep the PR description up to date with a summary of what we've decided on so far (it'll go into commit history, so treat it as such). Just wanted to write that so we can avoid a surprise of work at the end, no need necessarily to do incremental work to get there, I think it's primarily useful for historical reasons.

@anp anp changed the title Hypothesis for improving rustfmt adoption within rustc? Incremental rustfmt adoption Oct 31, 2019
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-10-31T02:24:30.5993660Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-10-31T02:24:30.6178154Z ##[command]git config gc.auto 0
2019-10-31T02:24:30.6249888Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-10-31T02:24:30.6320128Z ##[command]git config --get-all http.proxy
2019-10-31T02:24:30.6447553Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/65939/merge:refs/remotes/pull/65939/merge

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@anp anp force-pushed the incremental-rustfmt-rollout branch from 9fdefe3 to d33bd58 Compare November 1, 2019 00:37
@anp
Copy link
Member Author

anp commented Nov 1, 2019

In getting this to work, I found that rustfmt hangs on stdin activity if no paths are passed, so I took @Mark-Simulacrum's recommendation to format src/bootstrap as a starter.

src/bootstrap/format.rs Outdated Show resolved Hide resolved
src/bootstrap/format.rs Outdated Show resolved Hide resolved
src/bootstrap/format.rs Outdated Show resolved Hide resolved
src/bootstrap/format.rs Outdated Show resolved Hide resolved
src/stage0.txt Outdated Show resolved Hide resolved
@anp anp changed the title Incremental rustfmt adoption Enable incremental rustfmt adoption Nov 2, 2019
anp added a commit to anp/rustfmt that referenced this pull request Nov 2, 2019
@Mark-Simulacrum
Copy link
Member

One thought -- could we split the bootstrap re-formatting into a separate PR which I can r+ immediately? That way we keep the diff line count in this PR down (making it a little easier to review).

I would also like us to try and prepare a (potentially hacky) script that re-formats the repository automatically during rebasing or so with the idea being that a PR that is currently in flight to e.g. bootstrap would not be too hard to rebase atop this. Not sure how easy that is, if it proves pretty much impossible then no need to spend too much time on it.

@anp
Copy link
Member Author

anp commented Nov 4, 2019

This makes sense! Another thought: maybe instead of formatting src/bootstrap I should pick an even smaller target like src/build_helper?

@anp
Copy link
Member Author

anp commented Nov 4, 2019

Note to self: this PR should probably include or immediately kick off changes to the rustc guide.

@Mark-Simulacrum
Copy link
Member

This makes sense! Another thought: maybe instead of formatting src/bootstrap I should pick an even smaller target like src/build_helper?

Sure, yeah, or maybe even src/libarena -- basically any small and rarely changed crate seems like a fine fit, optimizing a little more for smallness (I had expected src/bootstrap to have a smaller diff, somehow).

@anp anp force-pushed the incremental-rustfmt-rollout branch from 50e8b75 to b1d0cdf Compare November 4, 2019 16:23
@anp
Copy link
Member Author

anp commented Nov 4, 2019

I picked librustc_fs_util (it's really tiny!) and pushed it to this branch since it's such a low risk for churn and hopefully doesn't increase review burden. Happy to split it up if you'd like though, lmk.

src/bootstrap/Cargo.toml Outdated Show resolved Hide resolved
src/bootstrap/lib.rs Outdated Show resolved Hide resolved
anp and others added 3 commits December 21, 2019 20:23
In total it's about 100 lines of code and has received less than 5 commits in 2019 -- a good starting point.
This replaces cargo-fmt with rustfmt with --skip-children which should
allow us to format code without running into rust-lang/rustfmt#3930.

This also bumps up the version of rustfmt used to a more recent one.
@Mark-Simulacrum
Copy link
Member

Rebased. @bors r+

@bors
Copy link
Contributor

bors commented Dec 22, 2019

📌 Commit dddd872 has been approved by Mark-Simulacrum

@Centril
Copy link
Contributor

Centril commented Dec 22, 2019

@bors p=210

@bors
Copy link
Contributor

bors commented Dec 22, 2019

⌛ Testing commit dddd872 with merge 242f3d774c05dca236a07e1564720a37ef6a7c31...

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-distcheck of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-12-22T03:39:51.5560941Z    Compiling toml v0.5.3
2019-12-22T03:39:51.9217012Z    Compiling serde_json v1.0.40
2019-12-22T03:39:55.1377441Z    Compiling bootstrap v0.0.0 (/checkout/obj/build/tmp/distcheck/src/bootstrap)
2019-12-22T03:40:30.4746169Z     Finished dev [unoptimized] target(s) in 1m 23s
2019-12-22T03:40:32.2588099Z thread 'main' panicked at 'std::fs::read_to_string(build.src.join("rustfmt.toml")) failed with No such file or directory (os error 2)', src/bootstrap/format.rs:41:26
2019-12-22T03:40:32.2589770Z failed to run: /checkout/obj/build/tmp/distcheck/build/bootstrap/debug/bootstrap test
2019-12-22T03:40:32.2595711Z Build completed unsuccessfully in 0:01:42
2019-12-22T03:40:32.2596666Z make: *** [check] Error 1
2019-12-22T03:40:32.2596666Z make: *** [check] Error 1
2019-12-22T03:40:32.2597274Z Makefile:48: recipe for target 'check' failed
2019-12-22T03:40:32.2597636Z 
2019-12-22T03:40:32.2597800Z command did not execute successfully: "make" "make" "check"
2019-12-22T03:40:32.2597990Z expected success, got: exit code: 2
2019-12-22T03:40:32.2598122Z 
---
2019-12-22T03:40:32.2599943Z   local time: Sun Dec 22 03:40:31 UTC 2019
2019-12-22T03:40:32.2600168Z   network time: Sun, 22 Dec 2019 03:40:31 GMT
2019-12-22T03:40:32.2600348Z == end clock drift check ==
2019-12-22T03:40:41.8523626Z 
2019-12-22T03:40:41.8642821Z ##[error]Bash exited with code '1'.
2019-12-22T03:40:41.8687929Z ##[section]Starting: Checkout
2019-12-22T03:40:41.8690554Z ==============================================================================
2019-12-22T03:40:41.8690667Z Task         : Get sources
2019-12-22T03:40:41.8690751Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@bors
Copy link
Contributor

bors commented Dec 22, 2019

💔 Test failed - checks-azure

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 22, 2019
distcheck (and generally publishing tarballs) will not package
rustfmt.toml and we for now still support running tidy etc in those
tarballs.
@Mark-Simulacrum
Copy link
Member

@bors r+

Fixed distcheck -- if we don't find rustfmt.toml we'll just bail out of formatting.

@bors
Copy link
Contributor

bors commented Dec 22, 2019

📌 Commit b9e4174 has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 22, 2019
@bors
Copy link
Contributor

bors commented Dec 22, 2019

⌛ Testing commit b9e4174 with merge 0d2817a...

bors added a commit that referenced this pull request Dec 22, 2019
…crum

Enable incremental rustfmt adoption

Enables an incremental rollout of rustfmt usage within the compiler via a granular ignore configuration and automated enforcement. The decision to format the repository was approved in rust-lang/compiler-team#80 (comment).

This PR includes:

* an `[ignore]` section to `rustfmt.toml` including most of the repository
* `./x.py` downloads rustfmt from a specific nightly (we do not pin to beta or stable as we need unstable features)
* an `./x.py fmt [--check]` command which runs cargo-fmt
* `./x.py fmt --check` runs during the same test step as `src/tools/tidy`, on master, but not on beta or stable as we don't want to depend on nightly rustfmt there.
* a commit to format `src/librustc_fs_util` as an initial target and to ensure enforcement is working from the start
@bors
Copy link
Contributor

bors commented Dec 22, 2019

☀️ Test successful - checks-azure
Approved by: Mark-Simulacrum
Pushing 0d2817a to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 22, 2019
@bors bors merged commit b9e4174 into rust-lang:master Dec 22, 2019
@anp anp deleted the incremental-rustfmt-rollout branch December 23, 2019 16:55
Comment on lines +26 to +27
let status = cmd.status().expect("executing rustfmt");
assert!(status.success(), "running {} successful", cmd_debug);
Copy link
Member

@eddyb eddyb Mar 22, 2020

Choose a reason for hiding this comment

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

One rustfmt --check failure should not prevent running rustfmt --check on other files, otherwise you only get one error at a time.

Copy link
Member Author

Choose a reason for hiding this comment

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

When that bit was originally written we were running rustfmt once for the whole repository and it showed all errors. Is that no longer how it's running?

Copy link
Member

Choose a reason for hiding this comment

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

It looks like @Mark-Simulacrum pushed commits to this PR to run rustfmt on individual files.

Copy link
Member Author

Choose a reason for hiding this comment

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

Aha - maybe file a bug so someone can pick up a fix?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, rustfmt on the whole repository was leading to errors in rustfmt (IIRC, it had some problems with ignoring files or so, I don't recall the specifics now).

This should be fixable though, like we do in tidy etc by keeping track of whether we errored and only stopping at the end.

nnethercote added a commit to nnethercote/rust that referenced this pull request May 23, 2024
This comment -- "by default we ignore everything in the repository" --
was added in rust-lang#65939 when rustfmt was first being introduced for this
repository and (briefly) every directory was ignored. Since then lots of
directories have opted in to formatting, so it is no longer true.
nnethercote added a commit to nnethercote/rust that referenced this pull request May 23, 2024
This comment -- "by default we ignore everything in the repository" --
was added in rust-lang#65939 when rustfmt was first being introduced for this
repository and (briefly) every directory was ignored. Since then lots of
directories have opted in to formatting, so it is no longer true.
nnethercote added a commit to nnethercote/rust that referenced this pull request May 28, 2024
This comment -- "by default we ignore everything in the repository" --
was added in rust-lang#65939 when rustfmt was first being introduced for this
repository and (briefly) every directory was ignored. Since then lots of
directories have opted in to formatting, so it is no longer true.
nnethercote added a commit to nnethercote/rust that referenced this pull request May 28, 2024
This comment -- "by default we ignore everything in the repository" --
was added in rust-lang#65939 when rustfmt was first being introduced for this
repository and (briefly) every directory was ignored. Since then lots of
directories have opted in to formatting, so it is no longer true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.