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

Need some way to substitute envvars into env maps #24

Open
peakschris opened this issue Aug 24, 2024 · 5 comments
Open

Need some way to substitute envvars into env maps #24

peakschris opened this issue Aug 24, 2024 · 5 comments

Comments

@peakschris
Copy link

Firstly, after some years with bazel I have just come across this repo from a stackoverflow comment. Really cool stuff, it was just what we were missing. Thanks!

It would be really helpful if there was a way to substitute existing envvars (passed via bazel --test_env could be substituted into the itest env maps. This is now the only reason that we need to run batch scripts before running bazel test. For example:

env = {
"PATH": "$(PATH);/extra"
"TEST_DATA" = "$(ROOT)/subdir"
}

It would be further cool if there was a way to provide a canned substitution that pointed to the workspace root.

Thanks!

Chris

@dzbarsky
Copy link
Owner

dzbarsky commented Aug 24, 2024

Glad to hear this is solving some problems for you!

Could you provide some more details on your use case? It sounds like you are doing something non-hermetic and want an escape hatch?

For example, I don't understand the TEST_DATA example or the desire for a substitution that points to the workspace root? Why not use runfiles or location expansion? For example:

itest_service(
    name = "svc",
    env = {
        "TEST_DATA": "$(execpath :testdata)",
    },
)

filegroup(
    name = "testdata",
    srcs = [ .. ],
)

@peakschris
Copy link
Author

Exactly, we are migrating from a legacy build system to bazel, and in the interim whilst both build systems are active, we need to set some envvars to subdirectories of the workspace root for consistency with the existing code. Eventually we won't need this if we manage to achieve the transition! We have such a large amount of stuff to migrate, and we need to take it in steps.

@peakschris
Copy link
Author

One more detailed example that I've recently got working with rules_idata:

Test and Service are both started with a common TEST_DATA envvar. The test process reads a file from TEST_DATA and makes a rest call to Service including a relative pathname. Service reads from this relative pathname and returns a response. It's tricky with runfiles and bazel to have both Test and Service starting with a common TEST_DATA directory.

This is horrible, I'd want to see it refactored, but there are perhaps 2000 of these cases, and we need to first just make them work by running them with almost exactly the same environment as they were written to assume (e.g. TEST_DATA pointing to a subdirectory of workspace)

@dzbarsky
Copy link
Owner

I am guessing the 2000 refers to the # of tests, not the # of services? Perhaps it would work to write sh_binary wrappers around the service binaries that can set up the environment you want? And wrap the tests in a macro or inject env setup code into them in another way?

Another option would be to apply a patch to the env setup in rules_itest to customize things as you want, perhaps after the runfiles env setup

I'm hesitant to add additional complexity to the ruleset to support non-hermetic cases if it means better-behaved setups need to incur the cost as well. We can leave this issue open and see if other users also request a similar feature

@peakschris
Copy link
Author

I'm still stumbling around this issue, not just in non-hermetic use cases but also when integrating with existing test cases which require environment variables and trying to avoid wrapping every one to patch up envvar anomalities.

I created a draft PR, for brainstorming/discussion: #42

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

No branches or pull requests

2 participants