Skip to content

Commit 47e4029

Browse files
committed
Strip leading UTF-8 BOM before parsing package.json
1 parent 27d8a02 commit 47e4029

8 files changed

Lines changed: 31 additions & 2 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-utf8-bom",
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 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-utf8-bom__no-bom",
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 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-utf8-bom__with-bom",
3+
"main": "./index.ts"
4+
}

packages/knip/src/util/fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const hasFileWithExtension = (cwd: string, dirName: string, extensions: s
5555
};
5656

5757
export const loadJSON = async (filePath: string) => {
58-
const contents = await loadFile(filePath);
58+
const contents = (await loadFile(filePath)).replace(/^/, '');
5959
try {
6060
return JSON.parse(contents);
6161
} catch {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default async function mapWorkspaces(cwd: string, workspaces: string[]):
2828
const dir = join(cwd, name);
2929
const manifestPath = join(cwd, match);
3030
try {
31-
const manifestStr = await readFile(manifestPath, 'utf8');
31+
const manifestStr = (await readFile(manifestPath, 'utf8')).replace(/^/, '');
3232
const manifest: PackageJson = JSON.parse(manifestStr);
3333
const pkgName = getPackageName(manifest, dir);
3434
const pkg: WorkspacePackage = { dir, name, pkgName, manifestPath, manifestStr, manifest };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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-utf8-bom');
8+
9+
test('Discover workspace whose package.json starts with a UTF-8 BOM', async () => {
10+
const options = await createOptions({ cwd });
11+
await assert.doesNotReject(main(options));
12+
const { counters } = await main(options);
13+
assert.equal(counters.processed, 2);
14+
});

0 commit comments

Comments
 (0)