aspect-build / rules_js

High-performance Bazel rules for running Node.js tools and building JavaScript projects
https://docs.aspect.build/rules/aspect_rules_js
Apache License 2.0
309 stars 106 forks source link

Failed to parse valid yaml in npm_translate_lock.bzl #282

Closed arobinson closed 2 years ago

arobinson commented 2 years ago

I’m having an issue with moving from my POC application to our actual application. I’m getting this error while parsing the pnpm lock file:

(15:38:58) ERROR: /Users/andrew/development/work/client/.../WORKSPACE.bazel:32:19: fetching npm_translate_lock rule //external:npm: Traceback (most recent call last):
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/npm_translate_lock.bzl", line 509, column 33, in _impl
                lockfile = _process_lockfile(rctx)
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/npm_translate_lock.bzl", line 164, column 37, in _process_lockfile
                lockfile = utils.parse_pnpm_lock(rctx.read(rctx.path(rctx.attr.pnpm_lock)))
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/utils.bzl", line 49, column 23, in _parse_pnpm_lock
                return _parse_yaml(lockfile_content)
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/yaml.bzl", line 42, column 31, in parse
                _handle_PARSE_NEXT(state, input, stack, starlark)
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/yaml.bzl", line 141, column 30, in _handle_PARSE_NEXT
                _set_result_value(starlark, stack, _parse_scalar(state["buffer"]))
        File "/Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/yaml.bzl", line 346, column 24, in _set_result_value
                curr_result.append(value)
Error: 'string' value has no field or method 'append'
(15:38:58) ERROR: no such package '@npm//': 'string' value has no field or method 'append'

To debug this, I added a print statement where it was dying:

        if type(curr_result) == "dict":
            curr_result[kns_states[-1]["key"]] = value
        else:
            print("curr_result", curr_result, "value", value)
            curr_result.append(value)

This printed this right before failing:

(15:42:39) DEBUG: /Users/andrew/.cache/bazel/f26303f99f5aa5dc2ecc7fa834780eb5/external/aspect_rules_js/npm/private/yaml.bzl:346:18: curr_result |- value npm i nyc

Looks like there is a bug in the yaml parser. Here is the entry in the pnpm-lock.yaml that looks to be causing the issue:

  /istanbul/0.4.5:
    resolution: {integrity: sha512-nMtdn4hvK0HjUlzr1DrKSUY8ychprt8dzHOgY2KXsIhHu5PuQQEOTM27gV9Xblyon7aUH/TSFIjRHEODF/FRPg==}
    deprecated: |-
      This module is no longer maintained, try this instead:
        npm i nyc
      Visit https://istanbul.js.org/integrations for other alternatives.
    hasBin: true
    dependencies:
      abbrev: 1.0.9
      async: 1.5.2
      escodegen: 1.8.1
      esprima: 2.7.3
      glob: 5.0.15
      handlebars: 4.7.7
      js-yaml: 3.14.1
      mkdirp: 0.5.5
      nopt: 3.0.6
      once: 1.4.0
      resolve: 1.1.7
      supports-color: 3.2.3
      which: 1.3.1
      wordwrap: 1.0.0
    dev: true

Using rules_js:

http_archive(
    name = "aspect_rules_js",
    sha256 = "80e168f9cd62f3640de429b70b34ff817d0d94ada2abaf2cffeef46e35434e1d",
    strip_prefix = "rules_js-1.0.0-rc.1",
    url = "https://github.com/aspect-build/rules_js/archive/refs/tags/v1.0.0-rc.1.tar.gz",
)
alexeagle commented 2 years ago

Yup that multi-line string under deprecated is probably not handled by our parser

kormide commented 2 years ago

Yup that multi-line string under deprecated is probably not handled by our parser

Yeah this is the first multiline string case we've come across in a lockfile. I'll have a look at improving the parser.

kormide commented 2 years ago

Fixed in https://github.com/aspect-build/rules_js/commit/62f52282e3a10a400121908f35c875323808c4c1.