-
Notifications
You must be signed in to change notification settings - Fork 124
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
Override env var #647
Comments
env files do override existing vars. |
@John0x I created the following env file: CONNECTION_STRING=envfile and makefile: env_files = [
"./myenv.env"
]
[tasks.envtest]
command = "echo"
args = ["connection string: ${CONNECTION_STRING}"] ran the following command: CONNECTION_STRING=cli cargo make envtest
[cargo-make] INFO - cargo make 0.35.10
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: envtest
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: legacy-migration
[cargo-make] INFO - Execute Command: "echo" "connection string: envfile"
connection string: envfile
[cargo-make] INFO - Build Done in 0.21 seconds. which shows that env file overridden the current env value just fine. |
The environment variable isn't being properly propagated to the applications that are run. envfile:
makefile: env_files = ["./.env"]
[env]
CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true
[tasks.testo]
script_runner = "@shell"
script = '''
echo connection string: $CONNECTION_STRING
''' Being invoked with: Output:
|
'envfile' is what you defined in your env file so... isn't that the right value? |
Nope. Output should be "connection string: fromcli". I'll check if the workspace is the problem |
ok now i got you. no, thats not how it works at the moment. |
ah, okay. But isn't that the whole purpose of dotenv files? For example by .env file will contain the connection string that is being used for local development (the default), which will then be overwritten by the docker image for production. |
i only used it as an alternative of defining everything in the makefile.toml and not as dotenv alternative. PREFER_EXISTING = { value = "new", condition = { env_not_set = ["PREFER_EXISTING"] } }
OVERWRITE_EXISTING = { value = "new", condition = { env_set = ["OVERWRITE_EXISTING"] } } |
Okay, thanks for the clarification. But I think it would be better to not name them .env files then. Dotenv isn't really a standard but it's pretty established and therefore can cause a lot of confusion if it doesn't behave the way you'd expect .env files to behave. Or make them behave like typical dotenv files, which I think would be better |
at this point it would be a big backward break, so not sure i am for that change. also the original purpose was different and that's why i documented the order specifically. |
closing this one for now |
@sagiegurari is there a way to use dotenv with cargo-make? Just, for example, supports them. But I would really prefere to use cargo-make |
what do you mean exactly? to have a file that only contains 'defaults'? env_scripts = [
'''
#!@duckscript
text = readfile ./test.env
handle = map
map_load_properties ${handle} ${text}
for key in ${handle}
value = map_get ${handle} ${key}
value_not_exists = is_empty ${value}
if ${value_not_exists}
set_env ${key} ${value}
end
end
'''
] |
Thank you @sagiegurari They seem to work for commands that are directly in the Makefile but the program that is executed by the task isn't picking it up |
can you share an example? |
@John0x like i wrote in the example, its not tested. env_scripts = [
'''
#!@duckscript
text = readfile ./myenv.env
handle = map
map_load_properties ${handle} ${text}
keys = map_keys ${handle}
for key in ${keys}
value = map_get ${handle} ${key}
current_value = get_env ${key}
value_not_exists = is_empty ${current_value}
if ${value_not_exists}
set_env ${key} ${value}
end
end
'''
] |
apart of the script i gave you which should solve your issue, i could also add an attribute to enable that. env_files = [
{ path = "./load_if_not_defined.env", defaults_only = true },
{ path = "./load_all.env" }
] |
ok, decided to go for it. env_files = [
{ path = "./load_only_undefined.env", defaults_only = true },
{ path = "./load_all.env" }
] you can see the docs at: |
@John0x this is now officially released. |
@John0x can you share if this resolved your issue? |
Thank you very much :) |
Seems to be working fine |
thanks for checking |
Hey, I did expect env_files to behave like typical dotnet files, which surprisingly isn't the case. Is that intended?
My env_file has a variable "CONNECTION_STRING", which should default to the value set in the env_file but be overwritten, if it's defined as an environment variable.
It's not being overwritten tho, is it a bug?
Usecase:
Having default values defined in the env_file that will be overwritten in the docker machine by global environment variables
The text was updated successfully, but these errors were encountered: