Skip to content

Commit

Permalink
Fix ownerOfFile bug on windows
Browse files Browse the repository at this point in the history
#1762 only fixed the bug on unix. On windows the bug is still lurking.
  • Loading branch information
ef4 committed May 28, 2024
1 parent e8a0dd0 commit f4f398c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/shared-internals/src/package-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Package from './package';
import { existsSync, realpathSync } from 'fs';
import { getOrCreate } from './get-or-create';
import resolvePackagePath from 'resolve-package-path';
import { dirname, sep } from 'path';
import { basename, dirname, join, resolve, sep } from 'path';

const realpathSyncCache = new Map<string, string>();

Expand Down Expand Up @@ -67,25 +67,29 @@ export default class PackageCache {
}

ownerOfFile(filename: string): Package | undefined {
let segments = filename.split(sep);
let candidate = filename;

// first we look through our cached packages for any that are rooted right
// at or above the file.
for (let length = segments.length; length >= 0; length--) {
if (segments[length - 1] === 'node_modules' || segments[length - 1] === '') {
// once we hit a node_modules or the filesystem root, we're leaving the
while (true) {
if (basename(candidate) === 'node_modules') {
// once we hit a node_modules, we're leaving the
// package we were in, so any higher caches don't apply to us
break;
}

let usedSegments = segments.slice(0, length);
let candidate = usedSegments.join(sep);
if (this.rootCache.has(candidate)) {
return this.rootCache.get(candidate);
}
if (getCachedExists([...usedSegments, 'package.json'].join(sep))) {
if (getCachedExists(join(candidate, 'package.json'))) {
return this.get(candidate);
}
let nextCandidate = resolve(candidate, '..');
if (nextCandidate === candidate) {
// got to the top
break;
}
candidate = nextCandidate;
}
}

Expand Down

0 comments on commit f4f398c

Please sign in to comment.