| <!DOCTYPE HTML> | |
| <html lang="en" class="light sidebar-visible" dir="ltr"> | |
| <head> | |
| <!-- Book generated using mdBook --> | |
| <meta charset="UTF-8"> | |
| <title>rust_analyzer - rules_rust</title> | |
| <!-- Custom HTML head --> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <meta name="theme-color" content="#ffffff"> | |
| <link rel="icon" href="favicon.svg"> | |
| <link rel="shortcut icon" href="favicon.png"> | |
| <link rel="stylesheet" href="css/variables.css"> | |
| <link rel="stylesheet" href="css/general.css"> | |
| <link rel="stylesheet" href="css/chrome.css"> | |
| <link rel="stylesheet" href="css/print.css" media="print"> | |
| <!-- Fonts --> | |
| <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> | |
| <link rel="stylesheet" href="fonts/fonts.css"> | |
| <!-- Highlight.js Stylesheets --> | |
| <link rel="stylesheet" href="highlight.css"> | |
| <link rel="stylesheet" href="tomorrow-night.css"> | |
| <link rel="stylesheet" href="ayu-highlight.css"> | |
| <!-- Custom theme stylesheets --> | |
| <!-- Provide site root to javascript --> | |
| <script> | |
| var path_to_root = ""; | |
| var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light"; | |
| </script> | |
| <!-- Start loading toc.js asap --> | |
| <script src="toc.js"></script> | |
| </head> | |
| <body> | |
| <div id="body-container"> | |
| <!-- Work around some values being stored in localStorage wrapped in quotes --> | |
| <script> | |
| try { | |
| var theme = localStorage.getItem('mdbook-theme'); | |
| var sidebar = localStorage.getItem('mdbook-sidebar'); | |
| if (theme.startsWith('"') && theme.endsWith('"')) { | |
| localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); | |
| } | |
| if (sidebar.startsWith('"') && sidebar.endsWith('"')) { | |
| localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); | |
| } | |
| } catch (e) { } | |
| </script> | |
| <!-- Set the theme before any content is loaded, prevents flash --> | |
| <script> | |
| var theme; | |
| try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } | |
| if (theme === null || theme === undefined) { theme = default_theme; } | |
| const html = document.documentElement; | |
| html.classList.remove('light') | |
| html.classList.add(theme); | |
| html.classList.add("js"); | |
| </script> | |
| <input type="checkbox" id="sidebar-toggle-anchor" class="hidden"> | |
| <!-- Hide / unhide sidebar before it is displayed --> | |
| <script> | |
| var sidebar = null; | |
| var sidebar_toggle = document.getElementById("sidebar-toggle-anchor"); | |
| if (document.body.clientWidth >= 1080) { | |
| try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } | |
| sidebar = sidebar || 'visible'; | |
| } else { | |
| sidebar = 'hidden'; | |
| } | |
| sidebar_toggle.checked = sidebar === 'visible'; | |
| html.classList.remove('sidebar-visible'); | |
| html.classList.add("sidebar-" + sidebar); | |
| </script> | |
| <nav id="sidebar" class="sidebar" aria-label="Table of contents"> | |
| <!-- populated by js --> | |
| <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox> | |
| <noscript> | |
| <iframe class="sidebar-iframe-outer" src="toc.html"></iframe> | |
| </noscript> | |
| <div id="sidebar-resize-handle" class="sidebar-resize-handle"> | |
| <div class="sidebar-resize-indicator"></div> | |
| </div> | |
| </nav> | |
| <div id="page-wrapper" class="page-wrapper"> | |
| <div class="page"> | |
| <div id="menu-bar-hover-placeholder"></div> | |
| <div id="menu-bar" class="menu-bar sticky"> | |
| <div class="left-buttons"> | |
| <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> | |
| <i class="fa fa-bars"></i> | |
| </label> | |
| <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> | |
| <i class="fa fa-paint-brush"></i> | |
| </button> | |
| <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> | |
| <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li> | |
| <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> | |
| <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> | |
| <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> | |
| <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> | |
| </ul> | |
| <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> | |
| <i class="fa fa-search"></i> | |
| </button> | |
| </div> | |
| <h1 class="menu-title">rules_rust</h1> | |
| <div class="right-buttons"> | |
| <a href="print.html" title="Print this book" aria-label="Print this book"> | |
| <i id="print-button" class="fa fa-print"></i> | |
| </a> | |
| <a href="https://github.com/bazelbuild/rules_rust" title="Git repository" aria-label="Git repository"> | |
| <i id="git-repository-button" class="fa fa-github"></i> | |
| </a> | |
| </div> | |
| </div> | |
| <div id="search-wrapper" class="hidden"> | |
| <form id="searchbar-outer" class="searchbar-outer"> | |
| <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> | |
| </form> | |
| <div id="searchresults-outer" class="searchresults-outer hidden"> | |
| <div id="searchresults-header" class="searchresults-header"></div> | |
| <ul id="searchresults"> | |
| </ul> | |
| </div> | |
| </div> | |
| <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> | |
| <script> | |
| document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); | |
| document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); | |
| Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { | |
| link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); | |
| }); | |
| </script> | |
| <div id="content" class="content"> | |
| <main> | |
| <h1 id="rust-analyzer"><a class="header" href="#rust-analyzer">Rust Analyzer</a></h1> | |
| <p><code>rules_rust</code> ships a one-shot installer that configures | |
| <a href="https://rust-analyzer.github.io/">rust-analyzer</a> to use the project's Bazel | |
| toolchain. After setup, rust-analyzer, the proc-macro server, and rustfmt | |
| all come from Bazel — no host Rust install required.</p> | |
| <h2 id="quick-start"><a class="header" href="#quick-start">Quick start</a></h2> | |
| <p>Pick your editor below. Each runs the same <code>setup</code> tool with a different | |
| subcommand — <code>setup</code> is re-runnable any time.</p> | |
| <h3 id="vscode"><a class="header" href="#vscode">VSCode</a></h3> | |
| <ol> | |
| <li>Install the | |
| <a href="https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer">rust-analyzer extension</a>.</li> | |
| <li> | |
| <pre><code>bazel run @rules_rust//tools/rust_analyzer:setup -- vscode | |
| </code></pre> | |
| </li> | |
| <li>Reload the VSCode window.</li> | |
| </ol> | |
| <p><code>.vscode/settings.json</code> is always written; a <code>*.code-workspace</code> at | |
| the workspace root is picked up too. Existing user keys and comments | |
| survive re-runs, so <code>.vscode/settings.json</code> and <code>.code-workspace</code> are | |
| safe to commit.</p> | |
| <p>Add the launcher dir to <code>.gitignore</code>:</p> | |
| <pre><code>.vscode/.rules_rust_analyzer/ | |
| </code></pre> | |
| <p>Re-run <code>setup</code> after a toolchain change (rustup update, <code>MODULE.bazel</code> | |
| edit, <code>bazel clean --expunge</code>).</p> | |
| <h3 id="neovim"><a class="header" href="#neovim">Neovim</a></h3> | |
| <pre><code>bazel run @rules_rust//tools/rust_analyzer:setup -- neovim | |
| </code></pre> | |
| <p>Prints an <code>nvim-lspconfig</code> Lua snippet to stdout. Paste it into your | |
| <code>init.lua</code> (or pipe to a file you <code>require</code>). Restart Neovim.</p> | |
| <p>For <a href="https://github.com/mrcjkb/rustaceanvim"><code>rustaceanvim</code></a> users: | |
| pass the printed <code>cmd</code> and <code>settings</code> table through its <code>server</code> | |
| option (<code>vim.g.rustaceanvim = { server = { cmd = ..., settings = ... } }</code>).</p> | |
| <h3 id="helix"><a class="header" href="#helix">Helix</a></h3> | |
| <pre><code>bazel run @rules_rust//tools/rust_analyzer:setup -- helix | |
| </code></pre> | |
| <p>Prints a <code>languages.toml</code> snippet. Paste it into | |
| <code><workspace>/.helix/languages.toml</code>. Restart Helix.</p> | |
| <h3 id="other-editors-cocnvim-vim-lsp-ale-etc"><a class="header" href="#other-editors-cocnvim-vim-lsp-ale-etc">Other editors (<code>coc.nvim</code>, <code>vim-lsp</code>, ALE, etc.)</a></h3> | |
| <pre><code>bazel run @rules_rust//tools/rust_analyzer:setup -- print | |
| </code></pre> | |
| <p>Prints a generic JSON snippet using the <code>rust-analyzer.*</code> keys VSCode | |
| uses. <code>coc.nvim</code> reads them via <code>coc-settings.json</code> (open with | |
| <code>:CocConfig</code>); <code>vim-lsp</code> / ALE / <code>LanguageClient-neovim</code> accept the | |
| same keys via plugin-specific config files.</p> | |
| <h2 id="flags"><a class="header" href="#flags">Flags</a></h2> | |
| <p>Re-runnable at any time. Global flags work on any subcommand.</p> | |
| <div class="table-wrapper"><table><thead><tr><th>Flag</th><th>Effect</th></tr></thead><tbody> | |
| <tr><td><code>--skip-proc-macro-server</code></td><td>Don't manage the proc-macro key.</td></tr> | |
| <tr><td><code>--skip-rustfmt</code></td><td>Don't manage the formatter key (use host rustfmt).</td></tr> | |
| <tr><td><code>--per-package-workspaces</code> / <code>--no-per-package-workspaces</code></td><td>Opt this developer in/out of per-package workspace splitting (see below).</td></tr> | |
| <tr><td><code>--clippy</code> / <code>--no-clippy</code></td><td>Opt this developer in/out of running clippy on save and streaming its diagnostics alongside rustc's.</td></tr> | |
| <tr><td><code>--clean</code></td><td>Delete <code><launcher-dir>/cache/</code> before running the rest of setup. See Troubleshooting.</td></tr> | |
| </tbody></table> | |
| </div> | |
| <p>The <code>--clippy</code> and <code>--per-package-workspaces</code> toggles are <strong>per-user</strong>: they mutate <code><launcher-dir>/user_config.json</code> (gitignored) instead of the shared committed settings file. Two developers on the same workspace can hold different preferences without touching the checked-in configuration. Editing <code>user_config.json</code> by hand works too.</p> | |
| <p>The <code>vscode</code> subcommand adds:</p> | |
| <div class="table-wrapper"><table><thead><tr><th>Flag</th><th>Effect</th></tr></thead><tbody> | |
| <tr><td><code>--settings-json <path></code></td><td>Override the settings.json output. Defaults to <code><workspace>/.vscode/settings.json</code>.</td></tr> | |
| <tr><td><code>--code-workspace <path></code></td><td><code>.code-workspace</code> file to also update. Required when the workspace root has more than one.</td></tr> | |
| <tr><td><code>--no-code-workspace</code></td><td>Skip the <code>.code-workspace</code> write.</td></tr> | |
| <tr><td><code>--settings-key <key></code></td><td>Nest managed keys under this key inside the <code>.code-workspace</code> (default: <code>settings</code>).</td></tr> | |
| <tr><td><code>--dry-run</code></td><td>Print each would-be-written file to stdout.</td></tr> | |
| <tr><td><code>--replace</code></td><td>Overwrite managed keys instead of merging. Sibling <code>folders</code> / <code>tasks</code> / <code>extensions</code> in a <code>.code-workspace</code> survive.</td></tr> | |
| </tbody></table> | |
| </div> | |
| <h2 id="what-you-get"><a class="header" href="#what-you-get">What you get</a></h2> | |
| <ul> | |
| <li><strong><code>▶ Run Tests</code> / <code>▶ Run Test</code></strong> codelens on every <code>#[cfg(test)] mod</code> and | |
| individual <code>#[test]</code>.</li> | |
| <li><strong>On-save squiggles</strong> from rustc diagnostics. Matches <code>cargo check</code> — | |
| errors anywhere in the dep graph surface at their actual file paths.</li> | |
| <li><strong>Format-on-save</strong> via the Bazel-toolchain rustfmt.</li> | |
| <li><strong>Workspace reload</strong> on watched <code>BUILD</code> / <code>MODULE.bazel</code> changes.</li> | |
| </ul> | |
| <h2 id="troubleshooting"><a class="header" href="#troubleshooting">Troubleshooting</a></h2> | |
| <h3 id="symbols--deps-look-wrong"><a class="header" href="#symbols--deps-look-wrong">Symbols / deps look wrong</a></h3> | |
| <p>Restart rust-analyzer (or save a <code>BUILD</code> file). If that doesn't fix | |
| it, re-run setup with <code>--clean</code> to nuke the discovery cache:</p> | |
| <pre><code>bazel run @rules_rust//tools/rust_analyzer:setup -- --clean vscode | |
| </code></pre> | |
| <p>Works with any subcommand (<code>vscode</code> / <code>neovim</code> / <code>helix</code> / <code>print</code>).</p> | |
| <h3 id="diagnostics-stopped-appearing"><a class="header" href="#diagnostics-stopped-appearing">Diagnostics stopped appearing</a></h3> | |
| <p>Check <code><workspace>/.rules_rust_analyzer/flycheck.log</code>.</p> | |
| <h3 id="after-bazel-clean---expunge-or-toolchain-changes"><a class="header" href="#after-bazel-clean---expunge-or-toolchain-changes">After <code>bazel clean --expunge</code> or toolchain changes</a></h3> | |
| <p>Re-run <code>setup</code>.</p> | |
| <h3 id="noisy-cargo-metadata-errors-on-startup"><a class="header" href="#noisy-cargo-metadata-errors-on-startup">Noisy <code>cargo metadata</code> errors on startup</a></h3> | |
| <p><code>setup</code> does not manage <code>rust-analyzer.files.excludeDirs</code>. If your | |
| workspace has stub <code>Cargo.toml</code> files that aren't meant to be | |
| auto-loaded (common in <code>rules_rust</code> itself under <code>examples/</code>, | |
| <code>crate_universe/</code>, etc.), rust-analyzer still finds them and logs | |
| errors. Silence them by adding the directory names to <code>settings.json</code> | |
| yourself — your entries survive future <code>setup</code> runs:</p> | |
| <pre><code>"rust-analyzer.files.excludeDirs": ["examples", "some_other_dir"] | |
| </code></pre> | |
| <p>Trade-off: <code>files.excludeDirs</code> also hides those sources from | |
| rust-analyzer's virtual filesystem, so files under those directories | |
| won't get IDE features even if they're part of a Bazel-discovered | |
| crate. Only exclude directories whose sources you're willing to lose | |
| IDE support on.</p> | |
| <h2 id="workspace-splitting"><a class="header" href="#workspace-splitting">Workspace splitting</a></h2> | |
| <p>By default the whole project is treated as a single workspace.</p> | |
| <p>For monorepos where indexing the whole graph is too slow, pass | |
| <code>--per-package-workspaces</code>. Discover then scopes to the saved file's | |
| package + deps; rust-analyzer reloads when you jump to a different | |
| package. Caveat: <em>dependents</em> of the package you're working on aren't | |
| indexed, so "find usages" can miss callers in other packages.</p> | |
| <p>Switch any time by re-running <code>setup</code> with or without the flag.</p> | |
| <h2 id="debugging"><a class="header" href="#debugging">Debugging</a></h2> | |
| <p>The <code>▶ Debug</code> codelens VSCode renders next to <code>#[test]</code> functions | |
| <strong>does not work</strong> for Bazel projects. Use <code>.vscode/launch.json</code> + F5 | |
| instead:</p> | |
| <pre><code>bazel run @rules_rust//tools/vscode:gen_launch_json | |
| </code></pre> | |
| <p>Install | |
| <a href="https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb">CodeLLDB</a> | |
| first. Set a breakpoint inside the test and run the target — one | |
| launch config covers every test in that binary.</p> | |
| </main> | |
| <nav class="nav-wrapper" aria-label="Page navigation"> | |
| <!-- Mobile navigation buttons --> | |
| <a rel="prev" href="extract_cargo_lints.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> | |
| <i class="fa fa-angle-left"></i> | |
| </a> | |
| <a rel="next prefetch" href="unpretty.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> | |
| <i class="fa fa-angle-right"></i> | |
| </a> | |
| <div style="clear: both"></div> | |
| </nav> | |
| </div> | |
| </div> | |
| <nav class="nav-wide-wrapper" aria-label="Page navigation"> | |
| <a rel="prev" href="extract_cargo_lints.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> | |
| <i class="fa fa-angle-left"></i> | |
| </a> | |
| <a rel="next prefetch" href="unpretty.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> | |
| <i class="fa fa-angle-right"></i> | |
| </a> | |
| </nav> | |
| </div> | |
| <script> | |
| window.playground_copyable = true; | |
| </script> | |
| <script src="elasticlunr.min.js"></script> | |
| <script src="mark.min.js"></script> | |
| <script src="searcher.js"></script> | |
| <script src="clipboard.min.js"></script> | |
| <script src="highlight.js"></script> | |
| <script src="book.js"></script> | |
| <!-- Custom JS scripts --> | |
| </div> | |
| </body> | |
| </html> |