can't run `system::command` that interacts with newly generated project
Bug Description
While attempting to get past another issue I ran into, template expansion failing #1387, I tried to modify template generated files, in a post Rhai script hook, with something like sed -i '' 's/some pattern/.../g' .github/workflows/*.y*, which failed to execute because the system::command apparently does not run in the context of the temporary working folder of cargo-generate.
make sure you
- [x] I'm on the latest rust version
rustup update - [x] I'm on the latest cargo-generate version
cargo install cargo-generate - [ ] In case you have an own
.gitconfig, provide all sections that contains one of:insteadOf,url
To Reproduce Steps to reproduce the behavior:
- define a
posthook script, that contains asystem::commandstatement that expects to access a known generated file, i.e.system::command("cat", ["my-known-file.txt"]); -
cargo generate --path my_template --allow-commands --name new-project - the template generation fails with an error saying command returned a none zero value
Expected behavior
I expect system::command to have access to generated files in a post hook script
Desktop (please complete the following information):
- OS: MacOS Sequoia
- Version: 15.3.1
- Build tools: gcc
- Shell: zsh
- rustc --version: rustc 1.84.1 (e71f9a9a9 2025-01-27)
- cargo generate --version: cargo generate-generate 0.22.1
Additional context
see, for more context template expansion failing #1387
Oh, that is a surprise and good catch. It should, of course, run within the generated project, the temporary folder of the final one, that depends probably on the hook type.
that depends probably on the hook type.
I defined my hook in post
Maybe you can try out the pre hooks, there is an example in the repo: https://github.com/cargo-generate/cargo-generate/blob/main/example-templates/hooks/cargo-generate.toml#L6
This actually should allow you to change the contents as you like.
also, since you're running on macOS, there are some issues with sed
I would suggest using gsed (the linux gnu sed), then the arguments as you have described them should work as expected.
Maybe you can try out the
prehooks, there is an example in the repo: https://github.com/cargo-generate/cargo-generate/blob/main/example-templates/hooks/cargo-generate.toml#L6This actually should allow you to change the contents as you like.
If that is the case the pre hook docs do not convey that. It even implies expansions are not yet run because you can add additional variables. Am I missing something?
Besides I tried it with the pre hook and the gsed suggestion and it does not work either.
I did a sanity check and ran another system::command requiring the files and that failed too unfortunately.
also, since you're running on macOS, there are some issues with sed I would suggest using
gsed(the linux gnu sed), then the arguments as you have described them should work as expected.
I tried this and unfortunately it the not fix the problem
I tried to reproduce the issue, but I cant. Please have a look at the code: https://github.com/cargo-generate/cargo-generate/pull/1392/files#diff-8d7079762a7191390b9114d55ccc9b5272eb29e370358108848fc696150150fd
There I have several rhai initiated sed calls, and they produce exactly what you are aiming for.
Only on the LICENSE file, but the essence is the same.
I suspect your issue is rooted to something else, but not to rhai and the directory.
@Nejat I wonder if you could have a look at the PR https://github.com/cargo-generate/cargo-generate/pull/1392 and see it has any impact on your issue described here? Maybe it solved it even?
@Nejat I wonder if you could have a look at the PR #1392 and see it has any impact on your issue described here? Maybe it solved it even?
~~This fixed this issue~~, as a bonus the system::command now returns stdout and stderr which fixes other issues I had other workarounds for, thanks
** UPDATE **
Unfortunately it did not work as I originally thought. I still can not access the newly generated files before the template expansions are run. The pre hook does not run system::command in the temp folder and the post hook is too late for what I need. I would like to run a rhai script before the expansions happen on the newly copied files in the working temp folder.