diff --git a/packages/typescript/internal/ts_config.bzl b/packages/typescript/internal/ts_config.bzl index 88841534ba..8c20ccdaed 100644 --- a/packages/typescript/internal/ts_config.bzl +++ b/packages/typescript/internal/ts_config.bzl @@ -59,15 +59,20 @@ extended configuration file as well, to pass them both to the TypeScript compile def _join(*elements): return "/".join([f for f in elements if f]) -def _relative_path(tsconfig_path, dest): +def _relative_path(tsconfig, dest): + relative_to = tsconfig.dirname if dest.is_source: # Calculate a relative path from the directory where we're writing the tsconfig # back to the sources root - workspace_root = "/".join([".."] * len(tsconfig_path.dirname.split("/"))) + workspace_root = "/".join([".."] * len(relative_to.split("/"))) return _join(workspace_root, dest.path) - # TODO: basename isn't exactly correct, there could be a subdir - return dest.basename + # Bazel guarantees that srcs are beneath the package directory, and we disallow + # tsconfig.json being generated with a "/" in the name. + # So we can calculate a relative path from e.g. + # bazel-out/darwin-fastbuild/bin/packages/typescript/test/ts_project/generated_tsconfig/gen_src + # to + return dest.path[len(relative_to) + 1:] def _write_tsconfig_rule(ctx): # TODO: is it useful to expand Make variables in the content? @@ -109,6 +114,8 @@ def write_tsconfig(name, config, files, out, extends = None): out: the file to write extends: a label for a tsconfig.json file to extend from, if any """ + if out.find("/") >= 0: + fail("tsconfig should be generated in the package directory, to make relative pathing simple") if extends: config["extends"] = "__extends__" diff --git a/packages/typescript/test/ts_project/generated_tsconfig/default/BUILD.bazel b/packages/typescript/test/ts_project/generated_tsconfig/default/BUILD.bazel index b826e3a90a..fc4a6ed0ba 100644 --- a/packages/typescript/test/ts_project/generated_tsconfig/default/BUILD.bazel +++ b/packages/typescript/test/ts_project/generated_tsconfig/default/BUILD.bazel @@ -3,7 +3,9 @@ load("//packages/typescript:index.bzl", "ts_project") ts_project( tsconfig = { - "types": [], + "compilerOptions": { + "types": [], + }, }, ) diff --git a/packages/typescript/test/ts_project/generated_tsconfig/gen_src/BUILD.bazel b/packages/typescript/test/ts_project/generated_tsconfig/gen_src/BUILD.bazel index be3800d951..be87f274ca 100644 --- a/packages/typescript/test/ts_project/generated_tsconfig/gen_src/BUILD.bazel +++ b/packages/typescript/test/ts_project/generated_tsconfig/gen_src/BUILD.bazel @@ -4,14 +4,17 @@ load("//packages/typescript:index.bzl", "ts_project") write_file( name = "gen_src", - out = "a.ts", + out = "subdir/a.ts", content = ["export const a: string = 'hello world';"], ) ts_project( - srcs = ["a.ts"], + srcs = ["subdir/a.ts"], tsconfig = { - "types": [], + "compilerOptions": { + "rootDir": "subdir", + "types": [], + }, }, ) @@ -19,5 +22,4 @@ generated_file_test( name = "test", src = "expected.js_", generated = ":a.js", - tags = ["manual"], )