Disable warnings from external (non-rust) file/folder in Rust - rust

Using an external dependency (tree-sitter-java) as a git submodule in my project.
When I run my program I always see this output:
warning: In file included from src/domain/usecase/java/parser/tree-sitter-java/src/parser.c:1:
warning: src/domain/usecase/java/parser/tree-sitter-java/src/parser.c: In function ‘ts_lex_keywords’:
warning: src/domain/usecase/java/parser/tree-sitter-java/src/tree_sitter/parser.h:136:8: warning: variable ‘eof’ set but not used [-Wunused-but-set-variable]
warning: 136 | bool eof = false; \
warning: | ^~~
warning: src/domain/usecase/java/parser/tree-sitter-java/src/parser.c:7164:3: note: in expansion of macro ‘START_LEXER’
warning: 7164 | START_LEXER();
warning: | ^~~~~~~~~~~
I do not want to see external dependencies warnings (from folder src/domain/usecase/java/parser/tree-sitter-java/) when I run my code.
I tried with things like
#[allow(dead_code)]
#[allow(unused_variables)]
but it ends up removing just the warnings in my code.
Is there any way to specify that a I do not want warnings from a specific folder/non-rust-file?
Ps: I solved the issue using an external rust wrapper dependency over tree-sitter-java, but I still want to know if it is possible to suppress warnings from external non-rust-code.

Related

Is there a way to fix "maybe a missing crate [crate_name]"?

My Rust crate wont load when compiling/running my file
I have reinstalled Rust several times, continuously restarting and checking the Path variables (as well as the current version installed)
I have restarted my system various times, and I am also experiencing this issue on my desktop PC at home; I am currently running Windows 11 as of the time of this post
I have added several other "dependencies" to my Cargo.toml file to no avail, they still refuse to work and I receive the same errors each time I try to utilise it
I have seen that some features of the Rust crate importation system have been deprecated; such as the "extern crate {crate_name}", I still receive these messages from time to time even though I am using the most up to date "rustup" version
I have consulted the Rust discord server and the official Rust language forums with the same issue
I have installed "git" on my system as that appeared to be an issue when adding my desired crates to the Cargo.toml
I expected to be able to use these crates without any hassle after solving my issue on importing these crates within the toml file, but I am still with nothing to show for that expectation.
Below is the code I have fully tested and ran
I have researched the documentation for importing crates with dependencies in Rust,
my directory looks like this: file directory
I have added the "ncurses" to my MinGW package manager, as requested by the "cursive" page on "crates.io"
I also gain the error from the "rust-analyser" extension in VSC
analyser error message
I have the current version of Rust installed,
rustup -V
rustup 1.25.1 (bb60b1e89 2022-07-12)
My File Directory looks like this:
-->Application
---->src
------>main.rs
---->target
---->.gitignore
---->Cargo.lock
---->Cargo.toml
My issue occurs when I try the command "cargo build" within the VSC terminal- which to my knowledge as I am in the bin file, it should run just fine.
I am thrown this error,
c1: fatal error C1083: Cannot open source file: 'C:\Users\dean7\Desktop\main.rs\crates\cratess\target\debug\build\ncurses-
4eee2f93b40e2da2\out\chtype_size.c': Permission denied
--- stderr
cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
thread 'main' panicked at 'assertion failed: command.status().expect(\"compilation failed\").success()', C:\Users\{%USERPROFILE%}\.cargo\registry\src\github.com-1ecc6299db9ec823\ncurses-5.101.0\build.rs:105:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
My "Cargo.toml" file looks like this,
[package]
name = "cratess"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cursive = "0.20.0"
My "main.rs" file looks like this,
use cursive::{Cursive, CursiveExt};
fn main() {
let mut app = Cursive::new();
app.run();
println!("Hello, world!");
}
My error when I run the command, run my code is this,
error[E0432]: unresolved import `cursive`
--> tempCodeRunnerFile.rs:1:5
|
1 | use cursive::{Cursive, CursiveExt};
| ^^^^^^^ maybe a missing crate `cursive`?
|
= help: consider adding `extern crate cursive` to use the `cursive` crate
error: aborting due to previous error
For more information about this error, try `rustc --explain E0432`.
An additional warning is given when I run "cargo build" in my terminal (NOT IN CMD),
warning: build failed, waiting for other jobs to finish...

