Skip to content

fix: Bun integration env.get is undefined #5601

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

Merged
merged 1 commit into from
Dec 18, 2023
Merged

fix: Bun integration env.get is undefined #5601

merged 1 commit into from
Dec 18, 2023

Conversation

phyrog
Copy link
Contributor

@phyrog phyrog commented Dec 16, 2023

Overview

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests / types / typos

Description

Fixes this error when using req.env with Bun (e.g. for qwik-auth):

TypeError: req.env.get is not a function. (In 'req.env.get("AUTH_SECRET")', 'req.env.get' is undefined)

Checklist:

  • My code follows the developer guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • Added new tests to cover the fix / functionality

Sorry, something went wrong.

Copy link

netlify bot commented Dec 16, 2023

👷 Deploy request for qwik-insights pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6e494c5

@phyrog phyrog changed the title Fix bun middleware serverRequestEv.env fix: Bun serverRequestEv.env.get is undefined Dec 16, 2023
@gioboa
Copy link
Member

gioboa commented Dec 18, 2023

Thanks @phyrog 💪
This fix looks great. did you try this implementation locally?

@phyrog
Copy link
Contributor Author

phyrog commented Dec 18, 2023

Thanks @phyrog 💪
This fix looks great. did you try this implementation locally?

Yes I did.

@gioboa
Copy link
Member

gioboa commented Dec 18, 2023

I tested the current bun integration and it's working fine 🤔
Am I missing something?

Steps:

  • pnpm create qwik@latest
  • pnpm qwik add bun
  • bun run build
  • bun run serve
  • I can see the env variable in my page

This is my /src/routes/index.tsx file

import { component$ } from "@builder.io/qwik";
import { routeLoader$ } from "@builder.io/qwik-city";

export const useEnvLoader = routeLoader$(({ env }) => env.get('TEST'))

export default component$(() => {
  const envLoader = useEnvLoader();
  return (<>{envLoader.value}</>
  );
});

@phyrog
Copy link
Contributor Author

phyrog commented Dec 18, 2023

@gioboa Really? I'm just getting an empty page when following your instructions (works when running in dev, though). Edit: see below

Here's my system specs:

  System:
    OS: macOS 13.3.1
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 17.72 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.4.0 - /usr/local/bin/node
    npm: 10.2.4 - /usr/local/bin/npm
    pnpm: 8.12.1 - ~/Library/pnpm/pnpm
    bun: 1.0.18 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.109
    Safari: 16.4
  npmPackages:
    @builder.io/qwik: ^1.3.1 => 1.3.1
    @builder.io/qwik-city: ^1.3.1 => 1.3.1
    undici: ^5.26.0 => 5.28.2
    vite: ^5.0.6 => 5.0.10

edit: I just rebuilt again, now it shows up. However: when I change the variable after the build, it is not picked up, it will still display the old value; so I guess it gets baked into the code instead of reading at runtime?

Just my guess: When setting env: Bun.env, the env will be copied at build time (baked into the code), while with my patch, it will copy the { get(key) { Bun.env[key] }} object, which will correctly read the value at runtime.

Edit2: changing the var after building also does not work with my version 😆 But anyway, the original problem was a different one, where .get was not defined on env. I will investigate why it works with routeLoader$ but not with qwik-auth.

@phyrog
Copy link
Contributor Author

phyrog commented Dec 18, 2023

Ok so I debugged this a bit more:

The current version works for routeLoader$ (with the above mentioned issue that it actually does not read the variable from env at runtime), but neither for routeAction$, nor globalAction$ (which is used by qwik-auth).

Reproducable the same way you posted above, but with this code in src/routes/index.tsx:

import { component$ } from "@builder.io/qwik";
import { Form, routeAction$ } from "@builder.io/qwik-city";

export const useEnv = routeAction$((_data, req) => {
  console.log(req.env.get("TEST"))
});

export default component$(() => {
  const e = useEnv()

  return (
    <>
      <Form action={e}>
        <button type="submit">Env</button>
      </Form>
    </>
  );
});

Will result in an error, same with globalAction$.

@phyrog
Copy link
Contributor Author

phyrog commented Dec 18, 2023

@gioboa I also looked more into why it works for routeLoader$, turns out when you add bun, it automatically configures SSG, which explains why the env variable only changes after a rebuild. It is actually built into dist/q-data.json. When you disable SSG in adapters/bun/vite.config.ts, it will show the same behavior as routeAction$ and globalAction$, i.e. env.get is not a function. (In 'env.get("TEST")', 'env.get' is undefined).

@gioboa
Copy link
Member

gioboa commented Dec 18, 2023

Thanks @phyrog for your commitment. Your change is working fine.


I have this other error in the terminal but it's not related to your change.
Do you have the same?
Screenshot 2023-12-18 at 21 18 28

@phyrog
Copy link
Contributor Author

phyrog commented Dec 18, 2023

@gioboa No I don't think I have seen this one

@gioboa gioboa changed the title fix: Bun serverRequestEv.env.get is undefined fix: Bun integration env.get is undefined Dec 18, 2023
@gioboa gioboa merged commit 4d4b4d4 into QwikDev:main Dec 18, 2023
kodiakhq bot referenced this pull request in ascorbic/unpic-img Jan 7, 2024
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@builder.io/qwik](https://qwik.builder.io/) ([source](https://togithub.com/BuilderIO/qwik/tree/HEAD/packages/qwik)) | [`1.3.1` -> `1.3.2`](https://renovatebot.com/diffs/npm/@builder.io%2fqwik/1.3.1/1.3.2) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@builder.io%2fqwik/1.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@builder.io%2fqwik/1.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@builder.io%2fqwik/1.3.1/1.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@builder.io%2fqwik/1.3.1/1.3.2?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>BuilderIO/qwik (@&#8203;builder.io/qwik)</summary>

### [`v1.3.2`](https://togithub.com/BuilderIO/qwik/releases/tag/v1.3.2)

[Compare Source](https://togithub.com/BuilderIO/qwik/compare/v1.3.1...v1.3.2)

##### What's Changed

-   docs: update portal cookbook with solved problems by [@&#8203;thejackshelton](https://togithub.com/thejackshelton) in [https://github.com/BuilderIO/qwik/pull/5600](https://togithub.com/BuilderIO/qwik/pull/5600)
-   chore: Add notice about service worker usage by [@&#8203;nelsonprsousa](https://togithub.com/nelsonprsousa) in [https://github.com/BuilderIO/qwik/pull/5606](https://togithub.com/BuilderIO/qwik/pull/5606)
-   fix: Bun integration env.get is undefined by [@&#8203;phyrog](https://togithub.com/phyrog) in [https://github.com/BuilderIO/qwik/pull/5601](https://togithub.com/BuilderIO/qwik/pull/5601)
-   fix(insights): form errors by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5607](https://togithub.com/BuilderIO/qwik/pull/5607)
-   fix(qwik-insights): fix up create application form by [@&#8203;iamriajul](https://togithub.com/iamriajul) in [https://github.com/BuilderIO/qwik/pull/5573](https://togithub.com/BuilderIO/qwik/pull/5573)
-   docs(layout): Add "Slot" import by [@&#8203;HenriqueLimas](https://togithub.com/HenriqueLimas) in [https://github.com/BuilderIO/qwik/pull/5612](https://togithub.com/BuilderIO/qwik/pull/5612)
-   fix(tests): fix typos by [@&#8203;maiieul](https://togithub.com/maiieul) in [https://github.com/BuilderIO/qwik/pull/5616](https://togithub.com/BuilderIO/qwik/pull/5616)
-   fix: Correct qwik types by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5623](https://togithub.com/BuilderIO/qwik/pull/5623)
-   fix(docs): add missing cookbook section by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5626](https://togithub.com/BuilderIO/qwik/pull/5626)
-   fix(tailwind starter): switch from cjs to esm to support vite 5 by [@&#8203;thejackshelton](https://togithub.com/thejackshelton) in [https://github.com/BuilderIO/qwik/pull/5635](https://togithub.com/BuilderIO/qwik/pull/5635)
-   docs: fix up markdown rendering by [@&#8203;ValentinBossi](https://togithub.com/ValentinBossi) in [https://github.com/BuilderIO/qwik/pull/5532](https://togithub.com/BuilderIO/qwik/pull/5532)
-   refactor(insights): improve consistency  by [@&#8203;iamriajul](https://togithub.com/iamriajul) in [https://github.com/BuilderIO/qwik/pull/5609](https://togithub.com/BuilderIO/qwik/pull/5609)
-   docs: improve getting started steps by [@&#8203;shwosner](https://togithub.com/shwosner) in [https://github.com/BuilderIO/qwik/pull/5620](https://togithub.com/BuilderIO/qwik/pull/5620)
-   docs: improve eslint loader msg + add cookbook example by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5591](https://togithub.com/BuilderIO/qwik/pull/5591)
-   fix(ssr): slot subscribers by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5608](https://togithub.com/BuilderIO/qwik/pull/5608)
-   docs: create NavLink cookbook example by [@&#8203;Adbib](https://togithub.com/Adbib) in [https://github.com/BuilderIO/qwik/pull/5621](https://togithub.com/BuilderIO/qwik/pull/5621)
-   fix(tailwind): fix prettier config type by [@&#8203;shairez](https://togithub.com/shairez) in [https://github.com/BuilderIO/qwik/pull/5641](https://togithub.com/BuilderIO/qwik/pull/5641)
-   docs: add Node Docker deploy example by [@&#8203;nelsonprsousa](https://togithub.com/nelsonprsousa) in [https://github.com/BuilderIO/qwik/pull/5605](https://togithub.com/BuilderIO/qwik/pull/5605)
-   docs(cookbook): font optimization guide by [@&#8203;thejackshelton](https://togithub.com/thejackshelton) in [https://github.com/BuilderIO/qwik/pull/5645](https://togithub.com/BuilderIO/qwik/pull/5645)
-   fix: rendering ssr and csr for value="" by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5642](https://togithub.com/BuilderIO/qwik/pull/5642)
-   fix: remove unnecessary whitespace when handling classes by [@&#8203;jakovljevic-mladen](https://togithub.com/jakovljevic-mladen) in [https://github.com/BuilderIO/qwik/pull/5648](https://togithub.com/BuilderIO/qwik/pull/5648)
-   fix(jsx): dynamic DOM element props by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5650](https://togithub.com/BuilderIO/qwik/pull/5650)
-   refactor(jsx): tiny improvement by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5654](https://togithub.com/BuilderIO/qwik/pull/5654)
-   feat: add `skipConfirmation` to cli add command by [@&#8203;shairez](https://togithub.com/shairez) in [https://github.com/BuilderIO/qwik/pull/5655](https://togithub.com/BuilderIO/qwik/pull/5655)
-   chore: 1.3.2 by [@&#8203;shairez](https://togithub.com/shairez) in [https://github.com/BuilderIO/qwik/pull/5661](https://togithub.com/BuilderIO/qwik/pull/5661)

##### New Contributors

-   [@&#8203;phyrog](https://togithub.com/phyrog) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5601](https://togithub.com/BuilderIO/qwik/pull/5601)
-   [@&#8203;HenriqueLimas](https://togithub.com/HenriqueLimas) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5612](https://togithub.com/BuilderIO/qwik/pull/5612)
-   [@&#8203;ValentinBossi](https://togithub.com/ValentinBossi) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5532](https://togithub.com/BuilderIO/qwik/pull/5532)
-   [@&#8203;shwosner](https://togithub.com/shwosner) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5620](https://togithub.com/BuilderIO/qwik/pull/5620)

**Full Changelog**: QwikDev/qwik@v1.3.1...v1.3.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 9pm on sunday" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/ascorbic/unpic-img).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMjEuMCIsInVwZGF0ZWRJblZlciI6IjM3LjEyMS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
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.

None yet

3 participants