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

vite:resolve plugin incorrectly resolves knownJsSrcRE files from root #4161

Closed
6 tasks done
GrygrFlzr opened this issue Jul 7, 2021 · 3 comments · Fixed by #8808
Closed
6 tasks done

vite:resolve plugin incorrectly resolves knownJsSrcRE files from root #4161

GrygrFlzr opened this issue Jul 7, 2021 · 3 comments · Fixed by #8808
Assignees
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) windows only

Comments

@GrygrFlzr
Copy link
Member

GrygrFlzr commented Jul 7, 2021

Describe the bug

const knownJsSrcRE = /\.((j|t)sx?|mjs|vue|marko|svelte)($|\?)/

When attempting to access any files with an extension that matches the knownJsSrcRE regex, the vite:resolve plugin attempts to load them from the root of the filesystem. The following code in the plugin is the branch taken by such files.

// URL
// /foo -> /fs-root/foo
if (asSrc && id.startsWith('/')) {
const fsPath = path.resolve(root, id.slice(1))
if ((res = tryFsResolve(fsPath, options))) {
isDebug && debug(`[url] ${chalk.cyan(id)} -> ${chalk.dim(res)}`)
return res
}
}

If the file exists at root and does not exist in the static assets directory, the plugin will resolve to the root file.

Reproduction

Add a test.svelte file at the root. Then, initialize a vanilla Vite template and run Vite in debug mode.

npm init @vitejs/app resolve-bug
# choose vanilla
cd resolve-bug
npm i
npm run dev -- --debug

Visit http://localhost:3000/test.svelte

  vite v2.4.1 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 204ms.

  vite:resolve 2ms   /test.svelte -> D:/test.svelte +0ms
  vite:spa-fallback Not rewriting GET /test.svelte because the path includes a dot (.) character. +0ms
  vite:time 8ms   /test.svelte +0ms

The file is not loaded in an environment with no plugins, but combined with plugins such as vite-plugin-svelte it actually loads the file from root and resolves it if server.fs.strict is disabled, taking priority over source code (though lower priority than the static asset public directory).

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 10.89 GB / 31.93 GB
  Binaries:
    Node: 14.16.0 - C:\program files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.13 - C:\program files\nodejs\npm.CMD
  Browsers:
    Chrome: 91.0.4472.124
    Edge: Spartan (44.22000.1.0), Chromium (91.0.864.59)
    Internet Explorer: 11.0.22000.1
  npmPackages:
    vite: ^2.4.1 => 2.4.1

Used Package Manager

pnpm

Validations

@GrygrFlzr GrygrFlzr added bug p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) labels Jul 7, 2021
@antfu antfu self-assigned this Jul 9, 2021
@bluwy
Copy link
Member

bluwy commented Mar 12, 2022

I tested this on macos on both Vite 2.4.1 and 2.8.6, and it seems to be working for me:

  vite v2.4.1 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 131ms.

  vite:resolve 1ms   /test.svelte -> /Users/bjorn/Work/repros/vite-root-test/test.svelte +0ms
  vite:time 14ms  /test.svelte +0ms

So it seems like a Windows only issue

@sapphi-red
Copy link
Member

I tested this on windows on Vite 2.4.1 and 2.9.5 and I was not able to reproduce it.
It was resolved in the following order on both versions.

  • C:\path\to\workspace\public\test.svelte
  • C:\path\to\workspace\test.svelte
  • C:\test.svelte

@sapphi-red sapphi-red added cannot reproduce The bug cannot be reproduced and removed cannot reproduce The bug cannot be reproduced labels Apr 14, 2022
@sapphi-red
Copy link
Member

Or does it mean /test.svelte should not be resolved to C:\test.svelte?
Which the code is here.

// absolute fs paths
if (path.isAbsolute(id) && (res = tryFsResolve(id, options))) {
isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) windows only
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants