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

FileSystemRouter backslash bug on Windows #8667

Closed
ghost opened this issue Feb 3, 2024 · 1 comment
Closed

FileSystemRouter backslash bug on Windows #8667

ghost opened this issue Feb 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Feb 3, 2024

What version of Bun is running?

1.0.26-canary.106+fcf004776

What platform is your computer?

Microsoft Windows NT 10.0.22000.0 x64

What steps can reproduce the bug?

const router = new Bun.FileSystemRouter({
    dir: "./app",
    style: "nextjs",
    assetPrefix: "_assets/",
    fileExtensions: [".scss", ".css", ".js", ".ts", ".tsx", ".html", ".njk"],
  });

console.log(router.routes) /*
              Windows backslash kicks in...
        {
            "/\\": "D:\\Projs\\teste\\app\\index.html",
            "/\\filt\\/nest": "D:\\Projs\\teste\\app\\filt\\nest.ts",
            "/\\templates\\/test.html": "D:\\Projs\\teste\\app\\templates\\test.html.njk",
            "/\\/_index": "D:\\Projs\\teste\\app\\_index.ts",
            "/\\[test]\\rott\\/e": "D:\\Projs\\teste\\app\\[test]\\rott\\e.ts",
            "/\\[test]\\/indes": "D:\\Projs\\teste\\app\\[test]\\indes.ts",
            "/\\filt\\/[...aaaa]": "D:\\Projs\\teste\\app\\filt\\[...aaaa].ts",
            "/\\filt\\/[...slug]": "D:\\Projs\\teste\\app\\filt\\[...slug].ts",
            "/\\filt\\/[...zug]": "D:\\Projs\\teste\\app\\filt\\[...zug].ts",
        }
    */

What is the expected behavior?

router.match('/') /*
    MatchedRoute {
        filePath: "D:\\Projs\\teste\\app\\index.html",
        kind: "exact",
        name: "/",
        params: {},
        pathname: "/",
        query: {},
        scriptSrc: "index.html",
        src: "index.html",
    }
  */

What do you see instead?

router.match('/') // null

router.match('/\\') // MatchedRoute { ... }

Additional information

I was able to make a workaround, it´s ugly but it works:

  if (platform() == "win32") {
    const brokenMatcher = router.match.bind(router);

    router.match = function (input) {
      const url = new URL(typeof input == 'string' ? input : input.url);

      return brokenMatcher(
        url.pathname // use URL.pathname to remove the query, as it also gets "corrupted"
          .replaceAll(/\/(\w*)\/?/g, "/\\$1\\/") // remove any backslash along the string
          .replace(/(\w*)\\\/$/, "/$1") // remove trailing backslash
      );
    };
  }

#wind

@ghost ghost added the bug Something isn't working label Feb 3, 2024
@dylan-conway
Copy link
Member

this is fixed in #8644 . The bugfix will be available in 1.0.26

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

1 participant