How to install the command "cargo set-version"? - rust

I'm trying to run a script that requires the cargo subcommand cargo set-version --workspace --bump="${2:-}".
How do I install it?
Google results only returned cargo install set-cargo-version but that is actually a different crate, not the one I need here, as I found out after installing.

Searching for set-version on cargo.io I discovered that the crate cargo-edit installs the subcommand cargo set-version.
Installation works as follows:
cargo install cargo-edit
As #Shirshak55 mentioned: cargo-edit provides cargo add, cargo rm, cargo upgrade, cargo set-version

Related

Rust nightly not installed, please install it

I was able to build and run substrate-node-template from the instructions from the cloned repo, following the instructions at https://substrate.dev/docs/en/tutorials/create-your-first-substrate-chain/
However when I try to do the follow on tutorial of adding the nicks pallet I get the following error. <Rust nightly not installed, please install it!>.
This is right after I run the command -
WASM_BUILD_TOOLCHAIN=nightly-2020-10-05 cargo build --release
I tried running the command -
WASM_BUILD_TOOLCHAIN=nightly-2021-02-12 cargo build --release
I get the same error.
Here is what I currently have -
$ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: syncing channel updates for 'nightly-x86_64-unknown-linux-gnu'
info: checking for self-updates
stable-x86_64-unknown-linux-gnu unchanged - rustc 1.50.0 (cb75ad5db 2021-02-10)
nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.52.0-nightly (e9920ef77 2021-02-11)
info: cleaning up downloads & tmp directories
Previously, I had built the cloned repo and everything worked fine.
Any help resolving this is appreciated.
I had the same problem but i fixed it by updating rust and target "wasm" to nightly build
rustup update
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly
Running only cargo run --release -- --dev --tmp without WASM_BUILD_TOOLCHAIN=nightly-2020-10-05 did the trick for me!

npm/yarn module install with git: cloning repo into node_modules and installing does not work but yarn add does

I'm trying to make a contribution to an opensource project. In particular, react-native.
I have a test app which I've created with create-react-native-app test if I do the following:
cd test
rm -rf node_modules/react-native
yarn add https://github.com/<mygithub>/react-native # (my fork)
There's no issue when I start my app with yarn run ios
However, if I try to clone my fork into node_modules and install it manually with the following:
cd test
rm -rf node_modules/react-native
git clone https://github.com/<mygithub>/react-native
cd react-native
yarn install
I get all sorts of issues. Namely, in my case:
console.error: "React Native version mismatch.
JavaScript version: 0.0.0
Native version: 0.52.0
...
Which to me indicates that something in cloning and manually installing is done differently than adding the package with yarn install and a github url.
I want to be able to work out of a git repo to commit my work and if I use yarn install it is not an initialized git repo. For lack of a better solution, how can I get manually cloning and installing a node_module with yarn to yield the same result as adding it with yarn install?
Edit to add steps attempted for yarn-link:
git clone https://github.com/<mygithub>/react-native
cd react-native
yarn install
yarn link
cd ../test
yarn link "react-native"
This results in the same results as cloning my fork into test/node_modules.
Edit 2: I should add, according the the react-native docs, I should be able to install my fork by cloning it into my project's node_modules folder and installing. The only difference is that I'm using yarn.
https://facebook.github.io/react-native/docs/android-building-from-source.html#building-the-source

Can Cargo download and build dependencies without also building the application?

