GiovanniCardamone / fastify-autoroutes

fastest way to map directories to URLs in fastify
https://giovannicardamone.github.io/fastify-autoroutes/
MIT License
85 stars 12 forks source link

Bug Report: Windows fileystem issue #188

Closed karmabadger closed 2 years ago

karmabadger commented 2 years ago

Versions

Behavior

from this repo: https://github.com/karmabadger/fastify-routes-test

when running: npm run dev

> fileroutes@1.0.0 dev
> nodemon index.ts

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node index.ts`
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4
    return new (P || (P = Promise))(function (resolve, reject) {
           ^
Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\index.ts\src\routes does not exists
    at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:116:19
    at Generator.next (<anonymous>)
    at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
    at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:98:7)
    at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
    at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
[nodemon] app crashed - waiting for file changes before starting...

when running:

npm run start```

fileroutes@1.0.0 start node ./build/index.js

C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44 return next(new Error(${exports.ERROR_LABEL} dir ${dirPath} does not exists)); ^

Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\build\index.js\src\routes does not exists at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44:21 at Generator.next () at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71 at new Promise () at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12) at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:31:72) at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19) at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10) at processTicksAndRejections (node:internal/process/task_queues:83:21)


Am I doing this correctly? Perhaps I'm missing something? 
I feel like on windows only, it's not resolving the paths to each of the routes correctly when it's doing the dynamic routes imports.   ```C:\Users\wenxu\Projects\test\fileroutes\index.ts\src\routes does not exists.``` as we can see here, it's getting the absolute path to the file not the directly which causes issues here. 
I know that the node module filesystem will also complain if ```file://``` isn't prepended at the beginning to tell it the protocol if it's an absolute file path (at least I think that could be an issue)).

Does this issue even occur on linux or mac? 
If only on windows I could try to figure out what's wrong and make a PR, tho I'm a bit new to the Open source dev scene so Ill have to figure out how it works haha.

Thanks
GiovanniCardamone commented 2 years ago

do you have "routes" directory in "src" ?

GiovanniCardamone commented 2 years ago

can you also provide code how you used fastify-autoroutes?

karmabadger commented 2 years ago

do you have "routes" directory in "src" ?

image

karmabadger commented 2 years ago

can you also provide code how you used fastify-autoroutes? index.ts:


import fastify, { FastifyInstance } from "fastify";
import * as autoroutes from "fastify-autoroutes";

const app: FastifyInstance = fastify({ logger: true });

const startServer = async () => { await app.register(autoroutes.default, { dir: "./src/routes", prefix: "/api/v1", // -> optional });

await app.listen(3000); };

startServer();


src/routes/some.ts:
```ts
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { Resource } from 'fastify-autoroutes'

export default (fastify: FastifyInstance) => <Resource> {
  get: {
    handler: async (request: FastifyRequest, reply: FastifyReply) => 'Hello, Route!'
  }
}
GiovanniCardamone commented 2 years ago

if you cwd is on src, than, 'dir' should be only 'routes'

karmabadger commented 2 years ago

if you cwd is on src, than, 'dir' should be only 'routes' no I'm running index.ts from the root dir whcih is not in src. is it better to have it all in src instead?

GiovanniCardamone commented 2 years ago

your index.ts should be in "src" folder (usually this is the way is done) than you have to run the program using "src" (or dist, if it's builded) as cwd

example: ... "scripts": { "start": "node dist", "dev": "ts-node-dev src" }


in this case, if you use "routes", the plugin will know he have to take from "<your folder location>/src" (or dist if is builded)
karmabadger commented 2 years ago

your index.ts should be in "src" folder (usually this is the way is done) than you have to run the program using "src" (or dist, if it's builded) as cwd

example: ... "scripts": { "start": "node dist", "dev": "ts-node-dev src" }

in this case, if you use "routes", the plugin will know he have to take from "<your folder location>/src" (or dist if is builded)

whether it is in src shouldn't really matter right? it's just mostly depending on how the devs are used to?

karmabadger commented 2 years ago

I'm going to try it anyways image

karmabadger commented 2 years ago
ts-node src/index.ts
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4
    return new (P || (P = Promise))(function (resolve, reject) {
           ^
Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\src\index.ts\routes does not exists
    at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:116:19       
    at Generator.next (<anonymous>)
    at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
    at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:98:7)
    at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
    at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
GiovanniCardamone commented 2 years ago

ts-node src

if you start with ts-node src/index.ts the cwd is not setted to 'src', so the routes should be "../routes".

Usually, if you are not running a script, you run a module by the index file (so just "node dist (or ts-node src in your case)"

Screenshot_20_04_2022-08_44_07

GiovanniCardamone commented 2 years ago

I opened a pull request on your repo, hope this help :)