Skip to content

Commit

Permalink
Merge pull request #1949 from embroider-build/ownerOfFile-bug
Browse files Browse the repository at this point in the history
Fix ownerOfFile bug on windows
  • Loading branch information
ef4 authored May 29, 2024
2 parents e8a0dd0 + 5c9a87f commit e093274
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"browserslist": "^4.14.0",
"graceful-fs": "^4.0.0",
"@types/eslint": "^8.37.0",
"[email protected]": "5.0.0"
"[email protected]": "5.0.0",
"@embroider/shared-internals": "workspace:*"
}
},
"devDependencies": {
Expand Down Expand Up @@ -66,4 +67,4 @@
},
"wildcardLabel": "unlabeled"
}
}
}
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 } 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
58 changes: 25 additions & 33 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-packages/support/suite-setup-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function githubMatrix() {
let dir = resolve(__dirname, '..', '..', 'tests', 'scenarios');
let { stdout } = await execa(
'scenario-tester',
['list', '--require', 'ts-node/register', '--files', '*-test.ts', '--matrix', 'pnpm run test --filter %s'],
['list', '--require', 'ts-node/register', '--files', '*-test.ts', '--matrix', 'pnpm run test --filter "/^%s/"'],
{
cwd: dir,
preferLocal: true,
Expand Down
2 changes: 0 additions & 2 deletions tests/scenarios/util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ supportMatrix(Scenarios.fromDir(dirname(require.resolve('@embroider/util/package
'test.js': `
const { module: QModule, test } = require("qunit");
const semver = require("semver");
const { PackageCache } = require("@embroider/shared-internals");
QModule("shared-internals", function () {
test("testing on node 12", function (assert) {
assert.ok(
Expand Down

0 comments on commit e093274

Please sign in to comment.