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

fix: incorrectly resolving knownJsSrcRE files from root (fixes #4161) #8808

Merged
merged 3 commits into from Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -24,6 +24,7 @@ import {
isDataUrl,
isExternalUrl,
isFileReadable,
isNonDriveRelativeAbsolutePath,
isObject,
isPossibleTsOutput,
isTsRequest,
Expand Down Expand Up @@ -172,8 +173,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
// relative
if (
id.startsWith('.') ||
(preferRelative && /^\w/.test(id)) ||
importer?.endsWith('.html')
((preferRelative || importer?.endsWith('.html')) && /^\w/.test(id))
Comment on lines -175 to +176
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was incorrect. If importer is html it was always handled as relative path.

So /foo.svelte will be resolved to

const basedir = importer ? path.dirname(importer) : process.cwd() // C:\Users\green\workspace
const fsPath = path.resolve(basedir, id) // C:\foo.svelte
const normalizedFsPath = normalizePath(fsPath) // C:/foo.svelte

) {
const basedir = importer ? path.dirname(importer) : process.cwd()
const fsPath = path.resolve(basedir, id)
Expand Down Expand Up @@ -239,7 +239,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

// absolute fs paths
if (path.isAbsolute(id) && (res = tryFsResolve(id, options))) {
if (
isNonDriveRelativeAbsolutePath(id) &&
(res = tryFsResolve(id, options))
) {
Comment on lines +242 to +245
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the condition above is fixed, this is still needed.
/foo.svelte will be handled as C:/foo.svelte inside tryFsResolve by fs.statSync and others.

isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
}
Expand Down
11 changes: 11 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1063,3 +1063,14 @@ export function stripBomTag(content: string): string {
}

export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename)

const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/

/**
* path.isAbsolute also returns true for drive relative paths on windows (e.g. /something)
* this function returns false for them but true for absolute paths (e.g. C:/something)
*/
export const isNonDriveRelativeAbsolutePath = (p: string): boolean => {
if (!isWindows) return p.startsWith('/')
return windowsDrivePathPrefixRE.test(p)
}