flycheck / flycheck-rust

Better Rust/Cargo support for Flycheck
GNU General Public License v3.0
121 stars 19 forks source link

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

Closed ryukinix closed 7 years ago

ryukinix commented 7 years ago

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
fmdkdd commented 7 years ago

Are you using flycheck-rust in addition to flycheck? flycheck-rust takes care of setting the correct cargo target for you, depending on the current buffer, but you have to set it up manually.

ryukinix commented 7 years ago

Yes, I installed flycheck-rust and works well when I have a file lib.rs on cargo project or I edit a simple file. On my initial startup I added a hook (add-hook 'flycheck-mode-hook #'flycheck-rust-setup) to load the flycheck-rust-setup in that way:

;;; Code:

(require 'prelude-programming)

(prelude-require-packages '(rust-mode
                            racer
                            flycheck-rust
                            cargo))

(eval-after-load 'rust-mode
  '(progn
     (add-hook 'rust-mode-hook #'racer-mode)
     (add-hook 'racer-mode-hook #'eldoc-mode)
     (add-hook 'racer-mode-hook #'company-mode)
     (add-hook 'rust-mode-hook #'flycheck-rust-setup)
     (add-hook 'flycheck-mode-hook #'flycheck-rust-setup)

     (defun prelude-rust-mode-defaults ()
       (local-set-key (kbd "TAB") #'company-indent-or-complete-common)
       (define-key (current-local-map) (kbd "C-c C-c") #'compile)
       (setq-local company-tooltip-align-annotations t)

       ;; CamelCase aware editing operations
       (subword-mode +1))

     (setq prelude-rust-mode-hook 'prelude-rust-mode-defaults)
     (setq rust-format-on-save t)

     (add-hook 'rust-mode-hook (lambda ()
                               (run-hooks 'prelude-rust-mode-hook)))))

(provide 'prelude-rust)
;;; prelude-rust.el ends here
ryukinix commented 7 years ago

If you want try replicate the same problem, you can clone this repository in that commit (before I added lib.rs, because this fixed).

Steps:

  1. Clone the repository
    git clone https://github.com/ryukinix/conway-rs.git
    cd conway-rs
    git checkout d5a36114cd218b4f78e2f7fe22f14461bb748f48
  2. Try edit any file inside src/

I always receive on my mini-buffer:

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!
user-error: Minibuffer window is not active
fmdkdd commented 7 years ago

Okay, then we will have to dig deeper. For me, opening the main.rs in a project created by cargo new --bin foo works. Here is what flycheck-verify-setup reports:

 rust-cargo
    - may enable:  yes
    - predicate:   t
    - executable:  Found at /usr/bin/cargo
    - Cargo.toml:  Found
    - Crate type:  bin
    - Binary name: foo

You can also check the value of Crate type using flycheck-rust-crate-type. This variable should be set by flycheck-rust-setup. Do you get bin or lib in this case?

If you get lib, then please report the following:

If you get bin, but you still get lib for your own project, then please report the last two commands for your own project as well.

fmdkdd commented 7 years ago

@ryukinix Didn't see your message before my previous reply. Your project is correctly detected for me:

  rust-cargo
    - may enable:  yes
    - predicate:   t
    - executable:  Found at /usr/bin/cargo
    - Cargo.toml:  Found
    - Crate type:  bin
    - Binary name: conway

So the previous reply still stands. Also, please make sure you have the latest flycheck-rust.

ryukinix commented 7 years ago

Thanks for the fast reply and the detailed description. This is really weird! :( I don't know what more I can do. Maybe can something related to the my init.el stuff? I'm thinking about that because one thing bothered me: the C-c C-c is not binding to the 'compile on rust-mode. My installed version of flycheck-rust is: 20170404.842. The last from melpa. This is the latest, right?

Well... how far you tested, seems this only my problem :laughing:. But really thanks. I'll give a feedback when fixed that. BTW I'll put here all the info you requested on the previous message.

ryukinix commented 7 years ago

You can also check the value of Crate type using flycheck-rust-crate-type. This variable should be set by flycheck-rust-setup. Do you get bin or lib in this case?

Value: "lib" Expected: "bin"

cargo --version and rustc --version (we need Rust >=1.15)

cargo 0.21.0-nightly (70b89fdd7 2017-07-10)
rustc 1.20.0-nightly (6d9d82d3d 2017-07-14)

the output of (flycheck-rust-find-cargo-target (buffer-file-name)) in the main.rs buffer

Output: ("bin" . "conway")

the output of cargo metadata --no-deps --format-version 1 for the foo project (this may disclose part of your file hierarchy; create the foo crate in /tmp if you care about that)

❯ cargo metadata --no-deps --format-version 1
{"packages":[{"name":"conway","version":"0.1.0","id":"conway 0.1.0 (path+file:///home/lerax/Desktop/conway-rs)","license":null,"license_file":null,"description":null,"source":null,"dependencies":[{"name":"rand","source":"registry+https://github.com/rust-lang/crates.io-index","req":"^0.3","kind":null,"optional":false,"uses_default_features":true,"features":[],"target":null}],"targets":[{"kind":["bin"],"crate_types":["bin"],"name":"conway","src_path":"/home/lerax/Desktop/conway-rs/src/main.rs"}],"features":{},"manifest_path":"/home/lerax/Desktop/conway-rs/Cargo.toml"}],"workspace_members":["conway 0.1.0 (path+file:///home/lerax/Desktop/conway-rs)"],"resolve":null,"target_directory":"/home/lerax/Desktop/conway-rs/target","version":1}

I want remember you that I'm executing this tests on the version of the repository at which doesn't contains the lib.rs file as I explained before.

ryukinix commented 7 years ago

How I can ensure that flycheck-rust-setup are been called?

ryukinix commented 7 years ago

WELLL, sorry for the shame! The whole problem was my init.el setup which are not calling the my rust hooks! My shame, my bad.

Thanks for all the support! You helped me try to figured out what are happening. Fixed problem. Thanks for the helps! Great package!

Cheers.

fmdkdd commented 7 years ago

No worries. Glad it works for you, and have fun with Rust!

knappador commented 5 years ago

Regarding "not calling my rust hooks" correct hook is as follows:

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

As documented on https://github.com/flycheck/flycheck-rust