Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix test
  • Loading branch information
jwoo0122 committed Jan 22, 2025
commit 35b8e9b311bf124b64b9a73673d9aa38dfa2fe0c
1 change: 1 addition & 0 deletions packages/knip/fixtures/yarn-pnp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn/install-state.gz
Binary file not shown.
24 changes: 15 additions & 9 deletions packages/knip/src/manifest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { isFile } from '../util/fs.js';
import { dirname, join } from '../util/path.js';
import { _require } from '../util/require.js';

let isPnPEnabled: boolean | 'NOT_DETERMINED_YET' = 'NOT_DETERMINED_YET';
const pnpStatus = {
cwd: '',
enabled: false,
}

type LoadPackageManifestOptions = { dir: string; packageName: string; cwd: string };

Expand All @@ -26,24 +29,26 @@ const findNearestPnPFile = (startDir: string): string | null => {
};

const tryLoadManifestWithYarnPnp = (cwd: string, packageName: string) => {
if (isPnPEnabled === false) {
if (pnpStatus.cwd === cwd && pnpStatus.enabled === false) {
return null;
}

try {
if (isPnPEnabled === 'NOT_DETERMINED_YET') {
if (pnpStatus.cwd !== cwd) {
const pnpPath = findNearestPnPFile(cwd);

if (pnpPath != null) {
const pnp = _require(pnpPath);
pnp.setup();
isPnPEnabled = true;
pnpStatus.cwd = cwd;
pnpStatus.enabled = true;
} else {
isPnPEnabled = false;
pnpStatus.cwd = cwd;
pnpStatus.enabled = false;
}
}

if (isPnPEnabled) {
if (pnpStatus.enabled) {
const pnpApi = _require('pnpapi');

if (pnpApi != null) {
Expand Down Expand Up @@ -71,9 +76,10 @@ const tryLoadManifestWithYarnPnp = (cwd: string, packageName: string) => {

export const loadPackageManifest = ({ dir, packageName, cwd }: LoadPackageManifestOptions) => {
// 1. Try Yarn PnP first
const pnpManifest = tryLoadManifestWithYarnPnp(cwd, packageName);
if (pnpManifest != null) {
return pnpManifest;
const manifest = tryLoadManifestWithYarnPnp(cwd, packageName);

if (manifest != null) {
return manifest;
}

// 2. Fallback to traditional node_modules resolution
Expand Down
24 changes: 13 additions & 11 deletions packages/knip/test/yarn-pnp.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from 'bun:test';
import { test, describe, beforeAll } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../src/index.js';
import { resolve } from '../src/util/path.js';
Expand All @@ -7,16 +7,18 @@ import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/yarn-pnp');

test('Find unused dependencies', async () => {
const { counters } = await main({
...baseArguments,
cwd,
isStrict: true,
});
describe('Yarn PnP Tests', () => {
test('Find unused dependencies in yarn pnp', async () => {
const { counters } = await main({
...baseArguments,
cwd,
isStrict: true,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 1,
total: 4,
assert.deepEqual(counters, {
...baseCounters,
processed: 1,
total: 4,
});
});
});
Loading