cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Running cargo commands in a subdirectory?

Open jduan opened this issue 5 years ago • 12 comments
trafficstars

Do the checklist before filing an issue:

  • [x] Is this related to the actions-rs Actions? If you think it's a problem related to Github Actions in general, use GitHub Community forum instead: https://github.community
  • [x] You've read the Contributing section about feature requests: https://github.com/actions-rs/.github/blob/master/CONTRIBUTING.md#feature-requests
  • [ ] Is this something you can debug and fix? Send a pull request! Bug fixes and documentation fixes are welcome.

Motivation

My understanding is this github action only supports running cargo commands from the root of the repo. Is it possible to run cargo commands from a subdirectory? The motivation is that I have a monorepo that has projects that use different languages. I'd like to run cargo commands only for the rust projects.

Additional context

I've tried to set "working-directory" at the top level but that didn't seem to work.

jduan avatar Jun 06 '20 19:06 jduan

This is also needed if you have a workspace project and want to tests with feature flags:

$ cargo test --features 'env'          
error: --features is not allowed in the root of a virtual workspace
note: while this was previously accepted, it didn't actually do anything

Gottox avatar Jul 05 '20 20:07 Gottox

Actually, while digging through the source, I suspect this is an actual bug in https://github.com/actions/toolkit. If you look at this comment, it states that if cwd is unset, it defaults to the current working directory, which is set by the working-directory option in the yaml:

https://github.com/actions/toolkit/blob/ea503a235e2ad9ef6aada2f72d62cbed17d31c76/packages/exec/src/interfaces.ts#L6-L7

Gottox avatar Jul 05 '20 20:07 Gottox

Perhaps this works?

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --manifest-path dir1/dir2/Cargo.toml

lisheeghan avatar Jul 31 '20 12:07 lisheeghan

This works around this issue. Nevertheless I think this is a bug.

Gottox avatar Aug 03 '20 09:08 Gottox

I could also use a way to run cargo commands from a subdirectory, especially when using cargo deb which has a bug (https://github.com/mmstick/cargo-deb/issues/151) where it doesn't work properly if run from the workspace root.

qwandor avatar Oct 27 '20 22:10 qwandor

I have a same requirement as to publish crates onto crates.io in a workspace through an action trigger.

huangjj27 avatar Feb 21 '21 23:02 huangjj27

So it seems we are waiting for #59 to be merged. As a workaround one could use marco-polos branch

unizippro avatar Apr 12 '21 13:04 unizippro

I also expected this to use

defaults:
  run:
    working-directory: my-rust-project

As the working directory for where cargo run is run

lukepighetti avatar Mar 16 '22 12:03 lukepighetti

Is there a way to do this? I cannot set the manifest path as that's not the directory I wish to run cargo from. 🙏

lpil avatar Oct 13 '22 13:10 lpil

@lpil yes, as others stated you could use the --manifest-path:

cargo build --manifest-path /path/to/your/Cargo.toml

rherrmannr avatar Dec 29 '22 16:12 rherrmannr

it'd still be neat to have this! --manifest-path doesn't work correctly with .cargo/config.toml and is usually broken when cross-compiling

ryankurte avatar Feb 08 '23 22:02 ryankurte

I ended up changing it to this

Screenshot 2023-04-12 at 13 25 10

goughjo02 avatar Apr 12 '23 12:04 goughjo02