Skip to content

Commit c65d9b7

Browse files
authored
fix(builtin): strip BOM when parsing package.json (#1453)
Fixes #1448
1 parent aacd924 commit c65d9b7

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

internal/npm_install/generate_build_file.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ package(default_visibility = ["//visibility:public"])
102102
// generate all BUILD files
103103
generateBuildFiles(pkgs);
104104
}
105-
module.exports = {
106-
main,
107-
printPackageBin,
108-
printIndexBzl,
109-
};
105+
exports.main = main;
110106
/**
111107
* Generates all build files
112108
*/
@@ -468,7 +464,9 @@ def _maybe(repo_rule, name, **kwargs):
468464
function parsePackage(p, hide = true) {
469465
// Parse the package.json file of this package
470466
const packageJson = path.posix.join(p, 'package.json');
471-
const pkg = isFile(packageJson) ? JSON.parse(fs.readFileSync(packageJson, { encoding: 'utf8' })) :
467+
const stripBom = (s) => s.charCodeAt(0) === 0xFEFF ? s.slice(1) : s;
468+
const pkg = isFile(packageJson) ?
469+
JSON.parse(stripBom(fs.readFileSync(packageJson, { encoding: 'utf8' }))) :
472470
{ version: '0.0.0' };
473471
// Trim the leading node_modules from the path and
474472
// assign to _dir for future use
@@ -492,6 +490,7 @@ def _maybe(repo_rule, name, **kwargs):
492490
hideBazelFiles(pkg);
493491
return pkg;
494492
}
493+
exports.parsePackage = parsePackage;
495494
/**
496495
* Check if a bin entry is a non-empty path
497496
*/
@@ -958,6 +957,7 @@ nodejs_binary(
958957
}
959958
return result;
960959
}
960+
exports.printPackageBin = printPackageBin;
961961
function printIndexBzl(pkg) {
962962
let result = '';
963963
const executables = _findExecutables(pkg);
@@ -1001,6 +1001,7 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
10011001
}
10021002
return result;
10031003
}
1004+
exports.printIndexBzl = printIndexBzl;
10041005
/**
10051006
* Given a scope, return the skylark `node_module_library` target for the scope.
10061007
*/

internal/npm_install/generate_build_file.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function writeFileSync(p: string, content: string) {
9090
/**
9191
* Main entrypoint.
9292
*/
93-
function main() {
93+
export function main() {
9494
// find all packages (including packages in nested node_modules)
9595
const pkgs = findPackages();
9696

@@ -104,12 +104,6 @@ function main() {
104104
generateBuildFiles(pkgs)
105105
}
106106

107-
module.exports = {
108-
main,
109-
printPackageBin,
110-
printIndexBzl,
111-
};
112-
113107
/**
114108
* Generates all build files
115109
*/
@@ -515,11 +509,13 @@ function findScopes() {
515509
* package json and return it as an object along with
516510
* some additional internal attributes prefixed with '_'.
517511
*/
518-
function parsePackage(p: string, hide: boolean = true): Dep {
512+
export function parsePackage(p: string, hide: boolean = true): Dep {
519513
// Parse the package.json file of this package
520514
const packageJson = path.posix.join(p, 'package.json');
521-
const pkg = isFile(packageJson) ? JSON.parse(fs.readFileSync(packageJson, {encoding: 'utf8'})) :
522-
{version: '0.0.0'};
515+
const stripBom = (s: string) => s.charCodeAt(0) === 0xFEFF ? s.slice(1) : s;
516+
const pkg = isFile(packageJson) ?
517+
JSON.parse(stripBom(fs.readFileSync(packageJson, {encoding: 'utf8'}))) :
518+
{version: '0.0.0'};
523519

524520
// Trim the leading node_modules from the path and
525521
// assign to _dir for future use
@@ -1031,7 +1027,7 @@ function additionalAttributes(pkg: Dep, name: string) {
10311027
/**
10321028
* Given a pkg, return the skylark nodejs_binary targets for the package.
10331029
*/
1034-
function printPackageBin(pkg: Dep) {
1030+
export function printPackageBin(pkg: Dep) {
10351031
let result = '';
10361032
const executables = _findExecutables(pkg);
10371033
if (executables.size) {
@@ -1058,7 +1054,7 @@ nodejs_binary(
10581054
return result;
10591055
}
10601056

1061-
function printIndexBzl(pkg: Dep) {
1057+
export function printIndexBzl(pkg: Dep) {
10621058
let result = '';
10631059
const executables = _findExecutables(pkg);
10641060
if (executables.size) {

internal/npm_install/test/generate_build_file.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
const fs = require('fs');
2+
const path = require('path');
13
const {check, files} = require('./check');
2-
const {printPackageBin, printIndexBzl} = require('../generate_build_file');
4+
const {parsePackage, printPackageBin, printIndexBzl} = require('../generate_build_file');
35

46
describe('build file generator', () => {
57
describe('integration test', () => {
@@ -10,6 +12,14 @@ describe('build file generator', () => {
1012
});
1113
});
1214

15+
describe('parsing package.json', () => {
16+
it('should strip leading Byte-Order Mark character', () => {
17+
const pkgPath = path.join(process.env['TEST_TMPDIR'], 'package.json');
18+
fs.writeFileSync(pkgPath, `\uFEFF{"name": "foo"}`, 'utf-8');
19+
expect(parsePackage(path.dirname(pkgPath)).name).toBe('foo');
20+
});
21+
});
22+
1323
describe('should exclude nodejs_binary rules when', () => {
1424
const pkg = {_name: 'some_name', _dir: 'some_dir', _dependencies: [], _files: []};
1525

0 commit comments

Comments
 (0)