'cargo' is not recognized from GitLab runner

I'm setting up a local Windows runner for GitLab for a Rust project.

Building from the Windows command prompt works fine. But the CI script fails with

24 $ cargo build --release
25cargo : The term 'cargo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
26the spelling of the name, or if a path was included, verify that the path is correct and try again.
27At C:\Windows\TEMP\build_script983527380\script.ps1:221 char:1
28+ cargo build --release
29+ ~~~~~
30    + CategoryInfo          : ObjectNotFound: (cargo:String) [], CommandNotFoundException
31    + FullyQualifiedErrorId : CommandNotFoundException

I've tried manually setting the PATH variable. I don't understand why the gitlab runner can't see Cargo. Any tips appreciated!

Silly question, but has cargo been installed on the runner? Normally I'll use a Rust image so everything is already installed and set up.

1 Like

It's on a Windows VM (via Parallels) on which I have installed Rust and all other dependencies. I can call cargo from a command prompt on the VM and it finds it and runs it no problem. I've tested the build process manually and it works. I'm not sure what's different about gitlab runner calling the commands.

Is GitLab Runner executing from different user? Rust (and Cargo) is usually installed per-user, not system-wide.

1 Like

I think you may be on the right track. I added the full path to cargo.exe and now get this error:

25$ C:\Users\buildrunner\.cargo\bin\cargo.exe build --release
26error: no override and no default toolchain set

Well, if I understand correctly, the default toolchain is being set per-user, too. You can add rust-toolchain.toml file to the project, to set it explicitly as override.

1 Like

Solved it by installing GitLab Runner under the user account not system account. This post helped: Failed to install gitlab-runner service on Windows (#1265) · Issues · GitLab.org / gitlab-runner · GitLab

I launched "Run" with services.msc after initially installing GitLab Runner under the system account (the default). I then set it to the correct user account in its properties in services.msc. Doing it via the command line did not work for me.

2 Likes

I'm still getting this error at seemingly random times, and have to stop and start the GitLab runner, along with switching the user from local to system and back. That usually gets it working again. I do need a solid solution however. Perhaps using the full path and setting the toolchain for the project is the way to go.