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: use the closest package.json as root when workspace not found fo… #3374

Merged
merged 1 commit into from
May 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/vite/src/node/server/searchRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,30 @@ function hasRootFile(root: string): boolean {
return ROOT_FILES.some((file) => fs.existsSync(join(root, file)))
}

function hasPackageJSON(root: string) {
const path = join(root, 'package.json')
return fs.existsSync(path)
}

/**
* Search up for the nearest `package.json`
*/
export function searchForPackageRoot(current: string, root = current): string {
if (hasPackageJSON(current)) return current

const dir = dirname(current)
// reach the fs root
if (!dir || dir === current) return root

return searchForPackageRoot(dir, root)
}

/**
* Search up for the nearest workspace root
*/
export function searchForWorkspaceRoot(
current: string,
root = current
root = searchForPackageRoot(current)
): string {
if (hasRootFile(current)) return current
if (hasWorkspacePackageJSON(current)) return current
Expand Down