Error in file /usr/include/ntirpc/rpc/xdr.h when compiling a program with rpc

I have to compile a c and c++ library with rpc for accessing meeasurement devices, that uses the standrd VXI11, see vxi11-library.
OS: opensuse tumbleweed
Compiler: gcc 12.1.0
After adding the include path -I/usr/include/ntirpc the program at least find the include path (rpc/rpc.h, obviously has been shifted around).
I had to change the name of some functions, but one error remained:
g++ -g -I/usr/include/ntirpc -c vxi11_cmd.cc -o vxi11_cmd.o
In file included from /usr/include/ntirpc/rpc/rpc.h:47,
from vxi11_user.h:31,
from vxi11_cmd.cc:25:
/usr/include/ntirpc/rpc/xdr.h:136:18: error: declaration of ‘vio_type xdr_vio::vio_type’ changes meaning of ‘vio_type’ [-fpermissive]
136 | vio_type vio_type; /* type of buffer */
/usr/include/ntirpc/rpc/xdr.h:126:3: note: ‘vio_type’ declared here as ‘typedef enum vio_type vio_type’
126 | } vio_type;
Anybode can help me, fixing thies error?
Thanks,
Johannes

Error adding Custom RPCs for custom pallet Substrate

I've been working with parity's contracts node (latest version) and the substrate template node (tag polkadot-v0.9.18), both present the same issue when compiling.
I have a very simple pallet that stores certain items. The main structure is the following:
#[pallet::storage]
#[pallet::getter(fn items)]
/// 'Mapping' Item ID -> Item Data
pub(crate) type Items<T: Config> = StorageMap<_, Twox64Concat, T::Hash, Item<T>>;
I was trying to add a simple RPC method following this guides https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-call-a-runtime-api and https://core.tetcoin.org/recipes/runtime-api.html
I also checked some projects that already have custom RPC calls implementations, like de subsocial node and I have pretty much the same structure and dependencies.
My rpc method does nothing but return a number 2 just to make sure it works, but it doesn't. This is what the pallets directory looks like:
pallets directory
When I try to compile, the following error shows
error: the wasm32-unknown-unknown target is not supported by default, you may need to
enable the "js" feature. For more information see:
https://docs.rs/getrandom/#webassembly-support
I don't even use that module, but I've read that it is used somewhere as an indirect dependency.
I'm compiling my project with the following command
cargo build --release
Checking the documentation regarding the 'getrandom' crate issue, I added the following dependency in the Cargo.toml (I tried adding it in every Cargo.toml within the project, individually, by pairs, ...)
getrandom = { version = "0.2", features = ["js"] }
Then another error shows up:
error: failed to run custom build command for secp256k1-sys v0.4.1
Which again, doesn't make any sense to me.
The project itself has nothing but the node template base and a new pallet that implements a create and transfer function. Without the RPC implementation, it works perfectly using the Polkadot App, but as soon as I include the custom rpc, it just doesn't compile.
This is my rust configuration (rustup show)
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-2021-11-04-x86_64-apple-darwin
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)
I haven't found anyone who is dealing with this kind of issue, and I don't know where the problem might be.
This is the first issue logs:
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:229:9
|
229 | / compile_error!("the wasm32-unknown-unknown target is not supported by \
230 | | default, you may need to enable the \"js\" feature. \
231 | | For more information see: \
232 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:256:5
|
256 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed
Current status (to reproduce error): https://github.com/andresvsm/substrate-pallet-rpc/tree/items-branch
Sometimes, you can get this error from a deep dependency of another dependency, e.g. when you really build for a wasm32-unknown-unknown target, and getrandom is linked but even not used. It can be fixed (worked around) with the following trick:
In Cargo.toml, add this line:
[dependencies]
getrandom = {version = "0.2", default-features = false, features = ["custom"]}
It tells the compiler to use a dummy implementation inside of getrandom.
Fixed for me when I added "default features = 'false'" into my Cargo.toml under the dependency in question.

Link to external object using the right (target-specific) compiler

I have a Rust crate that is targeting AVR. If I do a cargo build, it fails at linking, which is expected because I will need to add an extra object (compiled from C) to it. However, the error message shows that Cargo has correctly picked up from the target definition that it should use avr-gcc for linking:
"avr-gcc" "-mmcu=atmega328p" ....
= note: chirp8-avr/target/avr-unknown-gnu-atmega328/release/deps/chirp8_avr-e38dd93db57fe6d7.chirp8_avr.dlqaoark-cgu.0.rcgu.o: In function `main':
chirp8_avr.dlqaoark-cgu.0:(.text.main+0x18a): undefined reference to `font_rom_size'
chirp8_avr.dlqaoark-cgu.0:(.text.main+0x19e): undefined reference to `read_font_rom'
chirp8_avr.dlqaoark-cgu.0:(.text.main+0x1b4): undefined reference to `prog_rom_size'
chirp8_avr.dlqaoark-cgu.0:(.text.main+0x1c6): undefined reference to `read_prog_rom'
collect2: error: ld returned 1 exit status
So the next logical step is to add my external C source to the crate. From what I understand, cc is exactly the crate for that. So let's try that: I add cc to my [build-dependencies], and write the following build.rs:
fn main() {
cc::Build::new()
// .flag("-mmcu=atmega328p").pic(false)
.file("src/rom.c")
.compile("rom");
}
If I now try building again, this fails because Cargo suddenly seems to have forgotten to use avr-gcc, and is instead trying to use the system's default C compiler:
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-Wall" "-Wextra" "-o" "chirp8-avr/target/avr-unknown-gnu-atmega328/release/build/chirp8-avr-fd0f12cc4390c66b/out/src/rom.o" "-c" "src/rom.c"
cargo:warning=src/rom.c:1:10: fatal error: avr/pgmspace.h: No such file or directory
cargo:warning= 1 | #include <avr/pgmspace.h>
cargo:warning= | ^~~~~~~~~~~~~~~~
cargo:warning=compilation terminated.
exit code: 1
What am I doing wrong? Why is the target platform not picked up correctly by Cargo?
As documented in the cc crate's Readme:
This crate calls out to the most relevant compiler for a platform, for example using cl on MSVC.
Inspecting the source reveals that the cc crate does this with some hard-coded logic for certain statically known host/target combinations. Unfortunately, avr is not a target for which it knows to use avr-gcc.
The Readme also states:
External configuration via environment variables
To control the programs and flags used for building, the builder can
set a number of different environment variables.
[ deletia ]
CC - the actual C
compiler used. Note that this is used as an exact executable name, so
(for example) no extra flags can be passed inside this variable, and
the builder must ensure that there aren't any trailing spaces. This
compiler must understand the -c flag. For certain TARGETs, it also is
assumed to know about other flags (most common is -fPIC).
[ deletia ]
Each of these variables can also be supplied with certain prefixes and
suffixes, in the following prioritized order:
<var>_<target> - for example, CC_x86_64-unknown-linux-gnu
<var>_<target_with_underscores> - for example,
CC_x86_64_unknown_linux_gnu
<build-kind>_<var> - for example, HOST_CC
or TARGET_CFLAGS
<var> - a plain CC, AR as above.
If none of these
variables exist, cc-rs uses built-in defaults
Therefore, I suggest adding to your build.rs:
std::env::set_var("CC_avr-unknown-gnu-atmega328", "avr-gcc");

How do I allow deprecated features?

I have a Rust project that depends on string_cache, which requires nightly. However, the latest version of nightly is refusing to compile due to deprecated features:
$ cargo build
Compiling string_cache v0.1.0 (https://github.com/servo/string-cache#45f19068)
/home/wilfred/.multirust/toolchains/nightly/cargo/git/checkouts/string-cache-efa5c30b1d5a962c/master/src/atom/mod.rs:65:21: 65:37 error: use of deprecated item: use `String::from` instead, #[deny(deprecated)] on by default
/home/wilfred/.multirust/toolchains/nightly/cargo/git/checkouts/string-cache-efa5c30b1d5a962c/master/src/atom/mod.rs:65 string: String::from_str(string_to_add),
^~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `string_cache`.
To learn more, run the command again with --verbose.
How do I compile string_cache? I've tried adding
#![allow(deprecated)]
to my main.rs, but that doesn't change the behaviour.
The compiler doesn't impose any restrictions on the usage of deprecated methods by default:
fn main() {
(32.0f64).is_positive();
}
Compiles, but has the warning:
warning: use of deprecated item: renamed to is_sign_positive, #[warn(deprecated)] on by default
Your error message helps point to the culprit:
#[deny(deprecated)] on by default
You'll have to figure out where the deny is specified.

Resources