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

Config: Expected VALUE to exist in the process context #3427

Closed
boar-is opened this issue Aug 8, 2024 · 3 comments
Closed

Config: Expected VALUE to exist in the process context #3427

boar-is opened this issue Aug 8, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@boar-is
Copy link

boar-is commented Aug 8, 2024

What version of Effect is running?

3.6.1

What steps can reproduce the bug?

Open the reproduction

What is the expected behavior?

I want the value from the config to be resolved

What do you see instead?

(FiberFailure) Error: (Missing data at NEXT_PUBLIC_CONFIG_VALUE: "Expected NEXT_PUBLIC_CONFIG_VALUE to exist in the process context")

Additional information

Initially, I received this error implementing a Convex Function. However, process.env also worked fine there. So I decided to create a reproduction. Unfortunately, it doesn"t work too.

@boar-is boar-is added the bug Something isn't working label Aug 8, 2024
@KhraksMamtsov
Copy link
Contributor

KhraksMamtsov commented Aug 9, 2024

It"s not a bug of Effect

You can"t access environment variables in the browser dynamically
They are inlined by the Next.js only when accessed directly
Literally like this process.env.NEXT_PUBLIC_MY_VAR
https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser

However, you can create a ConfigProvider from the inlined values and provide it

"use client";

import { useEffect } from "react";
import { Config, Effect, ConfigProvider } from "effect";

const NextConfigProvider = ConfigProvider.fromMap(
  new Map([
    [
      //
      "NEXT_PUBLIC_CONFIG_VALUE",
      process.env.NEXT_PUBLIC_CONFIG_VALUE!, // <-- will be inlined by Next
    ],
  ])
);

export default function Home() {
  useEffect(() => {
    console.log("process.env", process.env.NEXT_PUBLIC_CONFIG_VALUE);
    const result = Effect.runSync(
      Config.string("NEXT_PUBLIC_CONFIG_VALUE").pipe(
        Effect.withConfigProvider(NextConfigProvider)
      )
    );
    console.log("effect", result);
  }, []);

  return <div>{process.env.NEXT_PUBLIC_CONFIG_VALUE}</div>;
}

Or you can use Layer and provide it with Layer.setConfigProvider as shown here https://effect.website/docs/guides/configuration#fallback-operators

@boar-is
Copy link
Author

boar-is commented Aug 9, 2024

You"re right. Forgot about that. Sorry.

The only thing is that it doesn"t work in Convex Runtime, which is not a browser. The worst part about that is that it would be hard to create an easily accessible reproduction.

For now, I use the following workaround:

const api_key = yield* Config.redacted("API_KEY").pipe(
  Config.withDefault(
    Redacted.make(process.env["API_KEY"] ?? ""),
  ),
)

@mikearnaldi
Copy link
Member

For environments that require static access you"ll need a custom provider and to list explicitely the env variables, nothing we can do. Would be good to open this issue in convex to see if there is a prefix like next that would automatically inline variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants