Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env override and makefile extending #523

Closed
x37v opened this issue Mar 6, 2021 · 9 comments
Closed

env override and makefile extending #523

x37v opened this issue Mar 6, 2021 · 9 comments
Assignees
Labels
Milestone

Comments

@x37v
Copy link

x37v commented Mar 6, 2021

I'm using cargo make 0.32.13

I have 2 Makefile.toml files

The first one defines

[env]
PACKAGE_NAME = { value = "foo", condition = { env_not_set = ["PACKAGE_NAME"] } }
PACKAGE_INSTALL_DIR = "${PACKAGE_NAME}/externals/"

[tasks.echo]
script_runner = "@shell"
script = [
'''
echo PACKAGE_INSTALL_DIR: ${PACKAGE_INSTALL_DIR}
'''
]

And the 2nd one extends the first

extend = "../path/to/Makefile.toml"

[env]
PACKAGE_NAME = "bar"

when I run cargo make echoin the directory with the extended makefile, I see:
PACKAGE_INSTALL_DIR: ${PACKAGE_NAME}/externals/

if I remove the [env] section from extended makefile I see:
PACKAGE_INSTALL_DIR: foo/externals/

If i keep it removed and run PACKAGE_NAME=bar cargo make echo i see:
PACKAGE_INSTALL_DIR: bar/externals/

So, maybe this is just a bug or am I messing something up? Is there a better way to do this? Basically, I have a variety of options in the base makefile that I'd like to be able to customize, names, paths, etc.

@sagiegurari
Copy link
Owner

feels like a bug. i will check it out.
thanks for notifying me :)

@sagiegurari
Copy link
Owner

@x37v this is now fixed in the 0.32.14 dev branch
thanks a lot for reporting. please tell me if it solves your issues.

@sagiegurari sagiegurari added this to the 0.32.14 milestone Mar 6, 2021
@x37v
Copy link
Author

x37v commented Mar 6, 2021

@x37v this is now fixed in the 0.32.14 dev branch
thanks a lot for reporting. please tell me if it solves your issues.

@sagiegurari that did it! thanks so much!

@x37v x37v closed this as completed Mar 6, 2021
@cdmistman
Copy link

cdmistman commented Aug 6, 2021

Not sure if this is a regression or a different bug, but I'm experiencing a bug that seems similar when running cargo-make 0.35.0:

[env]
TARGET = "hello"
CHK = "${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}"
TARGET_FILE = "${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}/${TARGET}.json"

[tasks.print-env]
script = [
  # prints "TARGET=hello"
  "echo TARGET=${TARGET}",
  # prints "TARGET_FILE=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}/hello.json"
  "echo TARGET_FILE=${TARGET_FILE}",
  # prints "CHK=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}"
  "echo CHK=${CHK}",
  # prints "CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY=/this/path/is/correct"
  "echo CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}",
]

@sagiegurari
Copy link
Owner

@cdmistman try the following:

[tasks.print-env]
script = [
  # prints "TARGET=hello"
  "echo TARGET=${TARGET}",
  # prints "TARGET_FILE=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}/hello.json"
  "echo TARGET_FILE=${TARGET_FILE}",
  # prints "CHK=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}"
  "echo CHK=${CHK}",
  # prints "CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY=/this/path/is/correct"
  "echo CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY=${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}",
]
[tasks.print-env.env]
TARGET = "hello"
CHK = "${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}"
TARGET_FILE = "${CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY}/${TARGET}.json"

The issue is that you are setting up global env vars based on other env vars that are defined a bit later.
for example CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY cannot be set globally by cargo-make as it is per task. you can have multiple tasks from different files and when you are in a context of a specific task only than its set.
so for task specific env vars, you must use by setting yours also in the contxt of a task.

@cdmistman
Copy link

Gotcha, can the docs be fixed in that case? Under https://github.com/sagiegurari/cargo-make#usage-env-vars-loading-order, it's suggested that env vars such as CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY are loaded before the [env] table (2nd bullet point is internal environment variables (see Global section), 5th bullet point is the global [env] table)

@cdmistman
Copy link

Although I would have a preference for some of these variables to be set before visiting the [env] table to reduce the number of times I have to repeat defining that variable. In the above use-case I listed, I need to use TARGET_FILE for multiple rules, and using a variable like CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY in the [env] table for the directory of the containing makefile would make pointing to the example .json file consistent across the workspace

@sagiegurari
Copy link
Owner

you could create a task to define the env vars you need and have it as a dependency.
the vars set would last the entire process so you can access them in other tasks.

as for the docs, I'll clarify it more. thanks for the feedback.

@cdmistman
Copy link

ah yes that makes sense! thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants