blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Internal Blitz Error: process.env.BLITZ_APP_DIR is not set #302

Closed kenneho closed 2 years ago

kenneho commented 2 years ago

What is the problem?

Hi.

Steps to reproduce:

  1. Install 0.40.0-canary.8
  2. blitz new myapp
  3. cd myapp
  4. blitz generate all something. Throws error message Error: Internal Blitz Error: process.env.BLITZ_APP_DIR is not set

Error logs:

blitz generate all something output:

CREATE    app/pages/somethings/[somethingId].tsx
CREATE    app/pages/somethings/[somethingId]/edit.tsx
CREATE    app/pages/somethings/index.tsx
CREATE    app/pages/somethings/new.tsx
CREATE    app/somethings/components/SomethingForm.tsx
CREATE    app/somethings/queries/getSomething.ts
CREATE    app/somethings/queries/getSomethings.ts
CREATE    app/somethings/mutations/createSomething.ts
CREATE    app/somethings/mutations/deleteSomething.ts
CREATE    app/somethings/mutations/updateSomething.ts
✕ Error: Internal Blitz Error: process.env.BLITZ_APP_DIR is not set
roshan-sama commented 2 years ago

I think I have the fix for this, working on it in PR https://github.com/blitz-js/blitz/pull/2679

Wondering if we should add a separate quick fix for this: packages/generator/src/generators/page-generator.ts

async postWrite() {
    if (!process.env.BLITZ_APP_DIR) {
      process.env.BLITZ_APP_DIR = "."
    }
    const {loadConfigProduction} = await import("next/dist/server/config-shared")
    const {saveRouteManifest} = await import("next/dist/build/routes")
roshan-sama commented 2 years ago

I actually was wondering where the BLITZ_APP_DIR should be set.

1) Should this be done by the abstract generator's constructor instead of at the postWrite hook in that one page generator? I don't mind leaving it before the call to loadConfigProduction until we add a feature to all generators that requires the config from blitz.config

2) process.env.BLITZ_APP_DIR = "." works for me in the other PR, but should it be set to something like process.cwd() instead?

flybayer commented 2 years ago

@Roesh yes let's get a quick fix for this.

It should be set in https://github.com/blitz-js/blitz/blob/canary/packages/cli/src/commands/generate.ts, same as we are doing in https://github.com/blitz-js/blitz/blob/canary/packages/cli/src/commands/routes.ts#L32

roshan-sama commented 2 years ago

Ok, I've made the following change:

constructor(protected readonly options: T) {
    super()

    this.options = options
    this.store = createStore()
    this.fs = createEditor(this.store)
    this.enquirer = new Enquirer()
    this.useTs =
      typeof this.options.useTs === "undefined"
        ? fs.existsSync(path.resolve("tsconfig.json"))
        : this.options.useTs
    if (!this.options.destinationRoot) this.options.destinationRoot = process.cwd()
    process.env.BLITZ_APP_DIR = process.cwd()     <------------------------
  }

I initially set BLITZ_APP_DIR to this.options.destinationRoot because several generator commands seem to be ensuring that that directory was created, but after looking at various places in different files, I'm not able to ascertain the role of BLITZ_APP_DIR vs destinationRoot to determine which should be set here.

It will probably take me a couple hours of reviewing where/how those variables are used to figure that out. @flybayer based on the linked PR it looks like we want process.cwd(), but thought I'd double check with you and make sure its OK as well.

I'm also going to write an integration test for the generators, might only be able to get to that on Friday though

IRediTOTO commented 2 years ago

Hi, I am getting this error. I ran blitz cg well before, but now it not work, don't know why. current version I am using "blitz": "0.43.0", Generating route manifest run forever, nonstop I tried delelte node_modules

◣ Generating route manifest◢ Generating Prisma client(node:13552) UnhandledPromiseRejectionWarning: Error: Internal Blitz Error: process.env.BLITZ_APP_DIR is not set
    at Object.loadConfigAtRuntime (E:\Job\test-blitz\node_modules\next\dist\server\config-shared.js:30:15)
    at Object.newline (E:\Job\test-blitz\node_modules\next\dist\server\lib\logging.js:11:169)
    at dedupeBy (E:\Job\test-blitznode_modules\next\dist\build\routes.js:195:23)
    at generateManifest (E:\Job\test-blitz\node_modules\next\dist\build\routes.js:220:37)
    at saveRouteManifest (E:\Job\test-blitz\node_modules\next\dist\build\routes.js:117:47)
(Use `node --trace-warnings ...` to show where the warning was created)        
(node:13552) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13552) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✔ Generating Prisma client
◤ Generating route manifest
flybayer commented 2 years ago

@IRediTOTO try upgrading to blitz@latest