Skip to content

More ergonomic way to generate tsconfig.json #2058

Closed

Description

@alexeagle

Some discussion on Slack points to a desire to use ts_project but not check in a tsconfig.json file for each compilation.

This is already possible in userspace and is demonstrated here:
https://github.com/bazelbuild/rules_nodejs/blob/d9ce5e8893a301238a7024899d850258e73c9ebf/packages/typescript/checked_in_ts_project.bzl#L18-L47

But this is a little unwieldy for users.

The core problem is that paths in the tsconfig file are relative to the location it was found on disk, so by generating it, you're making it a lot harder to point to the sources. Maybe one way to solve this is with a little syntax sugar around skylib's write_file like

def write_tsconfig(name, config, files, out):
    # workspace is up three dirs (bazel-out/arch/bin) plus number of segments in the package
    workspace_root = "/".join([".."] * (3 + len(native.package_name().split("/"))))

    amended_config = struct(
        files = ["/".join([workspace_root, native.package_name(), f]) for f in files],
        **config
    )
    write_file(
        name = name,
        content = [amended_config.to_json()],
        out = out,
    )

This would be used like

    tsconfig = "tsconfig_%s.json" % name

    write_tsconfig(
        name = "_gen_tsconfig_%s" % name,
        config = {
            "compilerOptions": struct(
                lib = ["es2017", "dom"],
                strict = True,
                target = "es2015",
                module = "commonjs",
                removeComments = True,
                declaration = True,
                skipLibCheck = True,
            ),
        },
        files = [src],
        out = tsconfig,
    )

    ts_project(
        name = name,
        srcs = [src],
        declaration = True,
        tsconfig = tsconfig,
        **kwargs
    )

It's pretty awkward to mix dict and struct in this way but I didn't find a better solution yet. You could have a compiler_options attribute containing the struct, but you still need a dict-type attribute for a catch-all of tsconfig top-level properties that we don't know about.

Another consideration here is keeping a good developer experience, if you have no tsconfig.json file in source control then your editor doesn't know what to do. Maybe a generated_file_test could be used to check in a tsconfig.json but assert it matches what Bazel would generate - but again the files can't actually be the same if they contain any tsconfig.json-relative paths. Another answer is to recommend having the base tsconfig.json (with the compilerOptions shared by editor and Bazel) and then only generate tsconfig that extends from it (like ts_library does)

#1931 suggests using the existing ts_config rule which makes sense too

Activity

  1. added 8 commits that reference this issue on Aug 20, 2020
    3a506e9
    d0d6bf3
    4b62bfa
    096345c
    0577d01
    c44964b
    9d34089
    8570d64
  2. added a commit that references this issue on Aug 28, 2020
    09ec233
  3. added a commit that references this issue on Jan 15, 2021
    9671291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions