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

import.meta.glob with {as: "raw"} doesn't handle relative paths #7307

Closed
7 tasks done
judofyr opened this issue Mar 13, 2022 · 1 comment · Fixed by #7371
Closed
7 tasks done

import.meta.glob with {as: "raw"} doesn't handle relative paths #7307

judofyr opened this issue Mar 13, 2022 · 1 comment · Fixed by #7371

Comments

@judofyr
Copy link

judofyr commented Mar 13, 2022

Describe the bug

import.meta.glob("../index.html", { as: "raw" }) fails with an error like "no such file or directory, open '/home/projects/index.html'". This seems to be caused by the following line:

await fsp.readFile(path.join(base, file), 'utf-8')
.

base is the common base (i.e. the directory root) and file is the relative path from the current file. This means that the .. is essentially applied "twice" and Vite is not able find the file.

Reproduction

https://stackblitz.com/edit/vitejs-vite-pvrnsj?file=src/main.js

System Info

2.9.0-beta.0

Used Package Manager

yarn

Logs

Error: Build failed with 1 error:
html:/home/projects/vitejs-vite-pvrnsj/index.html:1:7: ERROR: [plugin: vite:dep-scan] ENOENT: no such file or directory, open '/home/projects/index.html'
    at failureErrorWithLog (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:1557:15)
    at eval (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:1215:28)
    at runOnEndCallbacks (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:1005:63)
    at buildResponseToResult (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:1213:7)
    at eval (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:1322:14)
    at eval (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:637:9)
    at handleIncomingPacket (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:734:9)
    at Socket.readFromStdout (/home/projects/vitejs-vite-pvrnsj/node_modules/esbuild/lib/main.js:604:7)
    at Socket.EventEmitter.emit (https://vitejs-vite-pvrnsj.w.staticblitz.com/blitz.5bca79c80956550a0ee18f24bf5b874e18bb8626.js:6:131220)
    at addChunk (https://vitejs-vite-pvrnsj.w.staticblitz.com/blitz.5bca79c80956550a0ee18f24bf5b874e18bb8626.js:6:537045)

Validations

@toshify
Copy link
Contributor

toshify commented Mar 18, 2022

The issue occurs because in await fsp.readFile(path.join(base, file), 'utf-8'), file has been manipulated to include a number of ../ (as many as parentDepth). When joining base and file, parentDepth amount of folders are stripped from base. PR #7371 suggests fixing this by using path.join(base, file[i]) instead. file[i] is the unmanipulated path relative to base that file is created from.

toshify added a commit to toshify/vite that referenced this issue Mar 19, 2022
Contain test within `glob-import`-folder
Remove comments referencing issue number
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants