Skip to content

Set environment variables through the workspace - #1147

Merged
emillon merged 5 commits into
masterfrom
env-workspace
Oct 24, 2018
Merged

emillon merged 5 commits into
masterfrom
env-workspace

Conversation

@emillon

@emillon emillon commented Aug 17, 2018

Copy link
Copy Markdown
Collaborator

This is a first step towards #205.

The idea is that we add some extra environment variables to context.env in Context.create. These are sourced from (env).

Outstanding problems for now:

  • "env" is not a great name for the field (the full syntax is (env (_ (env (k v)))). environment_variables is precise but too long. Maybe env_vars could work?
  • at the moment, the env inheritance is done at the Super_context level, but we don't have a super-context when doing dune exec. Shall I move Env_node out of Super_context so that we can use that inheritance in Context as well?
  • in env_nodes, there is a workspace component and a context component. workspace corresponds to what is set in dune-workspace, but what is the context one?

Thanks!

@emillon
emillon requested review from a user and rgrinberg August 17, 2018 14:09
@rgrinberg

Copy link
Copy Markdown
Member

The feature itself is good, but I'm a bit concerned that we don't do dependency tracking at the env level. It seems to me that we should really be implementing dependency on env vars first, and then have ways of setting them.

@emillon

emillon commented Aug 17, 2018

Copy link
Copy Markdown
Collaborator Author

What do you mean by dependency tracking at the env level?

Comment thread src/context.ml Resolved
Comment thread src/context.ml Resolved
@rgrinberg

Copy link
Copy Markdown
Member

What do you mean by dependency tracking at the env level?

Just that changing the environment in the env stanza will trigger a rebuild.

@rgrinberg

Copy link
Copy Markdown
Member

"env" is not a great name for the field (the full syntax is (env (_ (env (k v)))). environment_variables is precise but too long. Maybe env_vars could work?

What about exec-env?

@emillon

emillon commented Aug 17, 2018

Copy link
Copy Markdown
Collaborator Author

What about exec-env?

As discussed in #205, this environment will also be used during the build (I haven't added a test for this yet).

@rgrinberg

Copy link
Copy Markdown
Member

Well, build commands are also executed. There's also env-vars, process-env perhaps.

@emillon

emillon commented Aug 20, 2018

Copy link
Copy Markdown
Collaborator Author

OK, I'll first deal with tracking env changes. Thanks!

@emillon

emillon commented Oct 2, 2018

Copy link
Copy Markdown
Collaborator Author

I just rebased now that env tracking works correctly. I have two questions:

  • what does the context component of env_nodes correspond to? (see initial question)
  • how can I add a new field to a record that is only available after a particular syntax version? It seems I'm missing a combinator (I like env-vars to be only parsed for syntax >= 1.4 and otherwise evaluate to the empty list).

Thanks!

@emillon emillon changed the title WIP: Set environment variables through the workspace Set environment variables through the workspace Oct 2, 2018
@ghost

ghost commented Oct 2, 2018

Copy link
Copy Markdown

what does the context component of env_nodes correspond to? (see initial question)

It's to represent dune-workspace files like this:

(context (default (env (flags ...))))

i.e. to allow the user to customize individual build contexts.

how can I add a new field to a record that is only available after a particular syntax version? It seems I'm missing a combinator (I like env-vars to be only parsed for syntax >= 1.4 and otherwise evaluate to the empty list).

Like this:

field "env_vars" ~default:[] (Syntax.since syntax (1, 4) >>> list ...)

@emillon

emillon commented Oct 22, 2018

Copy link
Copy Markdown
Collaborator Author

Thanks for the answers @diml. I added some tests for variables set from (context) as well. What should be the priority between the two sources?

@nojb

nojb commented Oct 22, 2018

Copy link
Copy Markdown
Collaborator

I'm coming late to the discussion, but will this feature allow setting variables such as PATH that will themselves be used during the build? (My use-case is adding the path to the C compiler to PATH so that it is used by the OCaml compiler).

@emillon

emillon commented Oct 22, 2018

Copy link
Copy Markdown
Collaborator Author

@nojb yes that's the plan. However for PATH there might be some particular handling to do since PATH is also interpreted by dune itself.

@emillon
emillon force-pushed the env-workspace branch 2 times, most recently from 4cb241a to 29d1257 Compare October 23, 2018 09:07
@ghost

ghost commented Oct 23, 2018

Copy link
Copy Markdown

@emillon the context specific variables are more specific, so they should have precedence over the workspace ones

Comment thread src/context.ml Resolved
@rgrinberg

Copy link
Copy Markdown
Member

Do we need a test case for setting the env-vars in an env stanza of a dune file? Or is that tested elsewhere?

@emillon

emillon commented Oct 23, 2018

Copy link
Copy Markdown
Collaborator Author

Right, that would be nice. Plus, we need some docs and that should be good.

@rgrinberg

Copy link
Copy Markdown
Member

Ah yes, the docs are definitely required for this to go in the release. Up to you if you want to do them in a separate PR or not.

@rgrinberg

Copy link
Copy Markdown
Member

@emillon could you give this a rebase? I'd like to have a look on top of the up to date code.

Comment thread src/dune_env.ml Outdated
{ flags : Ordered_set_lang.Unexpanded.t
; ocamlc_flags : Ordered_set_lang.Unexpanded.t
; ocamlopt_flags : Ordered_set_lang.Unexpanded.t
; env_vars : (string * string) list

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better if it was just Env.t?

@emillon

emillon commented Oct 24, 2018

Copy link
Copy Markdown
Collaborator Author

@rgrinberg I just rebased. I'm changing to use Env.t and add other tests (such as what happens when there are duplicate keys).

@rgrinberg rgrinberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @emillon. This looks much better. This is ready to merge once tests/DCO issues are fixed.

@emillon

emillon commented Oct 24, 2018

Copy link
Copy Markdown
Collaborator Author

Setting env vars from dune files is trickier since this relies on the supercontext for the dir -> env_node mapping, so I'll leave that part out of the PR.

Signed-off-by: Etienne Millon <[email protected]>
Signed-off-by: Etienne Millon <[email protected]>
Closes #205

Signed-off-by: Etienne Millon <[email protected]>
@emillon
emillon merged commit ba90f2a into master Oct 24, 2018
@emillon
emillon deleted the env-workspace branch October 24, 2018 17:52
shonfeder pushed a commit to shonfeder/dune that referenced this pull request Dec 31, 2018
* Set environment variables through the workspace

Closes ocaml#205

Signed-off-by: Etienne Millon <[email protected]>
punchagan added a commit to punchagan/dune that referenced this pull request Sep 1, 2026
env vars defined in a (context ...) stanza are documented to have higher
precedence than env vars defined in the root env stanza of a workspace.
The prose in the cram test also indicates this, but the test asserted
incorrect precedence. This seems to have been indicated in a review
comment [here] and marked done, but may have been undone by an incorrect
rebase or something.

Only consumers of Super_context.context_env like `dune exec` were
affected by this. Rules were not affected since they resolved their
environment separately through `Env_node.external_env` which is not
affected by this bug.

[here]: ocaml#1147 (comment)
punchagan added a commit to punchagan/dune that referenced this pull request Sep 1, 2026
env vars defined in a (context ...) stanza are documented to have higher
precedence than env vars defined in the root env stanza of a workspace.
The prose in the cram test also indicates this, but the test asserted
incorrect precedence. This seems to have been indicated in a review
comment [here] and marked done, but may have been undone by an incorrect
rebase or something.

Only consumers of Super_context.context_env like `dune exec` were
affected by this. Rules were not affected since they resolved their
environment separately through `Env_node.external_env` which is not
affected by this bug.

[here]: ocaml#1147 (comment)

Signed-off-by: Puneeth Chaganti <[email protected]>
Alizter added a commit that referenced this pull request Sep 2, 2026
<!-- Thank you for contributing to dune!

 For general guidelines on contributing to dune, see
https://github.com/ocaml/dune/blob/main/CONTRIBUTING.md#developing-dune
-->

## Description
<!-- Briefly describe your changes -->
env vars defined in a (context ...) stanza are documented to have higher
precedence than env vars defined in the root env stanza of a workspace.
The prose in the cram test also indicates this, but the test asserted
incorrect precedence. This seems to have been indicated in a review
comment [here] and marked done, but may have been undone by an incorrect
rebase or something.

Only consumers of Super_context.context_env like `dune exec` were
affected by this. Rules were not affected since they resolved their
environment separately through `Env_node.external_env` which is not
affected by this bug.

[here]: #1147 (comment)


## Related Issue and Motivation
<!-- Why is this change required? What problem does it solve? -->
<!-- If it closes an open issue, link to the issue here. -->
<!-- Non-trivial contributions are expected to be preceded by an issue,
as
     per CONTRIBUTING.md. -->

This issue was discovered while working on #15381 

## Checklist

- [ ] Tests added, if applicable.
- [x] [Change log entry
added](../CONTRIBUTING.md#updating-the-changelog) for any user-facing
changes.
- [ ] Documentation added for any user-facing changes.
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

Successfully merging this pull request may close these issues.

3 participants