Skip to content

Commit bfb4867

Browse files
committed
Skip workspace with invalid JSON manifest
1 parent 47e4029 commit bfb4867

7 files changed

Lines changed: 34 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@fixtures/workspaces-malformed-manifest",
3+
"private": true,
4+
"workspaces": ["packages/*"]
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 2;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@fixtures/workspaces-malformed-manifest__broken",
3+
"main": "./index.ts",
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@fixtures/workspaces-malformed-manifest__good",
3+
"main": "./index.ts"
4+
}

packages/knip/src/util/map-workspaces.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { readFile } from 'node:fs/promises';
22
import { glob } from 'tinyglobby';
33
import type { PackageJson, WorkspacePackage } from '../types/package-json.ts';
44
import { partition } from './array.ts';
5+
import { debugLog } from './debug.ts';
56
import { ConfigurationError } from './errors.ts';
7+
import { logWarning } from './log.ts';
68
import { getPackageName } from './package-name.ts';
79
import { join } from './path.ts';
810

@@ -36,9 +38,11 @@ export default async function mapWorkspaces(cwd: string, workspaces: string[]):
3638
if (pkgName) wsPkgNames.add(pkgName);
3739
else throw new ConfigurationError(`Missing package name in ${manifestPath}`);
3840
} catch (error) {
39-
// @ts-expect-error
40-
if (error?.code === 'ENOENT') debugLog('*', `Unable to load package.json for ${name}`);
41-
else throw error;
41+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
42+
debugLog('*', `Unable to load package.json for ${name}`);
43+
} else if (error instanceof SyntaxError) {
44+
logWarning('WARNING', `Skipping workspace ${name}: invalid JSON in ${manifestPath} (${error.message})`);
45+
} else throw error;
4246
}
4347
}
4448

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../src/index.ts';
4+
import { createOptions } from './helpers/create-options.ts';
5+
import { resolve } from './helpers/resolve.ts';
6+
7+
const cwd = resolve('fixtures/workspaces-malformed-manifest');
8+
9+
test('Skip workspace with invalid JSON in package.json (warn, continue)', async () => {
10+
const options = await createOptions({ cwd });
11+
await assert.doesNotReject(main(options));
12+
});

0 commit comments

Comments
 (0)