Is there a way to tell Cargo to install and build all my dependencies, but not attempt to build my application?
I thought cargo install would do that, but it actually goes all the way to building my app too. I want to get to a state where cargo build would find all dependencies ready to use, but without touching the /src directory.
What I'm really trying to accomplish:
I'm trying to build a Docker image for a Rust application, where I'd like to do the following steps:
Build time (docker build .):
import a docker image with rust tooling installed
add my Cargo.toml and Cargo.lock files
download and build all dependencies
add my source directory to the image
build my source code
Run time (docker run ...):
run the application
I've tried the following Dockerfile, but the indicated step builds my application as well (which of course fails since the source directory isn't there yet):
FROM jimmycuadra/rust
ADD Cargo.toml /source
ADD Cargo.lock /source
RUN cargo install # <-- failure here
ADD src /source/src
RUN cargo build
ENTRYPOINT cargo run
The reason I want to separate the install dependencies step from actually building my application, is that if I don't change the dependencies, I want Docker to be able use a cached image with all dependencies already installed and built. Thus, I can't ADD /src /source/src until after installing the dependecies, as that would invalidate the cached image when I change my own code.
There is no native support for building just the dependencies in Cargo, as far as I know. There is an open issue for it. I wouldn't be surprised if you could submit something to Cargo to accomplish it though, or perhaps create a third-party Cargo addon. I've wanted this functionality for cargo doc as well, when my own code is too broken to compile ;-)
However, the Rust playground that I maintain does accomplish your end goal. There's a base Docker container that installs Rustup and copies in a Cargo.toml with all of the crates available for the playground. The build steps create a blank project (with a dummy src/lib.rs), then calls cargo build and cargo build --release to compile the crates:
RUN cd / && \
cargo new playground
WORKDIR /playground
ADD Cargo.toml /playground/Cargo.toml
RUN cargo build
RUN cargo build --release
RUN rm src/*.rs
All of the downloaded crates are stored in the Docker image's $HOME/.cargo directory and all of the built crates are stored in the applications target/{debug,release} directories.
Later on, the real source files are copied into the container and cargo build / cargo run can be executed again, using the now-compiled crates.
If you were building an executable project, you'd want to copy in the Cargo.lock as well.
If you add a dummy main or lib file, you can use cargo build to just pull down the dependencies. I'm currently using this solution for my Docker based project:
COPY Cargo.toml .
RUN mkdir src \
&& echo "// dummy file" > src/lib.rs \
&& cargo build
I'm using --volumes, so I'm done at this point. The host volumes come in and blow away the dummy file, and cargo uses the cached dependencies when I go to build the source later. This solution will work just as well if you want to add a COPY (or ADD) later and use the cached dependencies though.
Based on a GitHub comment
FROM rust:1.37
WORKDIR /usr/src
# Create blank project
RUN USER=root cargo new PROJ
# We want dependencies cached, so copy those first.
COPY Cargo.toml /usr/src/PROJ/
COPY Cargo.lock /usr/src/PROJ/
WORKDIR /usr/src/PROJ
# This is a dummy build to get the dependencies cached.
RUN cargo build --release
# Now copy in the rest of the sources
COPY MyPROJECT/src /usr/src/PROJ/src/
# This is the actual build.
RUN cargo build --release \
&& mv target/release/appname /bin \
&& rm -rf /usr/src/PROJ
WORKDIR /
EXPOSE 8888
CMD ["/bin/appname"]
The cargo-chef tool is designed to solve this problem. Here's an example from the README on how you can use it in the Dockerfile:
FROM lukemathwalker/cargo-chef as planner
WORKDIR app
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM lukemathwalker/cargo-chef as cacher
WORKDIR app
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
FROM rust as builder
WORKDIR app
COPY . .
# Copy over the cached dependencies
COPY --from=cacher /app/target target
COPY --from=cacher $CARGO_HOME $CARGO_HOME
RUN cargo build --release --bin app
FROM rust as runtime
WORKDIR app
COPY --from=builder /app/target/release/app /usr/local/bin
ENTRYPOINT ["/usr/local/bin/app"]
I just wanted to post this here so others will see it going forward. There's an experimental tool for Docker I've just started using called cargo-wharf (https://github.com/denzp/cargo-wharf/tree/master/cargo-wharf-frontend). It's a Docker BuildKit frontend that caches built cargo dependencies for you. If you only change one of your source files, that's the only thing that gets rebuilt when you call docker build. You use it by annotating your Cargo.toml file, then directing Docker to your Cargo.toml instead of a Dockerfile. Go check it out, it's exactly what I wanted. (I am in no way affiliated with the project.)
It can be done via cargo init, cargo build, and cargo install. For example, for a project called foo, define the following Dockerfile:
FROM rust:slim-bullseye
# Build dependencies only.
RUN cargo init foo
COPY Cargo.toml foo/
RUN cargo build --release; \
rm -rf foo
# Install `foo`.
COPY . .
RUN echo "// force Cargo cache invalidation" >> foo/src/main.rs; \
cargo install --path foo
CMD ["foo"]
Here, cargo init creates the placeholder files that Cargo expects, cargo build builds the dependencies which were specified in the Cargo.toml, and cargo install creates the foo binary. For some reason, the Docker kept building the default project created by cargo init foo. This problem is resolved above by forcing an update for main.rs by appending // force Cargo cache invalidation.
To avoid slow builds due to large build contexts and large layers, make sure that unimportant folders such as target are ignored via .dockerignore. For example, define the following .dockerignore:
**/*.lock
LICENSE
README.md
target

Where does 'stack build PKG' install 'PKG'; how do I isolate the install of 'PKG'?

I'm confused about where stack build PKG places the packages it installs. I had thought that, for example
mkdir foo
cd foo
stack build PKG_IN_HACKAGE_NOT_IN_RESOLVER
would create foo/.stack-work and put the specified package there (as if to had been specified as an "extra dependency"); but I seen on .stack-work and the package is available globally on my system.
If what I want is an entirely isolated (temporary) Haskell configuration in which to install and use PKG, how do I do that?

How to install a dev branch into a Cabal Sandbox

I have a project in a Cabal sandbox. There is a package I would like to use but the one on Hackage isn't suitable. There is an alternative dev branch that should meet my needs that the author has on Github. I've previously installed dev branches without a sandbox by using:
$ runhaskell Setup.hs configure --user
$ runhaskell Setup.hs build
$ runhaskell Setup.hs install
Obviously that's not going to work if I only want this repo installed in the sandbox.
My directory structure is set up like this:
../MyProject
../MyProject/.cabal-sandbox
../MyProject/exec/Main.hs
../MyProject/src/MyProject.hs
../MyProject/MyProject.cabal
There's both an executable and a library. My build-depends has about 18 package dependencies, of which this is one. So my questions:
Once I've downloaded the dev repo, where should I extract it to?
Once extracted, how do I build/install into my sandbox?
Once that's all done, do I need to modify my .cabal file at all?
Reproducing the correct answer as community-wiki from the comments:
Try cabal sandbox add-source. See coldwa.st/e/blog/2013-08-20-Cabal-sandbox.html for an example.

Resources