daml/compiler/daml-extension/BUILD.bazel
Moritz Kiefer bf39f4890c
Upgrade vsce and markdown-it (#12431)
This upgrades vsce to the latest version and also markdown-it which is
unfortunately still pinned to an older version with a vulnerability.

There are some minor changes required to our invocations of vsce. I
tried to test this locally up to the point where it fails due to me
not having a token. We’ll only fully see it working after the next
stable release unfortunately.

changelog_begin
changelog_end
2022-01-17 13:40:42 +00:00

119 lines
4.3 KiB
Python

# Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
package(default_visibility = ["//visibility:public"])
load("@build_environment//:configuration.bzl", "npm_version")
filegroup(
name = "json-files",
srcs = glob([
"*.json",
"syntaxes/*.json",
]),
)
sh_test(
name = "valid-json",
srcs = ["ci-tests.sh"],
args = [
"$(location @jq_dev_env//:jq)",
"$(locations :json-files)",
],
data = [
":json-files",
"@jq_dev_env//:jq",
],
# This is required to get the test to run on Windows. Otherwise, it looks
# like Bazel decides that because it cannot create symlinks on Windows, it
# may as well just give up and start the test without any of its
# dependencies available at all. But not warn about it, so the script can
# just fail at build time when it doesn't find anything.
deps = ["@bazel_tools//tools/bash/runfiles"],
)
# For some reason,
# 1. Bazel only exposes the node_modules dependency as a list of files, not as
# a folder, and
# 2. Copying these files over is surprisingly slow on my machine.
#
# Because `vsce` needs to run in a folder where all of the node_modules
# dependencies are already populated, this separate step takes all of the
# node_module files, one by one (because that is how Bazel exposes them),
# copies them to their intended place, and then bundles the whole node_modules
# folder as a tarball so the next task, below, can depend on that cached
# tarball and be fast.
# Also for some reason on Windows I get "cannot ceate node_modules: file
# exists", so at this point I'm completely out of trust.
genrule(
name = "node_deps_cache",
srcs = ["@daml_extension_deps//:node_modules"],
outs = ["node_modules.tar.gz"],
cmd = """
if [[ -d node_modules ]]; then
rm -rf node_modules
fi
mkdir node_modules
cd node_modules
for f in $(locations @daml_extension_deps//:node_modules); do
# Because Bazel paths are weird, we need to remove everything
# before node_modules. We also need to extract the path separately
# from the filename because we need to create the path (mkdir -p)
# before we can write the file
file=$$(basename $$f)
dir=$$(dirname $$f | sed 's:^.*/node_modules/::')
mkdir -p $$dir
cp ../$$f $$dir/$$file
done
cd ..
# Avoid file path too long errors on Windows of the form
# .../tar_dev_env/usr/bin/tar: node_modules/.cache/terser-webpack-plugin/...: file name is too long (cannot be split); not dumped
$(execpath //bazel_tools/sh:mktgz) $@ --exclude="node_modules/.cache/*" node_modules
""",
tools = ["//bazel_tools/sh:mktgz"],
)
# This rule is not reproducible. `vsce package` generates a `.vsix` file which
# is just a zip archive. The order of entries in that archive is
# non-deterministic and the individual entries contain non-reproducible
# timestamps.
genrule(
name = "vsix",
srcs = glob([
"package.json",
"syntaxes/*",
"snippets/*",
"images/*",
"*.json",
"README.md",
"yarn.lock",
"src/*",
]) + [
":node_deps_cache",
],
outs = ["daml-bundled.vsix"],
# rm -rf can fail with "directory not empty" on Windows.
# As a workaround we add `|| return`.
cmd = """
export HOME=/does-not-exist
set -euo pipefail
TMP_DIR=$$(mktemp -d)
cleanup () {{ rm -rf $$TMP_DIR || return; }}
trap cleanup EXIT
DIR=$$PWD
cp -r compiler/daml-extension $$TMP_DIR
cd $$TMP_DIR/daml-extension
tar xzf $$DIR/$(location :node_deps_cache)
sed -i "s/__VERSION__/{npm}/" package.json
sed -i 's/"name": "daml"/"name": "daml-bundled"/' package.json
$$DIR/$(location //:yarn) compile
# vsce needs Yarn or NPM in path and for some reason NPM
# fails to find npm-cli.js so we use Yarn.
PATH=$$(dirname $$DIR/$(location //:yarn)):$$PATH $$DIR/$(location @daml_extension_deps//vsce/bin:vsce) package --yarn -o $$DIR/$@
""".format(npm = npm_version),
tools = [
"//:yarn",
"@daml_extension_deps//vsce/bin:vsce",
],
)