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

nudging tuyau to the location of controllers #8

Open
Fawwaz-2009 opened this issue Oct 18, 2024 · 6 comments
Open

nudging tuyau to the location of controllers #8

Fawwaz-2009 opened this issue Oct 18, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@Fawwaz-2009
Copy link

I'm using module base folder structure and it's seems tuyau can't find the controllers, is there is a way to nudge tuyau on where to look for the controllers?

image

@Julien-R44
Copy link
Owner

How do you define your routes? In what files + via what kind of import?

If possible a reproduction would be super helpful

@Fawwaz-2009
Copy link
Author

hey Julien thanks for the quick reply and sure thing will create a reproduction soon

@Fawwaz-2009
Copy link
Author

Hey Julien 👋 please find the reproduction below, also many thanks for you and the team for making this, I'm new to adonisjs and I like a lot about it but the type safety to inertia has been a big annoyance, so many thanks again 🙏

https://github.com/Fawwaz-2009/tuyau-adonisjs-reproduction

image

@Julien-R44
Copy link
Owner

Thank you very much for reproducing the issue
I don't have a clean solution in the short term, but I can offer you a workaround.

Here's the problem: try running node ace list:routes --json on your reproduction. You'll get the following output:

[
  // ....
  {
    "name": "",
    "pattern": "/some-thing",
    "methods": [
      "GET"
    ],
    "handler": {
      "type": "controller",
      "moduleNameOrPath": "../controllers/index.js",
      "method": "index"
    },
    "middleware": []
  }
]

As you can see, the path to the controller is relative from the file where you defined your route, meaning: app/someModule/routes/index.ts. And from Tuyau, I don't have that information. Therefore, I'm unable to determine where your controller is located.

I hope that makes sense. There might potentially be a patch that can be applied to @adonisjs/http-server to fix this, but I need to discuss it with the team first.

Now, the workaround I can suggest is to use subpath imports to register your routes:

import router from '@adonisjs/core/services/router'
const SomeController = () => import('#my-module/controllers/someControllers')

function someModuleRoutes() {
  router.get('/some-thing', [SomeController, 'index'])
}

export default someModuleRoutes

Of course, don't forget to register this subpath import in your package.json.
And it will work!

@Julien-R44 Julien-R44 added the bug Something isn't working label Oct 19, 2024
@Benjamin-htr
Copy link

Benjamin-htr commented Nov 27, 2024

Hi !

In my project, I want to change the file structure to organize the code by functionality. Before doing so, I wanted to test if tuyau support modified paths.

So I took this adonis starter who use this type of structure "by module" : https://github.com/batosai/adonis-starter-kit/tree/main in which I installed tuyau and followed your recommendations in this issue using subpaths imports but I have the same problem, tuyau can't find the controllers.

In the package.json I've even specifically specified the path for each controller of the auth module to test and it doesn't work.

image

Here is the repoduction link : https://github.com/Benjamin-htr/reproduction_tuyau_bug

Maybe I missed something?

EDIT :: I was doing that :

# package.json
...
"imports": {
    "#auth/start/*": "./src/auth/start/*.js",
    "#auth/controllers/session_controller": "./src/auth/app/controllers/session_controller.js",
    "#auth/controllers/forgot_password_controller": "./src/auth/app/controllers/forgot_password_controller.js",
    "#auth/controllers/reset_password_controller": "./src/auth/app/controllers/reset_password_controller.js",
    "#auth/controllers/impersonates_controller": "./src/auth/app/controllers/impersonates_controller.js",
    "#auth/policies/*": "./src/auth/app/policies/*.js",
    "#auth/mails/*": "./src/auth/app/mails/*.js",
    "#auth/middleware/*": "./src/auth/app/middleware/*.js",
    "#auth/validators/*": "./src/auth/app/validators/*.js",
    ...
  },
...

AND

# src/auth/start/routes.ts
...
const SessionController = () => import('#auth/controllers/session_controller')
const ForgotPasswordController = () => import('#auth/controllers/forgot_password_controller')
const ResetPasswordController = () => import('#auth/controllers/reset_password_controller')
const ImpersonatesController = () => import('#auth/controllers/impersonates_controller')
...

But by doing this, it works :

# package.json
...
"imports": {
    "#auth/*": "./src/auth/*.js",
    ...
  },
...

and :

# src/auth/start/routes.ts
...
const SessionController = () => import('#auth/app/controllers/session_controller')
const ForgotPasswordController = () => import('#auth/app/controllers/forgot_password_controller')
const ResetPasswordController = () => import('#auth/app/controllers/reset_password_controller')
const ImpersonatesController = () => import('#auth/app/controllers/impersonates_controller')
...

I think tuyau need the fullpath inside the lazy loading to find the path

@Benjamin-htr
Copy link

However, I'd like to help improve the internal workings of @tuyau or @adonisjs/http-server to make things easier.

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