Using Atom to work with Rust under Win32


#1

Dear all,

I have no clue if this is the right topic for such kind of information but because I’ve struggled for some time to bring Atom, racer and rust-language support together under Win32 I thought it could be of some value to other Win32 devs if I write it down.

Some time ago I’ve tried to activate the Rust code completion under Atom. Sadly, it didn’t work on my machine so I informed the maintainers about this issue: https://github.com/edubkendo/atom-racer/issues/45#issuecomment-126720989

At this time I used a pre-1.0 version of Racer and Atom v.1.0.

The problems were two fold: the code completion was not fully active (only certain namespaces were shown) and I always had to press CTRL+SPACE to activate it.

Of course, such workflow isn’t very productive so I gave up…and went back to Sublime3 :frowning:

Now, a few days ago Racer got updated to version 1.0 and I tried to install all components again. And it worked! :smile:

My set-up consists of following components:

a) Atom 1.0.3 editor https://atom.io/
b) rust-language support: https://atom.io/packages/language-rust
c) racer-addin for Atom: https://atom.io/packages/racer
d) racer itself: https://github.com/phildawes/racer
e) build-addin for Atom (to get nice Cargo.toml support under Atom): https://atom.io/packages/build

Installation/Config steps are as follows:

a) Atom install package needs to be downloaded, the rest is just Win32-typical click/click/click :wink:

b) Now, go to the Settings-page and under Install type “rust” and install the package “rust-language” from the list of available packages.

c) Then go to the Racer github pages (the binary, not the addin…this comes later) and clone the sources. For cloning either use “git clone” via Console, or a tool like this one: https://windows.github.com/

d) Build Racer with cargo build --release and move the whole target/release directory to your preferred path. On my machine I have it under C:\bin

e) Now, install the racer support under Atom. Again, go to Settings/Packages/Install and type “racer”.

f) Configure the Racer-Addin by clicking on it in the Settings/Packages list and selecting its own Settings option. You’ll see a list like this:

We see that Racer-Addin needs two paths, one of the racer binary you just compiled but also the path to the Rust sources itself. Racer needs this path to provide you the nice code-completions. Therefore, you must clone Rust sources from GitHub. In my case I was using the stable branch. You can decide on your own which sources you’ll use. What’s important is that you point to the /src sub-directory of the Rust-source tree.

So, after having completed all this steps the code completion will work:

But, this is not the End (…my only friend, the EndThe Doors) :beers:

We also want to use Cargo directly from Atom :smile:

Luckilly, there’s an incredibly cool package capable of compiling different sources/project types. And it understands Cargo.toml!

Atom Build: https://atom.io/packages/build

Install it via Settings/Install then go to its own Settings page and activate/deactivate options according to your preferences (save-on-build, build-on-save, auto-scroll-on-error etc.)

Now you’ll be able to execute an existing Cargo.toml via CTRL-ALT-B (or by any other key-combination you set up)

But, sometimes one would like to quickly compile a single *.rs file without creating a complete project. I myself often want to execute some nice scripts because I’m still learning the language, so there’s really no need for me to always set-up a complete project just to get some “hello world” executed.

Therefore I created this .atom-build.json config file for Atom-Build which I put into a root directory where all my “single-rust-sources” live.

{
  "cmd": "rustc",
  "args": [ "-v","{FILE_ACTIVE}", "--out-dir", "{FILE_ACTIVE_PATH}" ],
  "sh": false,
  "cwd": "{PROJECT_PATH}",
  "errorMatch": "^(?<file>[^\\.]+.rs):(?<line>\\d+):(?<col>\\d+)",
  "keymap": "alt-c"
}

This file instructs the Atom-Build package to take care of the current active file and feed the rustc compiler with it. The output (that is, the win32 EXE file), will be placed into the directory where the source file lives.

Now, when I hit ALT-C my single source file gets compiled and the rust-output is placed under the same path:

For more information on different Options/Flags of the Atom-Build Addin, please, consult their nice docs: https://atom.io/packages/build

This is it.

I hope this little intro will be of some value to other Win32 developers.

Best regards,
Harris


Adding a tutorial tag?
#2

This is awesome. Thanks. I’m planning on doing more work under Windows so I’ll try these instructions soon.


#3

I have not been as successful using racer within Atom on Windows. I contributed a few patches in the leadup to the 1.0 release but I still have no clue why completions fail to work for most crates, but only on Windows. It works fine on Mac and Linux. Best of luck to everyone else.


#4

related: http://stackoverflow.com/questions/30496599/how-to-create-build-run-and-debug-a-rust-program-with-the-atom-editor

Thanks for writing such a detailed explanation and instructions


#5

These instructions work for linux as well but CTRL-ALT-B doesn’t build as it’s already taken by some “vim-like” “go back one word” command.

These are really good instructions.


#6

I try racer 1.0 on Atom, stiil can not use it on Win64. :disappointed_relieved: Don’t know where is wrong. And Atom Rust linter is not working for cargo project.

Sublime Text 3 is working well.


#7

Couple of things:

  1. Copying racer.exe to the Rust bin directory (or alternatively, copying libgcc and family to the racer.exe directory) is required. This PR will make that clearer when this isn’t done: https://github.com/edubkendo/atom-racer/pull/47
  2. linter-rust's cargo mode will build your crate to lint. This will build all of your dependencies. Make sure that your dependencies are built before using the linter.
  3. racer doesn’t currently handle inferred types well. If you want completions on existing bindings, you need to explicitly annotate their type.
  4. racer doesn’t handle re-exports from other crates e.g. piston::window -> pistoncore_window

#8

Thanks a lot, everything works OK :smile:

Now the question: is it possible to run compiled exe with redirect to an Atom panel?


#9

Hi vaskir,

It depends on what kind of toolset you’re using to run scripts. For example, you could use the Atom-Build plugin to execute compiled code. Check their docs for available variables like {FILE_ACTIVE}

You’d then add add a “custom build command” that actually grabs the name of the current source file, concatenates it with the “exe” extension and runs it inside the console.

Here’s another plugin, atom-runner: https://atom.io/packages/atom-runner

…and another one that also supports Rust: https://github.com/rgbkrk/atom-script

It really depends on the preferred toolset/scripts.

Regards,
Harris


#10

Thanks, I found that Build package has “Cargo - Run” target and I’m happy now :slight_smile:


#11

Is there any possible that we could debug rust code inside atom?


#12

Last I checked there was only a broken unusable way to debug c code. Maybe that has improved. You can basically use anything that uses gdb to debug rust code.


#13

Or you can just use x86_64-pc-windows-msvc and debug your programs with VS.


#14

visual studio doesn’t support cargo project. Looks like rust community doesn’t have a very function completed IDE solution.


#15

i think you can try for intellij-idea for rust it’s very good.