Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Req: Add support for Rust files without Cargo.toml or valid cargo workspaces #4477

Closed
deavid opened this issue May 16, 2020 · 13 comments

Comments

@deavid
Copy link

deavid commented May 16, 2020

Some people are trying the rustlings repo and the exercises are outside the src/ folder, but in a exercises/ folder. I have been using VSCode and rust-analyzer extension, and it does work under the src/ folder, but it does not on exercises/. There's another issue in github with users having this problem: lsp-mope issue

I understand that this is not straightforward because rust-analyzer cannot know which dependencies you may have for your code. But at least, a minimal amount of support would be nice.

Would it be possible to add an option (maybe disabled by default) that makes those work by assuming no dependencies? (i.e. a new blank, standard Cargo.toml and using the latest stable rust)

On the other hand, in some corporate environments, cargo is not desirable because the build systems are tightly integrated, so we could expect that some people have their own way of compiling Rust and their programs would not have a regular structure. It would be really interesting to have a way of allowing these companies to extend rust-analyzer to support their build environments.

As for myself, I had to switch back to RLS to do the rustlings exercises.

@flodiebold
Copy link
Member

cargo is not desirable because the build systems are tightly integrated, so we could expect that some people have their own way of compiling Rust and their programs would not have a regular structure. It would be really interesting to have a way of allowing these companies to extend rust-analyzer to support their build environments.

We already have an alternate way of configuring the project structure, rust-project.json. It's in fact used by some of these corporate environments (e.g. Fuchsia). I have actually been wondering if it would be possible to make rustlings emit rust-project.json files to make it work in RA, although we'd still probably need more dynamic workspace support to make it seamless.

@matklad
Copy link
Member

matklad commented Jul 12, 2020

We support non-cargo based projects: https://rust-analyzer.github.io/manual.html#non-cargo-based-projects

@jrmoulton
Copy link

Does anyone have an example of how to make this rust-project.json work with rustlings?? I have been trying to follow along with the link that @matklad posted but I have no idea how to follow the typescript that was posted and the linked example is specifically for vscode and is pretty bare.

Are there any working examples for making this work with rustlings?

@jackos
Copy link
Contributor

jackos commented Jan 9, 2022

@jrmoulton I got you: https://gist.github.com/jackos/7332fa8ab0b67a87f382fd566696f412

@jrmoulton
Copy link

Ahhh. That's awesome. Thanks @jackos that's exactly what I was looking for

@jackos
Copy link
Contributor

jackos commented Jan 13, 2022

No worries, for anyone else hitting this issue I made a crate to fix it: https://crates.io/crates/rustlings-fix

Just go into rustlings folder and do:

cargo install rustlings-fix
rustlings-fix

And it'll generate the file

maradude added a commit to maradude/rustlings that referenced this issue Mar 25, 2022
@icyJoseph
Copy link

@jrmoulton I got you: https://gist.github.com/jackos/7332fa8ab0b67a87f382fd566696f412

Is there anyway to make this dynamic? I have a src folder with nothing but <file-name>.rs files which are solutions to coding challenges. I don't think it is scalable to try always keep on adding these to rust-project.json, but I might have to.

@iMilnb
Copy link

iMilnb commented Nov 1, 2022

Is there anyway to make this dynamic? I have a src folder with nothing but <file-name>.rs files which are solutions to coding challenges. I don't think it is scalable to try always keep on adding these to rust-project.json, but I might have to.

Same boat, it took me way too long to understand that no, rust-analyzer doesn't support non-cargo projects, they just have an ugly workaround.
I came up with this script, but don't get me wrong, having to do this is ridiculous:

#!/bin/sh

for f in *.rs
do
	crates="${crates}${next}{\"root_module\": \"$f\",\"edition\": \"2021\",\"deps\": []}"
	next=","
done

sysroot_src="$(rustc --print sysroot)/lib/rustlib/src/rust/library"

echo "{\"sysroot_src\": \"$sysroot_src\", \"crates\": [$crates]}"|jq '' >rust-project.json

@jackos
Copy link
Contributor

jackos commented Nov 2, 2022

@iMilnb it's built into rustlings now you can do:

rustlings lsp

@iMilnb
Copy link

iMilnb commented Nov 2, 2022

@iMilnb it's built into rustlings now you can do:

rustlings lsp

And what about any other toy directory you'd like to have loose .rs into?

@drzhivago420
Copy link

Agree with iMilnb, idk how this issue is closed at all.

@matklad
Copy link
Member

matklad commented Dec 2, 2022

The issue is closed because rust-analzyer does in fact fully support projects without Cargo.toml.

What you are asking is different: making rust-analyzer work out-of-the-box without any config for more ad-hoc projects structures. This is a hard problem, as I don't think rust-analyzer can "just" work with a bunch of .rs files -- something has to tell it which files are root files, and which files are included via mod declarations from other files. That "something" is our rust-project.json.

But perhaps some kind of heuristic to infer a project structure would cover 80% here. If anyone wants to work on that, that probably should be a separate issue. #8955 would be the starting point for that work.

@dream-dasher
Copy link

dream-dasher commented Dec 31, 2024

For anyone else that ends up here while searching for solutions I want to flag that Rust Analyzer support for Cargo-Script should give good workaround.

Tracking issues:

Hopefully close!
As of writing, almost everything seems to work on the single file level except RustFmt & RustAnalyzer.
Clippy even works, but needs to be called before the -Zscript as in

cargo +nightly clippy --manifest-path ./filename.rs -Zscript

vs

cargo +nightly -Zscript clippy --manifest-path ./filename.rs

Been really great just using Cargo-Script natively for exploration, small problems, and ad-hoc scripting.
I haven't seen a practical workaround for Rust-Analyzer support. Hopefully soon! (If not I suppose a script to auto write the json above would be an option. If it rewrote it or swapped visible file fast enough that rust-analyzer could switch -- with some pain points if working with more than one file, e.g. copying, doing comparisons, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants