Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build fails when including paralleljs #1448

Closed
cshunger opened this issue Dec 13, 2019 · 4 comments · Fixed by #1453
Closed

Build fails when including paralleljs #1448

cshunger opened this issue Dec 13, 2019 · 4 comments · Fixed by #1453
Labels

Comments

@cshunger
Copy link

cshunger commented Dec 13, 2019

🐞 bug report

Affected Rule

The issue is caused by the rule:

yarn_install/npm_install

Is this a regression?

Unsure

Description

After including paralleljs the bazel build //... fails due to an invalid package.json format. The package.json format does not look to be out of the ordinary and can be processed by other tools like jq
https://github.com/parallel-js/parallel.js/blob/master/package.json

🔬 Minimal Reproduction

Using the example/nestjs

Do yarn add paralleljs then bazel build. //...

🔥 Exception or Error


ERROR: An error occurred during the fetch of repository 'npm':
   Traceback (most recent call last):
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 383
		_create_build_files(repository_ctx, "yarn_install", node, ...)
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 150, in _create_build_files
		fail(("generate_build_file.js failed:...)))
generate_build_file.js failed: 
STDOUT:

STDERR:
undefined:1
{
^

SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse ()
    at parsePackage (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:471:48)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:440:23
    at Array.forEach ()
    at findPackages (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:435:18)
    at main (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:97:22)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:72:9
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:3:17
    at Object. (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:9:3)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
ERROR: no such package '@npm//': Traceback (most recent call last):
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 383
		_create_build_files(repository_ctx, "yarn_install", node, ...)
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 150, in _create_build_files
		fail(("generate_build_file.js failed:...)))
generate_build_file.js failed: 
STDOUT:

STDERR:
undefined:1
{
^

SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse ()
    at parsePackage (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:471:48)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:440:23
    at Array.forEach ()
    at findPackages (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:435:18)
    at main (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:97:22)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:72:9
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:3:17
    at Object. (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:9:3)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
ERROR: no such package '@npm//': Traceback (most recent call last):
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 383
		_create_build_files(repository_ctx, "yarn_install", node, ...)
	File "/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl", line 150, in _create_build_files
		fail(("generate_build_file.js failed:...)))
generate_build_file.js failed: 
STDOUT:

STDERR:
undefined:1
{
^

SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse ()
    at parsePackage (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:471:48)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:440:23
    at Array.forEach ()
    at findPackages (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:435:18)
    at main (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:97:22)
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:72:9
    at /home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:3:17
    at Object. (/home/cshunger/.cache/bazel/_bazel_cshunger/e48e55cf1f507300f5a6e65defb96cd1/external/npm/generate_build_file.js:9:3)
    at Module._compile (internal/modules/cjs/loader.js:956:30)

🌍 Your Environment

Operating System:

  
ubuntu
  

Output of bazel version:

  
1.0.0
  

Rules version (SHA):

  
a54b2511d6dae42c1f7cdaeb08144ee2808193a088004fc3b464a04583d5aa2e
  

Anything else relevant?

cat -A paralleljs/package.json
M-oM-;M-?{$
^I"name": "paralleljs",$
^I"version": "0.2.1",$
^I"author": "Adam Savitzky <adam.savitzky@gmail.com>",$
^I"contributors": [$
^I^I"Sebastian Mayr <sebmaster16@gmail.com> (http://s3bmaster.blogspot.co.at/)"$
^I],$
^I"description": "parallel.js enables easy multi-thread processing in javascript",$
^I"main": "lib/parallel.js",$
^I"repository": {$
^I^I"type": "git",$
^I^I"url": "https://github.com/adambom/parallel.js.git"$
^I},$
^I"directories": {$
^I^I"lib": "lib",$
^I^I"test": "test"$
^I},$
^I"keywords": [$
^I^I"parallel",$
^I^I"spawn",$
^I^I"map",$
^I^I"thread",$
^I^I"parallel.js",$
^I^I"workers",$
^I^I"webworkers"$
^I],$
^I"devDependencies": {$
^I^I"jasmine-node": "x",$
^I^I"q": "x"$
^I},$
^I"license": "BSD",$
^I"scripts": {$
^I^I"test": "jasmine-node --verbose test/specs"$
^I},$
^I"engines": {$
^I^I"node": ">=0.9.10"$
^I},$
^I"testling" : {$
^I^I"scripts": [$
^I^I^I"test/lib/jasmine/jasmine.js",$
^I^I^I"test/specs/*.js",$
^I^I^I"test/runner.js"$
^I^I]$
^I}$
}%          
@alexeagle
Copy link
Collaborator

That package.json is interesting! It starts with three strange bytes before the opening curly brace.

$ od -xc node_modules/paralleljs/package.json
0000000    bbef    7bbf    090a    6e22    6d61    2265    203a    7022
        357 273 277   {  \n  \t   "   n   a   m   e   "   :       "   p

$ file -bi node_modules/paralleljs/package.json
text/plain; charset=utf-8

utf-8 but I dunno what that garbage is. The real question is just, are we parsing the package.json in a way we shouldn't.?

let's see in node:

> JSON.parse('node_modules/paralleljs/package.json')
SyntaxError: Unexpected token o in JSON at position 1

> require('./node_modules/paralleljs/package.json').name
'paralleljs'

I guess we could fix this bug by using require() instead. Maybe @clydin has some opinion on that

@alexeagle alexeagle added the bug label Dec 13, 2019
@alexeagle
Copy link
Collaborator

Ah, I'm learning. These three bites are The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF. Looks easy to strip.

@Toxicable
Copy link

Toxicable commented Dec 13, 2019

A BOM is considered white space so doing a trim on the JSON string should strip it

> JSON.parse(fs.readFileSync('node_modules/paralleljs/package.json', 'utf8'))
Thrown:
SyntaxError: Unexpected token  in JSON at position 0

>JSON.parse(fs.readFileSync('node_modules/paralleljs/package.json', 'utf8').trim())
{      
    name: 'paralleljs',  

@clydin
Copy link
Contributor

clydin commented Dec 13, 2019

Yes. It’s the UTF8 BOM. Windows systems have a tendency to add it to files and can be safely stripped. Trim is a viable option here as leading and trailing whitespace is not significant for a JSON file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants