Skip to content

Format all code with rustfmt #21373

Closed

Description

@jdm

#8553 (comment) describes a configuration that respects some style settings that we care about. I think it's worth systematically formatting Servo's code crate by crate (or module by module in the case of particularly large crates) and then integrating the changes in #20617 to enforce proper style in our CI.

Components:

ports:

Activity

  1. added
    B-meta
    I-cleanup
    E-less-complex
    on Aug 9, 2018
  2. highfive commented on Aug 10, 2018

    @highfive

    Hi! If you have any questions regarding this issue, feel free to make a comment here, or ask it in the #servo channel in IRC.

    If you intend to work on this issue, then add @highfive: assign me to your comment, and I'll assign this to you. 😄

  3. jdm commented on Aug 10, 2018

    @jdm
    MemberAuthor

    We now have a ./mach rustfmt [path] command that can be used, and you'll want to run rustup component add rustfmt-preview to install rustfmt if you don't already have it.

  4. kingdido999 commented on Aug 11, 2018

    @kingdido999

    Hi, I would like to work on this and will probably start from formatting the first component on the top.

    @highfive: assign me

  5. highfive commented on Aug 11, 2018

    @highfive

    Hey @kingdido999! Thanks for your interest in working on this issue. It's now assigned to you!

  6. added
    C-assigned
    on Aug 11, 2018
  7. kingdido999 commented on Aug 11, 2018

    @kingdido999

    So I've successfully built the project and ran rustup component add rustfmt-preview successfully. But when I do ./mach rustfmt components/allocator/lib.rs, it says:

    It looks like you passed an unrecognized argument into mach.
    
    The rustfmt command does not accept the arguments: components/allocator/lib.rs
    

    So I look into the rustfmt command defined in python/servo/devenv_commands.py #21374 , seems we have to pass in a --directory argument. Then I do ./mach rustfmt -d components/allocator, it returns:

    Error: `/Users/Desmond/repo/servo/components/allocator` is a directory
    

    Despite the error, it does run the formatting but for all files in the project, which is not what we want. If I provide a file path instead of a directory path, no error is shown but it formats the entire project again. My best guess is that --directory is not a valid command for rustfmt, we could produce the same error if we run rustfmt components/allocator.

    Directly run rustfmt against a file works properly:

    rustfmt components/allocator/lib.rs

    Here are the versions of rustc and rustfmt on my system:

    $ rustc --version
    rustc 1.29.0-nightly (1ecf6929d 2018-07-16)
    
    $ rustfmt --version
    rustfmt 0.8.2-nightly (5e599251 2018-07-02)

    @jdm @JoshBrudnak Please let me know if I'm missing something when I do ./mach rustfmt [path], thanks!

  8. jdm commented on Aug 14, 2018

    @jdm
    MemberAuthor

    I've verified that you're not missing anything. cargo fmt does not appear to do what we want; it looks like we'll need to modify the ./mach rustfmt command to recursively find all of the rust source files under the given directory and pass those as arguments to rustfmt directly. Would you like to make that change?

  9. kingdido999 commented on Aug 15, 2018

    @kingdido999

    Sure I will work on it.

  10. kingdido999 commented on Aug 15, 2018

    @kingdido999

    I'm trying to get a rough idea on the use cases of /mach rustfmt [directory] other than resolving this particular issue (format crate one at a time). It seems to me that we can simply get away with rustfmt as a part of future CI build after all crates have been formatted. So if that is true, we don't necessarily need to invent an extra command to format a directory, we could do something like this instead:

    rustfmt ports/servo/**/*.rs
  11. jdm commented on Aug 15, 2018

    @jdm
    MemberAuthor

    Sure, that would work. I meant to make this issue possible for multiple people to work on in parallel since there are so many components, so I figured a mach command would be the easiest way to support that.

  12. added a commit that references this issue on Aug 16, 2018
  13. 70 remaining items

  14. added 3 commits that reference this issue on Sep 11, 2018
  15. AnshulMalik commented on Sep 12, 2018

    @AnshulMalik
    Contributor

    rand is already formatted.

  16. added 3 commits that reference this issue on Sep 13, 2018
  17. removed
    C-assigned
    on Oct 6, 2018
  18. pyfisch commented on Oct 15, 2018

    @pyfisch
    Contributor

    Hi, formatting is almost complete. I'd like to suggest that servo adopts a new formatting for use statements. Currently use statements are ordered alphabetically and split into multiple lines if they are longer than 100 characters.

    I would want to split the imports into 4 groups (std, third-party imports, workspace imports, imports from the same crate) and sort those groups individually. (See rust-lang/rustfmt#2979)

    Additionally I prefer to use merge_imports formatting. I can see at a glance which items are imported from a module and how the module hierarchy is structured. (example file)

    The grouping can be done with a script. This could either be added to cargo fmt or the lines are grouped by a script once.

  19. added 2 commits that reference this issue on Nov 5, 2018
  20. Dmitry-Borodin commented on Nov 5, 2018

    @Dmitry-Borodin

    @jdm I see the last components from the list net and net_traits was formatted in #22099.
    Should this issue now be closed?
    It's among the first options in https://starters.servo.org/ -)

  21. atouchet commented on Nov 5, 2018

    @atouchet
    Contributor

    #20617 still needs to be finished.

  22. CYBAI commented on Nov 7, 2018

    @CYBAI
    Member

    Fixed in #22126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-metaE-less-complexI-cleanup

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions