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

Preloads not being loaded during ace command execution #4858

Closed
qodeboy opened this issue Jan 18, 2025 · 8 comments
Closed

Preloads not being loaded during ace command execution #4858

qodeboy opened this issue Jan 18, 2025 · 8 comments
Assignees
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?

Comments

@qodeboy
Copy link

qodeboy commented Jan 18, 2025

Package version

@adonisjs/[email protected]

Describe the bug

Preloads and/or validator macros are not loaded in ace commands lifecycle.

Reproduction repo: https://codesandbox.io/p/github/qodeboy/adonis-ace-validator-bug/main

Steps:

  1. Open reproduction repo, disregard error in the main terminal, open new terminal.
  2. Try running node ace, you will get a TypeError: vine.string(...).regex(...).assume is not a function error.
  3. Go to #bin/console.ts and uncomment workaround (import start/validator)
  4. Repeat node ace - now executes without an error.

Implementation notes:

  1. start/validator - preload file which registers custom validator macro.
  2. app/validators/ping - custom shared validator which uses registered macro.
  3. commands/ping - command which is configured to boot application and uses shared validator.
  4. bin/console - is where current workaround is implemented.

Breakdown:

Sometimes we need to use a validator (will all extension/macros we configured) outside of HTTP context. Valid use cases is validation in ace commands or background/queue jobs. But the issue is that macros registered using preload is not being picked up when app is started in ace context.

This results in runtime errors when trying to use shared validator which use macros/extensions. Current workaround is to manually import validator preload inside bin/console file, but this feels dirty.

Solutions I have tried:

  1. Preload - not included during ace command bootstrap.
  2. Provider boot/register - too late (not sure why).

Questions:

  1. Is it a valid bug, or ace is designed to skip preloads? Is it documented somewhere?
  2. What is recommended place to register macros? Preload or provider?

Reproduction repo

https://codesandbox.io/p/github/qodeboy/adonis-ace-validator-bug/main

@thetutlage
Copy link
Member

Its because you have not started the app. The app must be started in order for all lifecycle actions to happen (including preloading files).

  static options: CommandOptions = {
    startApp: true,
  }

@thetutlage thetutlage self-assigned this Jan 21, 2025
@thetutlage thetutlage added the Type: Invalid Doesn't really belong here. Maybe use discussion threads? label Jan 21, 2025
@qodeboy
Copy link
Author

qodeboy commented Jan 23, 2025

You're right, I've forgotten to copy that to the reproduction repo, but my actual app had this. Configured reproduction console command to start the app, issue is still there.

@qodeboy
Copy link
Author

qodeboy commented Jan 23, 2025

@thetutlage can you reopen? I believe issue is valid.

@thetutlage
Copy link
Member

Just tried it and it works fine for me. I have the following log statement in the start/validator.ts file and it prints for me.

console.log('FILE LOADED')

Image

@qodeboy
Copy link
Author

qodeboy commented Jan 26, 2025

That's odd, because I have just tried in the online code sandbox and it still gives me an error:

Image

But once I uncomment fix in the the bin/console.ts, it starts working as expected:

Image

Have you tested in your local env? Online sandbox is at node 20, could it relate somehow? (p.s. my local env is at 22, and have same issue)

Can I provide any other info to help you reproduce?

@thetutlage
Copy link
Member

Yes, I tried it locally and it seems to work fine for me. I am also using Node v22. Can you try updating all the dependencies?

@qodeboy
Copy link
Author

qodeboy commented Jan 27, 2025

No luck, still same issue both locally and in online sandbox.

Updated to the latest versions:

[email protected] /project/workspace
├── @adonisjs/[email protected]
├── @adonisjs/[email protected]
├── @adonisjs/[email protected]
├── @adonisjs/[email protected]
├── @adonisjs/[email protected]
├── @japa/[email protected]
├── @japa/[email protected]
├── @japa/[email protected]
├── @swc/[email protected]
├── @types/[email protected]
├── @vinejs/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Just for the record, here is the list of updated deps:

  dependencies
    @adonisjs/core              ~1mo        ^6.17.0  →        ^6.17.1  ~15d                   

  devDependencies
    eslint                      ~1mo        ^9.17.0  →        ^9.19.0   ~3d                   
    @adonisjs/eslint-config     ~5mo  ^2.0.0-beta.6  →  ^2.0.0-beta.7  ~4mo                   
    @japa/assert               ~10mo         ^3.0.0  →         ^4.0.1  ~20d  
    @japa/plugin-adonisjs       ~15d         ^3.0.2  →         ^4.0.0  ~15d  
    @japa/runner               ~10mo         ^3.1.4  →         ^4.1.0  ~18d
    @types/node                 ~2mo       ^22.10.2  →      ^22.10.10   ~4d                   
    ts-node-maintained          ~5mo        ^10.9.4  →        ^10.9.5   ~9d                   
    typescript                                 ~5.7  →         ~5.7.3  ~19d

Will try to investigate further this week.

@thetutlage
Copy link
Member

Super weird. Because the same code works flawlessly on my end. Finally, can you please run your code with NODE_DEBUG=adonisjs:app node ace ping and share the logs that appear on the terminal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?
Projects
None yet
Development

No branches or pull requests

2 participants