vscode-rust icon indicating copy to clipboard operation
vscode-rust copied to clipboard

Project with 2 executables and a library shows error referencing common code

Open pfmoore opened this issue 4 years ago • 1 comments

It's possible I have my project structure wrong here. I'm new to rust, and just starting on splitting my project up.

I have a project pylaunch with 3 files, src/lib.rs containing pub fn launch, and bin/pylaunch.rs and bin/pylaunchw.rs. The idea is to build two slightly different executables using the common code.

Both files in bin say use pylaunch::launch; and then call launch. The pylaunch.rs file looks fine. But in pylaunchw.rs, the name pylaunch in the use statement is highlighted as an error, saying "unresolved import pylaunch".

The project builds fine with cargo build, so I don't think there's a problem with my file structure. But the error highlight is a distraction. Is there a genuine bug in my code here that I'm not understanding, or is it an issue with the extension?

pfmoore avatar Jan 31 '21 14:01 pfmoore

Update: Changing the package name in Cargo.toml to pylaunchlib and changing the two use statements to refer to that name, makes everything look OK. So I think the problem is likely to be around the fact that I tried to re-use the name for the library and one of the executables. However, it's still true that my code as written built fine, so highlighting an error isn't correct...

I couldn't find anything in the rust book that clearly explained whether this project structure was acceptable or not, but to me, it's logical - my project is essentially an application called pylaunch, with a variant executable called pylaunchw ("w" for "Windows GUI version"). The common code isn't really intended as a reusable library, just a way of sharing the code between the two executables. I could have used a module, I guess, but I thought that would add complexity that I didn't need at the moment (at its current stage, my project is only a few lines of code). So making the main project be the library seems to be the wrong way round to me.

As I say, if I'm unaware of "best practice" for a project of this form, building multiple executables from a chunk of shared code, then I'd be happy to be given pointers to where I can find out more.

pfmoore avatar Jan 31 '21 15:01 pfmoore