From bd36cd0c7529514dc652baff5bf09d5cf3f6260e Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 1 Mar 2022 05:49:54 -0800 Subject: [PATCH] fix(typescript): account for rootDir when predicting json output paths (#3348) Fixes #3330 --- packages/typescript/internal/ts_project.bzl | 7 ++++--- .../test/ts_project/rootdir_with_value/BUILD.bazel | 3 +++ .../test/ts_project/rootdir_with_value/subdir/a.ts | 3 ++- .../test/ts_project/rootdir_with_value/subdir/file.json | 4 ++++ .../test/ts_project/rootdir_with_value/tsconfig.json | 2 ++ 5 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 packages/typescript/test/ts_project/rootdir_with_value/subdir/file.json diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index cb358e74f0..6ac368bafb 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -177,11 +177,12 @@ def _ts_project_impl(ctx): # tsc will only produce .json if it also produces .js if len(js_outs): pkg_len = len(ctx.label.package) + 1 if len(ctx.label.package) else 0 - json_outs = [ - ctx.actions.declare_file(_lib.join(ctx.attr.out_dir, src.short_path[pkg_len:])) + rootdir_replace_pattern = ctx.attr.root_dir + "/" if ctx.attr.root_dir else "" + json_outs = _declare_outputs(ctx, [ + _lib.join(ctx.attr.out_dir, src.short_path[pkg_len:].replace(rootdir_replace_pattern, "")) for src in ctx.files.srcs if src.basename.endswith(".json") and src.is_source - ] + ]) else: json_outs = [] diff --git a/packages/typescript/test/ts_project/rootdir_with_value/BUILD.bazel b/packages/typescript/test/ts_project/rootdir_with_value/BUILD.bazel index e513c82c19..972c7bf1c2 100644 --- a/packages/typescript/test/ts_project/rootdir_with_value/BUILD.bazel +++ b/packages/typescript/test/ts_project/rootdir_with_value/BUILD.bazel @@ -4,11 +4,14 @@ load("//packages/typescript:index.bzl", "ts_project") # Ensure that subdir/a.ts produces outDir/a.js SRCS = [ "subdir/a.ts", + "subdir/file.json", ] ts_project( name = "transpile", srcs = SRCS, + args = ["--listEmittedFiles"], + resolve_json_module = True, root_dir = "subdir", ) diff --git a/packages/typescript/test/ts_project/rootdir_with_value/subdir/a.ts b/packages/typescript/test/ts_project/rootdir_with_value/subdir/a.ts index a668b7e336..e48b89770f 100644 --- a/packages/typescript/test/ts_project/rootdir_with_value/subdir/a.ts +++ b/packages/typescript/test/ts_project/rootdir_with_value/subdir/a.ts @@ -1 +1,2 @@ -export const a: string = 'hello'; +import data from './file.json'; +export const a: string = 'hello' + JSON.stringify(data); \ No newline at end of file diff --git a/packages/typescript/test/ts_project/rootdir_with_value/subdir/file.json b/packages/typescript/test/ts_project/rootdir_with_value/subdir/file.json new file mode 100644 index 0000000000..40c1b22dd5 --- /dev/null +++ b/packages/typescript/test/ts_project/rootdir_with_value/subdir/file.json @@ -0,0 +1,4 @@ +[{ + "a": "b" +}] + \ No newline at end of file diff --git a/packages/typescript/test/ts_project/rootdir_with_value/tsconfig.json b/packages/typescript/test/ts_project/rootdir_with_value/tsconfig.json index aa5798f3ce..2aa21b5460 100644 --- a/packages/typescript/test/ts_project/rootdir_with_value/tsconfig.json +++ b/packages/typescript/test/ts_project/rootdir_with_value/tsconfig.json @@ -1,4 +1,6 @@ { "compilerOptions": { + "resolveJsonModule": true, + "esModuleInterop": true, } }