wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.
https://wasp-lang.dev
MIT License
13.32k stars 1.18k forks source link

ERR_MODULE_NOT_FOUND on build (reproducible with fresh install) #2216

Closed win3ry closed 1 week ago

win3ry commented 1 month ago

Describe the bug I have been working for a week on my project. Then, I added an api and a function. and I had a weird error not related to code or any edited file.

I tried to debug for an hour then I setup a fresh install of wasp and tried with just that portion of code to reproduce it. The bug remains the same.

To Reproduce Steps to reproduce the behavior:

  1. Fresh install of wasp
  2. Add this in wasp.config:
api factoryTest {
  fn: import { factoryTest } from "@src/factory/api",
  entities: [User],
  auth: false,
  httpRoute: (GET, "/test")
}

query verifyApiKey {
  fn: import { verifyApiKey } from "@src/factory/operations",
  auth: false,
  entities: [User]
}
  1. in /src/ create a /factory/ folder.
  2. in /src/factory/api.ts:
import { 
    verifyApiKey,
} from 'wasp/server/operations'

import {
    FactoryTest,
} from 'wasp/server/api'

export const factoryTest: FactoryTest = async (req, res, context) => {
    const apiVerified = await verifyApiKey({id: 'aa0379d4b56c858b2ca83ac2f123d111f01da85958228917893a6fce3d3884a2'});
}
  1. in /src/factory/operations.ts:
import {
    type VerifyApiKey,
} from 'wasp/server/operations';

import { 
    type User,
} from 'wasp/entities';

export const verifyApiKey: VerifyApiKey<Pick<User, 'id'>, boolean> = async ({ id }, context) => {
    return true;
}
  1. Launch the wasp server with wasp start.

Expected behavior

You should see in the console this error:

[ Server ] 🔍 Validating environment variables...
[ Server!] node:internal/modules/esm/resolve:265
[ Server!]     throw new ERR_MODULE_NOT_FOUND(
[ Server!]           ^
[ Server!] 
[ Server!] Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/aristophane/testbug/app/.wasp/out/sdk/wasp/dist/ext-src/file-upload/s3Utils' imported from /home/aristophane/testbug/app/.wasp/out/sdk/wasp/dist/ext-src/file-upload/operations.js
[ Server!]     at finalizeResolution (node:internal/modules/esm/resolve:265:11)
[ Server!]     at moduleResolve (node:internal/modules/esm/resolve:933:10)
[ Server!]     at defaultResolve (node:internal/modules/esm/resolve:1169:11)
[ Server!]     at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
[ Server!]     at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
[ Server!]     at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
[ Server!]     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
[ Server!]     at link (node:internal/modules/esm/module_job:86:36) {
[ Server!]   code: 'ERR_MODULE_NOT_FOUND',
[ Server!]   url: 'file:///home/aristophane/testbug/app/.wasp/out/sdk/wasp/dist/ext-src/file-upload/s3Utils'
[ Server!] }
[ Server!] 
[ Server!] Node.js v20.15.1
win3ry commented 1 month ago

I think I am getting close.

So, apparently, there is a conflict between the contexts that I am trying to pass.

What I tried:

  1. If I move the function verifyApiKey inside api.ts and run it from here: everything works.

  2. If I put the function verifyApiKey inside operations.ts without the VerifyApiKey type generated by wasp and import it in api.ts: everything works.

  3. But! I noticed that when the function is using the VerifyApiKey type, there is a conflict with both contexts.

I guess, you're not supposed to call a Query/Action inside an Api. The good implementation of my case is to load my related functions without declaring them as Query/Action.

So the REAL problem here is that if there is such a conflict, the build fails and displays an unrelated error.

Edit: Might be related to #1909.

sodic commented 3 weeks ago

Hey @win3ry, apologies for the late reply. I don't know how I'm only seeing this now.

Anyway, I tried it out and could not reproduce it. However, your steps to reproduce might be incomplete so I might have got something wrong:

I tried to work around these missing parts by removing auth: false and defining the User entity, but can't reproduce the behavior. Here's my project, does that one work for you?

repro.zip

Btw, this error seems very similar to the one we had in version 0.13.x, but has since been fixed in Wasp 0.14.0 (see this comment: https://github.com/wasp-lang/wasp/issues/2096#issuecomment-2263566708). Which version are you using?

Martinsos commented 1 week ago

Closing for now due to not being able to reproduce, but if needed we can reopen it again!