-
Notifications
You must be signed in to change notification settings - Fork 522
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
ts_project cannot compile a ts file that imports a generated json file #2843
Comments
microsoft/TypeScript#24744 kinda suggests "json always treated as a module" which is similar. In fact the JSON sources are always written to the output tree, which is why TS needs them to conform to the rootDir/outDir scheme that .ts inputs would. What we really want here is to say "you can use this json file to typecheck against, but there's no need to copy it to the output folder since it's already there". |
microsoft/TypeScript#36066 seems to explicitly break what we wanted here, which is a way to get the typeshape for the json file without having to treat it as an emittable source. |
I've successfully worked around this issue by using https://www.npmjs.com/package/json-to-dts to generate .d.ts files which can be passed to a ts_project via a js_library (to pick up DeclarationInfo provider). With this approach the .d.ts files can be passed to the ts_project rule instead of the generated json and ts_project doesn't complain about the root directory violation. |
@gregmagolan this is awesome. Just curious, are use using an |
was able to use |
According to my understanding of the comment left above, I have used used Note: i'm also using the load("@npm//json-dts-generator:index.bzl", "json_dts_generator")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "copy_to_bin")
package(default_visibility = ["//visibility:public"])
filegroup(
name = "data",
srcs = [
"data.json",
],
)
copy_to_bin(
name = "srcs",
srcs = ["data.json"]
)
json_dts_generator(
name = "data_declarations",
outs = [
"_common.d.ts",
"data.d.ts",
],
data = [":srcs"],
args = ["$(RULEDIR) $(RULEDIR)"],
)
js_library(
name = "data_js",
package_name = "data-lib",
package_path = "path/to/lib",
srcs = [
":data.json",
"_common.d.ts",
"data.d.ts",
],
deps = [
":data_declarations",
],
) |
I put up a minimal example of using the json-to-dts package I forked from json-dts-generator. https://github.com/gregmagolan/json-to-dts-example/blob/main/src/BUILD.bazel. Hope that helps. |
This issue has been automatically marked as stale because it has not had any activity for 6 months. It will be closed if no further activity occurs in 30 days. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs! |
This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?" |
Note, this is fixed in https://github.com/aspect-build/rules_ts |
The TypeScript compiler treats .json files as sources that must be within the rootDir. Unlike resolution of
.d.ts
files for typings, we can't simply pre-process the generated .json files as a separate project.Repro:
a.ts:
BUILD:
tsconfig.json:
Error:
However if the json file were represented as typings, then it would work. That is, when performing type-checking we would want a generated
.d.ts
file to show the type-shape of the json file. This would presumably be the output of some other rule that turns json schema into TypeScript typings, like the compiler does internally, but exposed as a separate "project".With the same a.ts and tsconfig.json as above, this now works:
However it's not really feasible for users to have to generate typings for their json manually, they want to do the simple thing that TypeScript normally supports if the .json file were actually located with the sources.
It feels like the JSON import feature added to TypeScript is just missing the pathmapping resolution logic that exists for .d.ts files, and so we ought to be able to file a bug upstream on the TypeScript project.
The text was updated successfully, but these errors were encountered: