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

Confusion around util.env and util.tryEnv in preflight vs inflight contexts #4601

Open
skyrpex opened this issue Oct 19, 2023 · 7 comments
Open
Labels
🐶 dogfood Discovered while dogfooding Winglang good first issue Good for newcomers 🎨 sdk SDK

Comments

@skyrpex
Copy link
Contributor

skyrpex commented Oct 19, 2023

The util.env and util.tryEnv utilities work differently in preflight and inflight contexts.

Given the following example:

MY_VAR=123 wing compile main.w

// main.w
bring cloud;
bring util;

log(util.env("MY_VAR")); // Logs 123
let MY_VAR = util.env("MY_VAR");

new cloud.Function(inflight () => {
  log(util.tryEnv("MY_VAR") ?? "<no value>"); // Logs <no value>
  log(MY_VAR); // Logs 123
});

The Cloud Function util.tryEnv is being translated into a lookup to process.env rather than use the host's environment variables. I find this behaviour confusing, maybe even unnecessary. Do we need to access environment variables in inflight functions? The only environment variables we can specify are when invoking the Wing CLI.

IMO, log(util.tryEnv("MY_VAR") ?? "<no value>"); should log 123 as well, with the actual inflight code being inlined as log("123" ?? "<no value>"); (it could even be simplified to log("123")).

@skyrpex skyrpex added 🎨 sdk SDK needs-discussion Further discussion is needed prior to impl labels Oct 19, 2023
@eladb
Copy link
Contributor

eladb commented Oct 19, 2023

Let's start by restricting util.env() to preflight.

It feels like the right direction and one can always assign the value to a preflight variable and capture it:

let myEnv = util.env("MYENV");

inflight () => {
  log(myEnv);
};

Thoughts? Too restrictive?

@skyrpex
Copy link
Contributor Author

skyrpex commented Oct 20, 2023

It makes sense to me 👍.

@eladb eladb added good first issue Good for newcomers and removed good first issue Good for newcomers labels Oct 20, 2023
@eladb
Copy link
Contributor

eladb commented Oct 20, 2023

@Chriscbr do we have a way to mark a utility API as "only preflight"?

@staycoolcall911 staycoolcall911 added the 🐶 dogfood Discovered while dogfooding Winglang label Oct 30, 2023
Copy link

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@boyney123
Copy link
Contributor

Hey!

Just coming to this from a winglib I'm trying to make for stripe (winglang/winglibs#279).

I literally just come across some of this confusion, trying RANDOM=true wing test and expecting the variable to be accessible in my inflight code (using util.env and util.tryEnv) but no luck, as ofc it's inflight code.

Having it preflight only makes sense, only after I got my head around a few things (thanks @Chriscbr!), but I do wonder people coming from other languages, if it's too restrictive or unintuitive? I expected it to be in both preflight and inflight, this extra layer of cognitive load thew me off abit 🤷‍♂️

@Chriscbr
Copy link
Contributor

Chriscbr commented Jul 8, 2024

Let's start by restricting util.env() to preflight.

It feels like the right direction and one can always assign the value to a preflight variable and capture it:

let myEnv = util.env("MYENV");

inflight () => {
  log(myEnv);
};

Thoughts? Too restrictive?

I think this solution makes sense to me, especially in lieu of the confusion David and other users have run into trying to access environment variables in inflight code. The current design where util.env is a phase-independent function that retrieves an environment variable in the phase this function was called seemed ok to me in the past, but I don't think it's helping people fall into the pit of success.

(Let's make sure util.tryEnv is also preflight-only as well)

@Chriscbr Chriscbr added good first issue Good for newcomers and removed needs-discussion Further discussion is needed prior to impl labels Jul 8, 2024
@eladb
Copy link
Contributor

eladb commented Jul 8, 2024

Let's do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐶 dogfood Discovered while dogfooding Winglang good first issue Good for newcomers 🎨 sdk SDK
Projects
Status: 🤝 Backlog - handoff to owners
Development

No branches or pull requests

5 participants