Better Rust/Cargo support for Flycheck

Related tags

IDEs flycheck-rust
Overview

flycheck-rust — Flycheck for Rust

Build Status License GPL 3 MELPA

This Flycheck extension configures Flycheck automatically for the current Cargo project.

Setup

Install from MELPA or MELPA Stable.

If you use Cask:

(source gnu)
(source melpa)

(depends-on "flycheck-rust")

Then, in your init.el:

(with-eval-after-load 'rust-mode
  (add-hook 'flycheck-mode-hook #'flycheck-rust-setup))

Usage

Just use Flycheck as usual in your Rust/Cargo projects. flycheck-rust-setup will call cargo to determine your project layout and set the variables needed by the rust-cargo checker to properly check your buffer.

You may also want to have a look at flycheck/flycheck-inline, which works well with diagnostics emitted by Rust.

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

See COPYING for details.

Comments
  • Emacs blocks on flycheck-rust-find-target

    Emacs blocks on flycheck-rust-find-target

    Hello, When using this function directly or automatically through flycheck it blocks up the UI for a few seconds. Is it possible to make this not block?

    help wanted wontfix 
    opened by Pireax 30
  • Error related to rust-cargo

    Error related to rust-cargo

    Suspicious state from syntax checker rust-cargo: Checker rust-cargo returned non-zero exit code 101, but no errors from output: extra arguments to `rustc` can only be passed to one target, consider filtering
    the package by passing e.g. `--lib` or `--bin NAME` to specify a single target
    

    I believe this is related to #16 and #18. I'm using the latest version from Dec 25 and getting this message when I save any file on my project. The project layout is:

    src/
    ├── config.rs
    ├── error.rs
    ├── lib.rs
    ├── main.rs
    

    Any ideas about this? Is there something I'm doing wrong?

    Thank you.

    help wanted 
    opened by carlosgaldino 28
  • Extract build settings and project layout from Cargo itself

    Extract build settings and project layout from Cargo itself

    Don't guess, use Cargo's own settings. Blocked by https://github.com/rust-lang/cargo/pull/1434

    Specifically: Extract the layout, the crate roots, the dependencies, and probably compiler flags as well. Will probably require changes to Flycheck's rust checker, to pass additional flags.

    Fixes: #5, #7, probably #11

    enhancement upstream 
    opened by swsnr 27
  • unresolved name `rand::thread_rng`

    unresolved name `rand::thread_rng`

    Hello,

    I have the following code which compiles fine via cargo run:

    src/main.rs

    extern crate rand;
    
    use rand::distributions::{IndependentSample, Range};
    
    fn main() {
        let mut rng = rand::thread_rng(); // Flycheck error is here
        let range = Range::new(-512.03, 511.97);
        println!("A random number is {}", range.ind_sample(&mut rng));
    }
    

    Cargo.toml

    [package]
    name = "rando"
    version = "0.1.0"
    authors = ["Andrew Schwartzmeyer <[email protected]>"]
    
    [dependencies]
    rand = "0.3"
    

    However, Flycheck with flycheck-rust gives the warning "unresolved name rand::thread_rng" on line 6. It does not appear to be recognize that the function was imported from a crate.

    The Flycheck project may be a better place to post this issue, but I was hoping you could help me out!

    rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
    cargo 0.2.0-nightly (efb482d 2015-04-30) (built 2015-04-29)
    OS X 10.10.3
    GNU Emacs 24.5.50.1 (x86_64-apple-darwin14.3.0, NS apple-appkit-1347.57) of 2015-04-13
    Flycheck version: 0.24snapshot (package: 20150514.528)
    flycheck-rust package 20150417.1048
    

    I am using the rand crate.

    bug 
    opened by andschwa 17
  • flycheck-rust-find-target: Wrong type argument: stringp, nil

    flycheck-rust-find-target: Wrong type argument: stringp, nil

    Hi.

    When running flycheck-rust-setup I get this error everytime. Then flycheck checks nothing, nothing is underlined and running flycheck-buffer manually does nothing (no error).

    opened by navaati 16
  • Refactor flycheck-rust using cargo

    Refactor flycheck-rust using cargo

    This PR is there to start a discussion on the basis of a working implementation, but is not meant to merge as-is until the discussion is over.

    The gist of it is that I'm proposing to:

    1. Use cargo (and only cargo) to find the build target for the current file, and set the variables used by rust-cargo accordingly.
    2. Leave the rust checker for single rust files, which means not setting up any variables for it in flycheck-rust.

    And as I'm unsure whether this will break anyone's setup, I'm asking for feedback.

    cc @flycheck/rust

    Read on for the details.

    To fix #38, I figured I might as well clean up the logic a bit in flycheck-rust, which was getting a bit redundant. Originally, it seems we used various methods to get the information to pass to the rustc checker in order to be able to check single rust files. Then we started to use cargo directly to get this information, and now we have two ways of doing things. I think that #8 intended for cargo to be the main way to extract the information needed to run the rust checkers, and this PR aims to do that.

    As I understand it, the role of flycheck-rust is to set the variables used by the rust checkers in flycheck, so the checkers can produce meaningful errors for the current file. The rust checkers (especially rust-cargo) won't actually do anything without the proper variables set first, especially flycheck-rust-crate-type and flycheck-rust-binary-name. These variables could also be set directly by the user, by file-local variables or .dir-locals.el or any other means. Flycheck-rust will just try to set them when their value can be found automatically.

    Also, I understand there's a slight mismatch between flycheck checkers that are meant to run on single files, but cargo only builds targets, not individual files. We cannot point cargo to the file X and say "give me the compilation errors for X". We have to say "give me compilation errors for the binary crate Y", where crate Y contains and makes use of the file X, and we will get errors for X (among all the other files in Y). The problem with that is that we have no way to know for sure that file X is part of crate Y. cargo won't tell us, and we would need to resolve the imports and build a dependency graph to know the answer; that's certainly out of scope. The only cases where we can be certain is when cargo read-manifest gives us the exact filenames for targets. Other than that, we can make educated guesses based on the conventional cargo layout (e.g., any file under src/ other than src/main.rs contribute to the library target). These guesses may fail if the project actually does not follow the convention, but then the user can always override these guesses with local variables.

    I set up a test crate, added all the relevant test cases from the conventional cargo layout I could find, and just used cargo to find the correct target to build based on the buffer filename. Even when deleting most of the code, it just works and sets up the flycheck-rust-crate-type and flycheck-rust-binary-name correctly for all the test cases.

    This fixes #38, increases our coverage, and gets rid of most of the code. However, I don't know if I've broken anything else, especially for the plain rust checker.

    I left out setting these variables:

    • flycheck-rust-crate-root is unneeded for rust-cargo and might just be redundant with flycheck-rust-binary-name
    • flycheck-rust-check-tests: I don't understand why we would skip tests in executables. It's less surprising if all the code is checked, including code behind the #[cfg(test)] feature.
    • flycheck-rust-library-path is unneeded for rust-cargo as well, as cargo already handles passing the dependencies to rustc. It certainly is needed for the rust checker, but only when building crates, which brings me to the last point.

    As I gathered from #8, the use case for the bare rust checker is to check single files that are not part of a cargo project (like StackOverflow answers, or minimal working examples), leaving the rust-cargo checker for pretty much anything else. Though if that is the case, I don't understand why we we pass the variable flycheck-rust-crate-type to rustc in the rust checker, or flycheck-rust-library-path, especially as we have flycheck-rust-args to already pass arbitrary arguments to rustc. That may be a separate PR to flycheck if it turns out these variables are indeed superfluous.

    Note: an alternative would be to add the ability to cargo to give us a list of build targets a file is associated with. That way, we could just get rid of flycheck-rust altogether and just invoke that new command in rust-cargo directly. I don't know if the cargo maintainers would be open to it, or how complex it would be to implement (I'm guessing not much, since the dependency information should already be available, but I haven't checked).

    needs-input 
    opened by fmdkdd 15
  • Error from syntax checker rust-cargo: JSON readtable error

    Error from syntax checker rust-cargo: JSON readtable error

    Syntax checkers for buffer main.rs in rust-mode:
    
      rust-cargo
        - may enable:  yes
        - predicate:   t
        - executable:  Found at /Users/zwb/.cargo/bin/cargo
        - Cargo.toml:  Found
        - Crate type:  bin
        - Binary name: iron-gcd
    
      rust
        - may enable: yes
        - predicate:  t
        - executable: Found at /Users/zwb/.cargo/bin/rustc
    
      rust-clippy (disabled)
        - may enable: Automatically disabled!
        - predicate:  t
        - executable: Found at /Users/zwb/.cargo/bin/cargo
        - Clippy:     Cannot find the `cargo clippy' command
        - Cargo.toml: Found
    
    Flycheck Mode is enabled. Use SPC u C-c ! x to enable disabled checkers.
    
    --------------------
    
    Flycheck version: 32snapshot
    Emacs version:    25.3.1
    System:           x86_64-apple-darwin17.3.0
    Window system:    mac
    
    opened by ghost 13
  • flycheck-rust-setup errors

    flycheck-rust-setup errors

    I'm getting the following errors when opening a Rust buffer:

    Error in flycheck-rust-setup: (invalid-function (packages (let-alist (with-temp-buffer (call-process cargo nil t nil "metadata" "--no-deps" "--manifest-path" manifest) (goto-char (point-min)) (let ((json-array-type (quote list))) (json-read))) \.packages)))
    Suspicious state from syntax checker rust-cargo: Flycheck checker rust-cargo returned non-zero exit code 101, but its output contained no errors: error: no library targets found
    
    Try installing a more recent version of rust-cargo, and please open a bug report if the issue persists in the latest release.  Thanks!
    

    My init.el setup:

    (use-package rust-mode
      :defer t
      :mode "\\.rs\\'"
      :config (setq rust-format-on-save t)
      )
    
    ;; Run cargo commands in rust buffers, e.g. C-c C-c C-r for cargo-run
    (use-package cargo
      :init
      (add-hook 'rust-mode-hook 'cargo-minor-mode)
      (add-hook 'toml-mode-hook 'cargo-minor-mode)
      )
    
    (use-package racer
      :init
      (add-hook 'rust-mode-hook #'racer-mode)
      (add-hook 'racer-mode-hook #'eldoc-mode)
      :config
      (define-key rust-mode-map (kbd "TAB") #'company-indent-or-complete-common)
      (setq racer-rust-src-path "/usr/local/src/rust/src")
      )
    
    ;; Doesn't work, json-read-error
    (use-package flycheck-rust
      :init
      (add-hook 'flycheck-mode-hook #'flycheck-rust-setup))
    

    Running flycheck-verify-setup:

    Syntax checkers for buffer main.rs in rust-mode:
    
      rust-cargo
        - may enable:  yes
        - predicate:   t
        - executable:  Found at /usr/local/bin/cargo
        - Cargo.toml:  Found
        - Crate type:  lib
        - Binary name: Not required
    
      rust
        - may enable: yes
        - predicate:  t
        - executable: Found at /usr/local/bin/rustc
    
    Flycheck Mode is enabled. Use C-u C-c ! x to enable disabled checkers.
    
    --------------------
    
    Flycheck version: 31snapshot (package: 20170324.340)
    Emacs version:    26.0.50
    System:           x86_64-pc-linux-gnu
    Window system:    x
    
    opened by mrcnski 13
  • Getting couldn't read

    Getting couldn't read "src/main.rs": No such file or directory (os error 2)

    flycheck-rust reports this error whenever I open a rust file.

    The file src/main.rs exists and is the file I am currently opening. Do I miss some config here?

    I am using only rust-mode and flycheck-rust from melpa, with:

    (require 'sm-flycheck)
    
    (add-hook 'rust-mode-hook 'flycheck-mode)
    (add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
    (add-hook 'rust-mode-hook 'subword-mode)
    

    Thanks

    opened by simao 13
  • Only checks syntax on save

    Only checks syntax on save

    It seems that flycheck-rust only checks syntax when the file is saved, even if the appropriate variables are present in flycheck-check-syntax-automatically.

    Its value is (save idle-change new-line mode-enabled), and yet idle-change and new-line don't trigger the syntax check.

    upstream 
    opened by zakkor 13
  • On a Cargo project, to get flycheck running okay is mandatory lib.rs?

    On a Cargo project, to get flycheck running okay is mandatory lib.rs?

    Because when I try check projects without lib.rs (only main.rs) I got this on C-c ! C-c rust-cargo RET on any file:

    -*- mode: compilation; default-directory: "~/Desktop/project-church/src/" -*-
    Compilation started at Sat Jul 15 22:00:17
    
    cargo rustc --lib --message-format\=json -- -Z no-trans --test
    error: no library targets found
    
    Compilation exited abnormally with code 101 at Sat Jul 15 22:00:17
    
    
    opened by ryukinix 11
  • Error in rustic-flycheck-setup: (user-error

    Error in rustic-flycheck-setup: (user-error "flycheck-rust cannot find ‘cargo’. Please make sure that cargo is installed and on your PATH.

    Hi I am using doom emacs and my configuration is simple

    (rust +lsp) in init.el

    (use-package! rustic)

    that is it, But i got an error Error in rustic-flycheck-setup: (user-error "flycheck-rust cannot find ‘cargo’. Please make sure that cargo is installed and on your PATH.

    how can I solve this problem?

    opened by freewebwithme 1
  • ensure json-read returns cargo manifest as an alist

    ensure json-read returns cargo manifest as an alist

    json-read from json.el can return a non-alist value when json-object-type is set to a value distinct from 'alist. Since the let-alist macro expects to receive an alist, flycheck-rust-get-cargo-targets will raise a wrong-type-argument error and prevent flycheck-rust from loading when json-object-type is not 'alist. (In my case, json-object-type was set to 'hash-table either by default or by some other package.)

    opened by bassettmb 5
Owner
Flycheck
On the fly syntax checking for Emacs
Flycheck
Rust IDE support for Atom, powered by the Rust Language Server (RLS)

IDE-Rust Rust language support for Atom-IDE, powered by rust-analyzer. Features Auto-completion Diagnostics (errors and warnings from rustc) Document

The Rust Programming Language 239 Dec 14, 2022
Rust language support in Atom - LOOKING FOR MAINTAINER, see #144

Rust language support in Atom Adds syntax highlighting and snippets to Rust files in Atom. Install Install the package language-rust in Atom (Preferen

Andreas Neuhaus 118 Oct 11, 2022
Rust extension for Visual Studio 2017 with RLS support

Rust support for Visual Studio 2017 Preview Adds language support for Rust to Visual Studio 2017. Supports: code completion goto definition find all r

Zoey Riordan 111 Aug 4, 2022
Racer support for Emacs

Racer for Emacs This is the official Emacs package for Racer. Table of Contents Racer for Emacs Completion Find Definitions Describe Functions and Typ

null 398 Nov 30, 2022
Racer support for Vim

Vim Racer Plugin This plugin allows vim to use Racer for Rust code completion and navigation. Installation Build / Install Racer Install using Pathoge

null 624 Dec 7, 2022
Language Server Protocol (LSP) support for vim and neovim.

For legacy python implementation, see branch master. LanguageClient-neovim Language Server Protocol support for vim and neovim. More recordings at Upd

Junfeng Li 3.5k Dec 29, 2022
Fennel language server protocol (LSP) support.

fennel-language-server Fennel language server protocol (LSP) support. fennel-language-server is currently in a very early stage and unreliable. Use it

null 68 Dec 27, 2022
rust-analyzer is a modular compiler frontend for the Rust language

rust-analyzer is a modular compiler frontend for the Rust language. It is a part of a larger rls-2.0 effort to create excellent IDE support for Rust.

null 11.2k Jan 8, 2023
Eclipse Corrosion - Rust edition in Eclipse IDE

Eclipse Corrosion Rust edition and debug in Eclipse IDE Corrosion is a Rust development plugin for the Eclipse IDE, providing a rich edition experienc

Eclipse Foundation 194 Dec 23, 2022
RustDT is an Eclipse based IDE for the Rust programming language:

Project website: http://rustdt.github.io/ As of 2017, RustDT is no longer actively maintained, see this blog post for more information. If you are int

null 351 Aug 20, 2022
Emacs configuration for Rust

Table of Contents Introduction Installation Melpa Manual installation Feature guide Indentation Code formatting Running / testing / compiling code Cli

The Rust Programming Language 919 Jan 4, 2023
Rust development environment for Emacs

Rustic Table of Contents Rustic Intro Installation straight Compilation Faces rustc errors Rustfmt edition 2018 LSP Server Client eglot lsp-mode lsp-e

null 612 Dec 30, 2022
NetBeans Rust plugin

Rust NetBeans Plugin A NetBeans plugin for Rust. Linux / OSX Windows Requirements NetBeans 8.2.x Java 8+ Rust Cargo Rustup Features So far, it include

drrb 52 Oct 10, 2022
Rust plugin for the IntelliJ Platform

Rust plugin for the IntelliJ Platform Build Status Check Stable Beta Nightly Installation & Usage Available installation options and features are desc

IntelliJ Rust 4.2k Jan 2, 2023
Rust IDE

This branch contains the development of a "new ride" that maintain a small impact on the ui library. This is for a few reasons. Can customize the colo

Gustav Jansson 171 Dec 24, 2022
An IDE for Rust

Introduction SolidOak is a simple IDE for Rust. See the website for binary releases. It has the following features: An embedded copy of Neovim as its

Zach Oakes 907 Dec 29, 2022
The official Sublime Text 3 package for the Rust Programming Language

Rust Enhanced About This is a Sublime Text 3 package which supports Rust starting with version 1.0, it makes no attempt at supporting earlier incompat

The Rust Programming Language 704 Jan 7, 2023
Vim configuration for Rust.

rust.vim Description This is a Vim plugin that provides Rust file detection, syntax highlighting, formatting, Syntastic integration, and more. It requ

The Rust Programming Language 3.4k Jan 8, 2023
Visual Studio extension for Rust

Visual Studio extension for Rust Currently in development, and not feature complete. Stable versions are available on the Visual Studio extension gall

PistonDevelopers 697 Dec 18, 2022