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

Root Directory Not Properly Indentified #74

Closed
keithlee2010 opened this issue Jul 24, 2022 · 1 comment
Closed

Root Directory Not Properly Indentified #74

keithlee2010 opened this issue Jul 24, 2022 · 1 comment

Comments

@keithlee2010
Copy link

keithlee2010 commented Jul 24, 2022

This package is used as a dependency with Nuxt. When importing file outside the project root on a windows machine the 'readNearestPackageJSON ' function enters an infinite loop. Running the same code in Linux does not present the problem. The exit condition of the while loop is never met because root directory is misidentified. The root is 'C:' (not '/'). The solution is finding the root directory agnostic of the OS. Perhaps something like this:

path.parse(process.cwd()).root

Current Function:

export function readNearestPackageJSON (path: string): PackageJson | undefined {
  while (path && path !== '.' && path !== '/') {
    path = join(path, '..')
    try {
      const pkg = readFileSync(join(path, 'package.json'), 'utf-8')
      try {
        return JSON.parse(pkg)
      } catch {}
      break
    } catch {}
  }
}

Possible Updated Function:

export function readNearestPackageJSON (path: string): PackageJson | undefined {

  const root = path.parse(process.cwd()).root;
  while (path && path !== '.' && path !== root) {
    path = join(path, '..')
    try {
      const pkg = readFileSync(join(path, 'package.json'), 'utf-8')
      try {
        return JSON.parse(pkg)
      } catch {}
      break
    } catch {}
  }
}
@pi0 pi0 closed this as completed in 000c6ad Sep 6, 2022
@pi0
Copy link
Member

pi0 commented Sep 6, 2022

Thanks for reporting issue and sorry took long to address. Using unjs/pathe to fix. It handles join('C:\\', '..') to be /.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants