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

URL's with @fs prefixes do not resolve properly on Windows #1982

Closed
3 tasks done
dvdzkwsk opened this issue Feb 11, 2021 · 1 comment
Closed
3 tasks done

URL's with @fs prefixes do not resolve properly on Windows #1982

dvdzkwsk opened this issue Feb 11, 2021 · 1 comment

Comments

@dvdzkwsk
Copy link

dvdzkwsk commented Feb 11, 2021

  • Read the docs.
  • Use Vite >=2.0. (1.x is no longer supported)
  • If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.

Describe the bug

When importing an asset that is outside of root, vite returns the absolute path to that file. This path is not served properly by the development server when on Windows because the path is incorrectly joined to fsRoot. The issue has to do with these lines in https://github.com/vitejs/vite/commits/main/packages/vite/src/node/server/middlewares/static.ts:

// fsRoot is set to "C:/" when on Windows
const fsRoot = os__default.platform() == 'win32' ? process.cwd().split(path__default.sep)[0] + '/' : '/';
const serveFromRoot = sirv(fsRoot, sirvOptions);
return (req, res, nexT) => {
  const url = req.url

  // url is "/@fs/C:/path/to/file.ext"
  if (url.startsWith(FS_PREFIX)) {

    // req.url is now "C:/path/to/file.ext"
    req.url = decodeURI(url.slice(FS_PREFIX.length));

    // tries to serve  "C:\C:\path\to\file.ext" (note the duplicate C:\ prefix)
    // because serveFromRoot JOINS fsRoot with the url. This is incorrect on Windows,
    // since fsRoot is not "/".
    serveFromRoot(req, res, next);
  }
}

function viaLocal(dir, isEtag, uri, extns) {
	let i=0, arr=toAssume(uri, extns);
	let abs, stats, name, headers;
	for (; i < arr.length; i++) {
		abs = path$1.normalize(path$1.join(dir, name=arr[i]));   // <---- Join happens here
                console.log(abs) // <-------------------------------------------- Observe incorrect path here
		if (abs.startsWith(dir) && fs$2.existsSync(abs)) {
			stats = fs$2.statSync(abs);
			if (stats.isDirectory()) continue;
			headers = toHeaders(name, stats, isEtag);
			headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
			return { abs, stats, headers };
		}
	}
}

Proposed Fix

I've explored two fixes for this, both of which solve the problem on my end:

  1. Strip fsRoot from the requested URL if fsRoot is not /
  2. Replace path.join() with path.resolve() in the viaLocal function

I hesitated from sending a PR because I wondered if you had a preferred approach or if I was overlooking anything, i.e. is it even necessary to have fsRoot be C:/ on Windows? If you can let me know your thoughts, I would be happy to open up a PR to address this.

Reproduction

https://github.com/davezuko/reproductions/tree/master/vite-1982

System Info

  • vite version: 2.0.0-beta.67 and 2.0.0-beta.68
  • Operating System: Windows 10
  • Node version: 12.14.1
  • Package manager (npm/yarn/pnpm) and version: yarn @ 1.21.1
@dvdzkwsk
Copy link
Author

Thanks @yyx990803 :)

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant