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

feat: add JsInfo provider to ts_config rule #315

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ bazel-output-base
examples/node_modules
examples/linked/node_modules
examples/linked_consumer/node_modules
examples/linked_tsconfig/node_modules
examples/linked_tsconfig_consumer/node_modules
19 changes: 19 additions & 0 deletions examples/linked_tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")

npm_link_all_packages()

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
":node_modules/@tsconfig/node18",
],
)

npm_package(
name = "linked_tsconfig",
srcs = [":tsconfig"],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions examples/linked_tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@lib/tsconfig",
"private": true,
"dependencies": {
"@tsconfig/node18": "1.0.1"
}
}
3 changes: 3 additions & 0 deletions examples/linked_tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node18/tsconfig.json"
}
23 changes: 23 additions & 0 deletions examples/linked_tsconfig_consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")

npm_link_all_packages()

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
":node_modules/@lib/tsconfig",
],
)

ts_project(
name = "lib",
srcs = [
"index.ts",
],
tsconfig = ":tsconfig",
deps = [
":node_modules/@types/node",
],
)
1 change: 1 addition & 0 deletions examples/linked_tsconfig_consumer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world')
7 changes: 7 additions & 0 deletions examples/linked_tsconfig_consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"@lib/tsconfig": "workspace:*",
"@types/node": "18.13.0"
}
}
4 changes: 4 additions & 0 deletions examples/linked_tsconfig_consumer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"//": "the hacky path below is required because while @lib/tsconfig/tsconfig.json _will_ resolve, tsc won't follow the symlink into the virtual store so the transitive npm dep of @lib/tsconfig on @tsconfig/node18/tsconfig.json won't be resolvable",
"extends": "../node_modules/.aspect_rules_js/@[email protected]/node_modules/@lib/tsconfig/tsconfig.json"
}
18 changes: 18 additions & 0 deletions examples/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
packages:
- linked
- linked_consumer
- linked_tsconfig
- linked_tsconfig_consumer
47 changes: 45 additions & 2 deletions ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_file_to_bin_action", "copy_files_to_bin_actions")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "js_info")
load(":ts_lib.bzl", _lib = "lib")

TsConfigInfo = provider(
Expand All @@ -30,21 +31,63 @@ TsConfigInfo = provider(

def _ts_config_impl(ctx):
files = [copy_file_to_bin_action(ctx, ctx.file.src)]

transitive_deps = [
depset(copy_files_to_bin_actions(ctx, ctx.files.deps)),
js_lib_helpers.gather_files_from_js_providers(
targets = ctx.attr.deps,
include_transitive_sources = True,
include_declarations = True,
include_npm_linked_packages = True,
)
),
]

# TODO: now that ts_config.bzl provides a JsInfo, we should be able to remove TsConfigInfo in the future
# since transitive files will now be passed through transitive_declarations in JsInfo
for dep in ctx.attr.deps:
if TsConfigInfo in dep:
transitive_deps.append(dep[TsConfigInfo].deps)

transitive_sources = js_lib_helpers.gather_transitive_sources([], ctx.attr.deps)

transitive_declarations = js_lib_helpers.gather_transitive_declarations(files, ctx.attr.deps)

npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = [],
deps = ctx.attr.deps,
)

npm_package_store_deps = js_lib_helpers.gather_npm_package_store_deps(
targets = ctx.attr.deps,
)

files_depset = depset(files)

runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = depset(), # tsconfig.json file won't be needed at runtime
data = [],
deps = ctx.attr.deps,
)

return [
DefaultInfo(files = depset(files)),
DefaultInfo(
files = files_depset,
runfiles = runfiles,
),
js_info(
# provide tsconfig.json file via `declarations` and not `sources` since they are only needed
# for downstream ts_project rules and not in downstream runtime binary rules
declarations = files_depset,
npm_linked_package_files = npm_linked_packages.direct_files,
npm_linked_packages = npm_linked_packages.direct,
npm_package_store_deps = npm_package_store_deps,
sources = depset(),
transitive_declarations = transitive_declarations,
transitive_npm_linked_package_files = npm_linked_packages.transitive_files,
transitive_npm_linked_packages = npm_linked_packages.transitive,
transitive_sources = transitive_sources,
),
TsConfigInfo(deps = depset(files, transitive = transitive_deps)),
]

Expand Down
10 changes: 5 additions & 5 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ This is an error because Bazel does not run actions unless their outputs are nee
if len(outputs) > 0:
inputs_depset = depset(
copy_files_to_bin_actions(ctx, inputs),
transitive = transitive_inputs + [_gather_declarations_from_js_providers(ctx.attr.srcs + ctx.attr.deps)],
transitive = transitive_inputs + [_gather_declarations_from_js_providers(ctx.attr.srcs + [ctx.attr.tsconfig] + ctx.attr.deps)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it possible there will be no tsconfig so [None] will be passed to _gather_declarations_from_js_providers? Is that ok?

Copy link
Member Author

@gregmagolan gregmagolan Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the same thing. Not possible as the attribute is "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), in the rule. You either get the user tsconfig or an auto-generated one that is done in the ts_project macro

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)

ctx.actions.run(
Expand All @@ -237,12 +237,12 @@ This is an error because Bazel does not run actions unless their outputs are nee
},
)

transitive_sources = js_lib_helpers.gather_transitive_sources(output_sources, ctx.attr.srcs + ctx.attr.deps)
transitive_sources = js_lib_helpers.gather_transitive_sources(output_sources, ctx.attr.srcs + [ctx.attr.tsconfig] + ctx.attr.deps)

transitive_declarations = js_lib_helpers.gather_transitive_declarations(output_declarations, ctx.attr.srcs + ctx.attr.deps)
transitive_declarations = js_lib_helpers.gather_transitive_declarations(output_declarations, ctx.attr.srcs + [ctx.attr.tsconfig] + ctx.attr.deps)

npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = ctx.attr.srcs,
srcs = ctx.attr.srcs + [ctx.attr.tsconfig],
deps = ctx.attr.deps,
)

Expand All @@ -257,7 +257,7 @@ This is an error because Bazel does not run actions unless their outputs are nee
ctx = ctx,
sources = output_sources_depset,
data = ctx.attr.data,
deps = ctx.attr.srcs + ctx.attr.deps,
deps = ctx.attr.srcs + [ctx.attr.tsconfig] + ctx.attr.deps,
)

providers = [
Expand Down