From 41a26789ef5c867aa1faaee53ca97635fdc32428 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Mon, 21 Oct 2024 14:47:31 -0700 Subject: [PATCH] fix: align ts_project(data) runfiles with js_library(data) --- examples/js_lib/data.json | 1 + examples/js_lib/index.js | 2 +- examples/json_data/BUILD.bazel | 55 ++++++++++++++++++++++++++++++ examples/json_data/src/asset.txt | 1 + examples/json_data/src/fg.json | 1 + examples/json_data/src/fg.txt | 1 + examples/json_data/src/index.ts | 16 +++++++++ examples/json_data/src/src.json | 1 + examples/json_data/src/tsdata.json | 1 + examples/json_data/src/tsdata.txt | 1 + examples/proto_grpc/BUILD.bazel | 4 ++- ts/private/ts_project.bzl | 10 ++++++ 12 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 examples/js_lib/data.json create mode 100644 examples/json_data/BUILD.bazel create mode 100644 examples/json_data/src/asset.txt create mode 100644 examples/json_data/src/fg.json create mode 100644 examples/json_data/src/fg.txt create mode 100644 examples/json_data/src/index.ts create mode 100644 examples/json_data/src/src.json create mode 100644 examples/json_data/src/tsdata.json create mode 100644 examples/json_data/src/tsdata.txt diff --git a/examples/js_lib/data.json b/examples/js_lib/data.json new file mode 100644 index 00000000..986ead87 --- /dev/null +++ b/examples/js_lib/data.json @@ -0,0 +1 @@ +{ "number": 1 } diff --git a/examples/js_lib/index.js b/examples/js_lib/index.js index e8f7328d..f7d6a842 100644 --- a/examples/js_lib/index.js +++ b/examples/js_lib/index.js @@ -1 +1 @@ -module.exports = 1 +module.exports = require('./data.json').number diff --git a/examples/json_data/BUILD.bazel b/examples/json_data/BUILD.bazel new file mode 100644 index 00000000..4fd8a563 --- /dev/null +++ b/examples/json_data/BUILD.bazel @@ -0,0 +1,55 @@ +load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary") +load("@aspect_rules_ts//ts:defs.bzl", "ts_project") +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +ts_project( + name = "tsc", + srcs = [ + "src/index.ts", + "src/src.json", + ], + assets = [ + "src/asset.txt", + ], + data = [ + "src/tsdata.json", + "src/tsdata.txt", + ":fg", + ], + resolve_json_module = True, + tsconfig = { + "compilerOptions": { + "resolveJsonModule": True, + }, + }, + deps = [ + "//examples:node_modules/@types/node", + ], +) + +filegroup( + name = "fg", + srcs = [ + "src/fg.json", + "src/fg.txt", + ], +) + +js_binary( + name = "bin1", + data = [":tsc"], + entry_point = "src/index.js", +) + +js_run_binary( + name = "run1", + srcs = [], + outs = ["src/all.txt"], + args = ["%s/src" % package_name()], + tool = ":bin1", +) + +build_test( + name = "test", + targets = ["src/all.txt"], +) diff --git a/examples/json_data/src/asset.txt b/examples/json_data/src/asset.txt new file mode 100644 index 00000000..18a5eac2 --- /dev/null +++ b/examples/json_data/src/asset.txt @@ -0,0 +1 @@ +{"asset": 42} diff --git a/examples/json_data/src/fg.json b/examples/json_data/src/fg.json new file mode 100644 index 00000000..840a1624 --- /dev/null +++ b/examples/json_data/src/fg.json @@ -0,0 +1 @@ +{ "fg": 42 } diff --git a/examples/json_data/src/fg.txt b/examples/json_data/src/fg.txt new file mode 100644 index 00000000..6217d717 --- /dev/null +++ b/examples/json_data/src/fg.txt @@ -0,0 +1 @@ +{"fg": 42} diff --git a/examples/json_data/src/index.ts b/examples/json_data/src/index.ts new file mode 100644 index 00000000..9a777d5e --- /dev/null +++ b/examples/json_data/src/index.ts @@ -0,0 +1,16 @@ +const fs = require('fs') +const path = require('path') + +const dataFiles = [ + './asset.txt', + './fg.json', + './fg.txt', + './src.json', + './tsdata.json', + './tsdata.txt', +] + +fs.writeFileSync( + path.join(process.argv[2], 'all.txt'), + dataFiles.map((f) => fs.readFileSync(path.join(__dirname, f))).join('') +) diff --git a/examples/json_data/src/src.json b/examples/json_data/src/src.json new file mode 100644 index 00000000..878c1b91 --- /dev/null +++ b/examples/json_data/src/src.json @@ -0,0 +1 @@ +{ "asset": 42 } diff --git a/examples/json_data/src/tsdata.json b/examples/json_data/src/tsdata.json new file mode 100644 index 00000000..f6c9c189 --- /dev/null +++ b/examples/json_data/src/tsdata.json @@ -0,0 +1 @@ +{ "data": 42 } diff --git a/examples/json_data/src/tsdata.txt b/examples/json_data/src/tsdata.txt new file mode 100644 index 00000000..687a5638 --- /dev/null +++ b/examples/json_data/src/tsdata.txt @@ -0,0 +1 @@ +{"data": 42} diff --git a/examples/proto_grpc/BUILD.bazel b/examples/proto_grpc/BUILD.bazel index 65f740aa..28b3f1aa 100644 --- a/examples/proto_grpc/BUILD.bazel +++ b/examples/proto_grpc/BUILD.bazel @@ -50,6 +50,9 @@ ts_proto_library( ts_project( name = "proto_grpc", srcs = ["main.ts"], + data = [ + "package.json", # for type=module + ], deps = [ ":logger_ts_proto", ":status_ts_proto", @@ -59,7 +62,6 @@ ts_project( js_test( name = "main", data = [ - "package.json", # for type=module "proto_grpc", ], entry_point = "main.js", diff --git a/ts/private/ts_project.bzl b/ts/private/ts_project.bzl index dfd5c629..9da21069 100644 --- a/ts/private/ts_project.bzl +++ b/ts/private/ts_project.bzl @@ -325,11 +325,21 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details. output_types_depset = depset(output_types) output_sources_depset = depset(output_sources) + # Align runfiles config with rules_js js_library() to align behaviour. + # See https://github.com/aspect-build/rules_js/blob/v2.1.0/js/private/js_library.bzl#L241-L254 runfiles = js_lib_helpers.gather_runfiles( ctx = ctx, sources = output_sources_depset, data = ctx.attr.data, deps = srcs_tsconfig_deps, + data_files = ctx.files.data, + copy_data_files_to_bin = True, # NOTE: configurable (default true) in js_library() + no_copy_to_bin = [], # NOTE: configurable (default []) in js_library() + include_sources = True, + include_types = False, + include_transitive_sources = True, + include_transitive_types = False, + include_npm_sources = True, ) providers = [