oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.22k stars 2.77k forks source link

Bun.FileSystemRouter router match unknown behaviour #12206

Open AlbertSabate opened 4 months ago

AlbertSabate commented 4 months ago

What version of Bun is running?

1.1.17

What platform is your computer?

Darwin 23.5.0 x86_64 i386

What steps can reproduce the bug?

Create a bun project with FileSystemRouter:

Files created (narrowed down):

Create file on process.cwd() - for example test.js:

const router = new Bun.FileSystemRouter({
  style: "nextjs",
  dir: process.cwd() + '/src/pages',
});

const route = router.match('http://localhost:3001/admin/6679fbe17b41431a977163fd/providers/create');
console.log(router);
console.log(route);

What is the expected behavior?

My expectation is the definition of the documentation. https://bun.sh/docs/api/file-system-router

MatchedRoute {
  filePath: "/xxxxxx/src/pages/admin/[businessId]/providers/create.tsx",
  kind: "dynamic",
  name: "/admin/[businessId]/providers/create",
  params: {
    businessId: "6679fbe17b41431a977163fd",
  },
  pathname: "/admin/6679fbe17b41431a977163fd/providers/create",
  query: {},
  scriptSrc: "admin/[businessId]/providers/create.tsx",
  src: "admin/[businessId]/providers/create.tsx",
}

What do you see instead?

MatchedRoute {
  filePath: "/xxxxxx/src/pages/admin/[businessId]/providers/create.tsx",
  kind: "dynamic",
  name: "/admin/[businessId]/providers/create",
  params: {
    businessId: [ "6679fbe17b41431a977163fd", "6679fbe17b41431a977163fd" ],
  },
  pathname: "/admin/6679fbe17b41431a977163fd/providers/create",
  query: {
    businessId: [ "6679fbe17b41431a977163fd", "6679fbe17b41431a977163fd" ],
  },
  scriptSrc: "admin/[businessId]/providers/create.tsx",
  src: "admin/[businessId]/providers/create.tsx",
}

Additional information

aralroca commented 4 months ago

I found another issue with nested dynamic routes in Bun 1.1.20-canary.1+ae1948925:

URL: http://localhost:3000/fdsgsdfg/a

Matched Route:

{
  route: MatchedRoute {
    filePath: "/Users/aralroca/Documents/repos/error-bug/build/pages/[test]/a/[test2]/lala.js",
    kind: "dynamic",
    name: "/[test]/a/[test2]/lala",
    params: {
      test: "fdsgsdfg",
    },
    pathname: "/fdsgsdfg/a",
    query: {
      test: "fdsgsdfg",
    },
    scriptSrc: "[test]/a/[test2]/lala.js",
    src: "[test]/a/[test2]/lala.js",
  },

I was expecting /[test]/a instead of /[test]/a/[test2]/lala.

├── pages
│   ├── [test]
│   │   └── a
│   │       ├── [test2]
│   │       │   └── lala.js
│   │       └── index.js
│   ├── index.js
│   └── page-b.js

I hope it can be prioritized @paperdave @Jarred-Sumner , since FileSystemRouter makes it easy to create frameworks, but they better not be buggy frameworks 🙏