{"id":4135,"date":"2025-02-17T17:32:19","date_gmt":"2025-02-17T17:32:19","guid":{"rendered":"https:\/\/www.gubatron.com\/blog\/?p=4135"},"modified":"2025-02-17T17:32:19","modified_gmt":"2025-02-17T17:32:19","slug":"the-curious-case-of-inconsistent-cargo-fmt-formatting-and-how-to-fix-it","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/the-curious-case-of-inconsistent-cargo-fmt-formatting-and-how-to-fix-it\/","title":{"rendered":"The Curious Case of Inconsistent cargo fmt Formatting (and How to Fix It)"},"content":{"rendered":"<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Have you ever run into a situation where\u00a0<\/span><span class=\"inline-code ng-star-inserted\">cargo fmt<\/span><span class=\"ng-star-inserted\">, Rust&#8217;s code formatter, produces different output on different machines, even though you&#8217;re working on the same project? This can be incredibly frustrating, especially when you&#8217;re trying to maintain consistent code style across a team or between your own development environments. I recently encountered this issue, and after a bit of digging, I found a somewhat obscure cause and a straightforward solution that I wanted to share.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">The Problem: Formatting Discrepancies<\/span><\/strong><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">I was working on a Rust project, and I noticed that\u00a0<\/span><span class=\"inline-code ng-star-inserted\">cargo fmt<\/span><span class=\"ng-star-inserted\">\u00a0was formatting the code slightly differently on my macOS machine compared to my Linux server. The differences were subtle, but they were enough to cause unnecessary diffs in my Git repository and make code reviews a bit more challenging.<\/span><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Naturally, I started by checking the usual suspects:<\/span><\/p>\n<ul class=\"ng-star-inserted\">\n<li class=\"ng-star-inserted\">\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Rust Versions:<\/span><\/strong><span class=\"ng-star-inserted\">\u00a0I made sure that both machines were running the same version of the Rust toolchain using\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustc &#8211;version<\/span><span class=\"ng-star-inserted\">\u00a0and\u00a0<\/span><span class=\"inline-code ng-star-inserted\">cargo &#8211;version<\/span><span class=\"ng-star-inserted\">. They were identical.<\/span><\/p>\n<\/li>\n<li class=\"ng-star-inserted\">\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">:<\/span><\/strong><span class=\"ng-star-inserted\">\u00a0I searched my project directory and all parent directories for a\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0file, which is used to customize the formatting rules. I couldn&#8217;t find any.<\/span><\/p>\n<\/li>\n<li class=\"ng-star-inserted\">\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Environment Variables:<\/span><\/strong><span class=\"ng-star-inserted\">\u00a0I checked for any environment variables that might be influencing\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">&#8216;s behavior, but nothing seemed out of the ordinary.<\/span><\/p>\n<\/li>\n<\/ul>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Despite these efforts, the formatting differences persisted. It was a real head-scratcher.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">The Unexpected Culprit:\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0in\u00a0<\/span><span class=\"inline-code ng-star-inserted\">stdarch<\/span><span class=\"ng-star-inserted\">\u00a0Source Code<\/span><\/strong><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">After further investigation, I discovered the root cause:<br \/>\n<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0was somehow picking up\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0files located within the source code of the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">stdarch<\/span><span class=\"ng-star-inserted\"> library, which is part of the Rust standard library.<\/p>\n<p>These files are located in the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustup<\/span><span class=\"ng-star-inserted\">\u00a0directory, specifically under the toolchain directory (e.g.,\u00a0<\/span><span class=\"inline-code ng-star-inserted\">~\/.rustup\/toolchains\/&lt;toolchain_name&gt;\/lib\/rustlib\/src\/rust\/library\/stdarch\/<\/span><span class=\"ng-star-inserted\">).<\/span><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">The issue seemed to be happening only on my macOS machine, not on my Linux server, suggesting a platform-specific difference in how\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0searches for configuration files.<\/span><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">These\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0files are\u00a0<\/span><span class=\"ng-star-inserted\">not<\/span><span class=\"ng-star-inserted\"> intended to be user configuration files.<\/p>\n<p>They are used internally to format the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">stdarch<\/span><span class=\"ng-star-inserted\"> library&#8217;s code.<\/p>\n<p>For some reason,\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0was incorrectly identifying them as valid configuration files for my project.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">The Solution:\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustfmt.ignore<\/span><span class=\"ng-star-inserted\">\u00a0to the Rescue<\/p>\n<p><\/span><\/strong><span class=\"ng-star-inserted\">You&#8217;d think, well, maybe I can just delete these files, or copy them to my other machine, but the best solution is neither.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong><span class=\"ng-star-inserted\">The solution is to tell\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0to ignore these unintended configuration files using a\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustfmt.ignore<\/span><\/strong><span class=\"ng-star-inserted\"><strong>\u00a0file.<\/strong><\/p>\n<p>Similar to .gitignore, a .rustfmt.ingore file in your project allows you to specify paths that <\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0should skip when searching for configuration files.<\/span><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">I created a <\/span><span class=\"inline-code ng-star-inserted\">.rustfmt.ignore<\/span><span class=\"ng-star-inserted\">\u00a0file in the root of my project (where my\u00a0<\/span><span class=\"inline-code ng-star-inserted\">Cargo.toml<\/span><span class=\"ng-star-inserted\">\u00a0file is) and added the following lines:<\/span><\/p>\n<div class=\"syntax-highlighted-code-wrapper\">\n<div class=\"syntax-highlighted-code\">\n<pre><code>.\/toolchains<span class=\"hljs-comment\">\/*\/lib\/rustlib\/src\/rust\/library\/stdarch\/crates\/core_arch\/rustfmt.toml\n.\/toolchains\/*\/<\/span>lib\/rustlib\/<span class=\"hljs-attribute\">src<\/span>\/rust\/library\/stdarch\/rustfmt<span class=\"hljs-selector-class\">.toml<\/span><\/code><\/pre>\n<\/div>\n<footer class=\"gmat-body-medium ng-star-inserted\">I ran cargo fmt, and finally my files on the mac were formatted identically as they were being formatted on my linux server.<\/footer>\n<\/div>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">This tells\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0to ignore any\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0files found in those specific paths within the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustup<\/span><span class=\"ng-star-inserted\">\u00a0directory, using a glob to match the varying toolchain names.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Why This Works<\/span><\/strong><\/p>\n<p class=\"ng-star-inserted\"><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0respects the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustfmt.ignore<\/span><span class=\"ng-star-inserted\">\u00a0file and will skip any files or directories listed in it when searching for configuration files. By adding the paths to the\u00a0<\/span><span class=\"inline-code ng-star-inserted\">stdarch<\/span><span class=\"ng-star-inserted\">\u00a0library&#8217;s\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt.toml<\/span><span class=\"ng-star-inserted\">\u00a0files, I prevented\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0from accidentally picking them up.<\/span><\/p>\n<p class=\"ng-star-inserted\"><strong class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Conclusion<\/span><\/strong><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">Inconsistent\u00a0<\/span><span class=\"inline-code ng-star-inserted\">cargo fmt<\/span><span class=\"ng-star-inserted\">\u00a0formatting can be a frustrating problem, but it&#8217;s often caused by unexpected configuration issues. In this case, the culprit was\u00a0<\/span><span class=\"inline-code ng-star-inserted\">rustfmt<\/span><span class=\"ng-star-inserted\">\u00a0mistakenly picking up configuration files from within the standard library&#8217;s source code. By using a\u00a0<\/span><span class=\"inline-code ng-star-inserted\">.rustfmt.ignore<\/span><span class=\"ng-star-inserted\">\u00a0file, I was able to resolve the issue and ensure consistent formatting across all my development environments.<\/span><\/p>\n<p class=\"ng-star-inserted\"><span class=\"ng-star-inserted\">If you encounter similar formatting discrepancies, I hope this post helps you troubleshoot the problem and find a solution!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever run into a situation where\u00a0cargo fmt, Rust&#8217;s code formatter, produces different output on different machines, even though you&#8217;re working on the same project? This can be incredibly frustrating, especially when you&#8217;re trying to maintain consistent code style across a team or between your own development environments. I recently encountered this issue, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4136,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[],"class_list":["post-4135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Abstract_digital_art_fragmented_code_snippets_floati_e3f2c486-78ad-4b2a-b86a-e792a1970064_2.png?fit=1024%2C1024&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-14H","jetpack-related-posts":[{"id":4000,"url":"https:\/\/www.gubatron.com\/blog\/what-is-the-rust-equivalent-to-javas-printwriter\/","url_meta":{"origin":4135,"position":0},"title":"What is the Rust equivalent to Java&#8217;s PrintWriter?","author":"gubatron","date":"December 21, 2022","format":false,"excerpt":"In Rust, the equivalent of Java's PrintWriter is the std::io::Write trait, which is implemented by a number of types that can be used to write data to an output stream, such as a file or a network socket. To use Write to write text to an output stream, you can\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":542,"url":"https:\/\/www.gubatron.com\/blog\/how-to-update-the-location-of-your-subversion-repo-without-checking-out-again-everything\/","url_meta":{"origin":4135,"position":1},"title":"How to update the location of your subversion Repo without checking out again everything","author":"gubatron","date":"July 10, 2007","format":false,"excerpt":"So you checked out code from a place, and whoever runs your subversion server decided to change the URL, or decided to switch from http:\/\/ to https:\/\/ or to svn+ssh:\/\/ ... If you're a noob, you'll probably just checkout everything again. Next time this happens just use svn switch Here's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4129,"url":"https:\/\/www.gubatron.com\/blog\/introducing-uninews-a-universal-news-scraper-in-rust\/","url_meta":{"origin":4135,"position":2},"title":"Introducing Uninews: A Universal News Scraper in Rust","author":"gubatron","date":"February 17, 2025","format":false,"excerpt":"The internet is overflowing with news, but extracting clean, readable content from articles can be a tedious task. Whether you're aggregating news for personal consumption, research, or AI training, automating this process is a must. Enter Uninews, a powerful, lightweight, and efficient Rust-based news scraper that simplifies content extraction and\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3241,"url":"https:\/\/www.gubatron.com\/blog\/setting-up-eclipse-as-your-ide-for-bitcoin-c-development-on-macosx\/","url_meta":{"origin":4135,"position":3},"title":"Setting up Eclipse as your IDE for Bitcoin C++ development on MacOSX.","author":"gubatron","date":"February 9, 2014","format":false,"excerpt":"If you are a Java developer used to the productivity levels achieved by working with eclipse's code navigation, code completion and refactoring tools, it's worth your time staying in eclipse for any sort of C++ development. This post refers specifically to getting your eclipse environment to work with a particular\u2026","rel":"","context":"In &quot;BitCoin&quot;","block_context":{"text":"BitCoin","link":"https:\/\/www.gubatron.com\/blog\/category\/bitcoin-2\/"},"img":{"alt_text":"Screen Shot 2014-02-09 at 1.09.39 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-09-at-1.09.39-PM.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3020,"url":"https:\/\/www.gubatron.com\/blog\/how-to-avoid-android-compilationbuilding-issues-setting-up-the-eclipse-buildpath-correctly\/","url_meta":{"origin":4135,"position":4},"title":"How to avoid Android compilation\/building issues &#8211; Setting up the Eclipse Buildpath correctly","author":"gubatron","date":"May 21, 2013","format":false,"excerpt":"This post is the product of about 48 hours of not being able to work on what I was supposed to be working because I had to deal with build issues in Eclipse. Hopefully If you follow these steps you will be able to solve some of the issues you\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"Screen Shot 2013-05-21 at 12.13.46 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1922,"url":"https:\/\/www.gubatron.com\/blog\/think-youre-in-a-big-project\/","url_meta":{"origin":4135,"position":5},"title":"Think you&#8217;re in a big project?","author":"gubatron","date":"May 18, 2010","format":false,"excerpt":"Recently I became curious on how many lines of code a huge open source project I contribute to has on what languages. I found a tool called \"cloc\" on sourceforge, check out the results and I dare you to think again if you think you're in a big project. [bash]\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=4135"}],"version-history":[{"count":1,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions"}],"predecessor-version":[{"id":4137,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions\/4137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media\/4136"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=4135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=4135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=4135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}