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

Types are only made available when explicitly added in tsconfig.json #111

Closed
paullewis opened this issue Aug 1, 2022 · 9 comments
Closed
Assignees

Comments

@paullewis
Copy link

paullewis commented Aug 1, 2022

Hey,

Was just trying the 0.11.0 prerelease, and thought I'd let you know of an unusual behavior (in case you don't know!).

Supposing you have a BUILD.bazel file like this:

load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

ts_project(
    name = "src",
    srcs = [
        "src.ts",
    ],
    declaration = True,
    tsconfig = "//:tsconfig",
    deps = [
        "//:node_modules/@types/node",
    ],
)

ts_project(
    name = "src_test",
    testonly = True,
    srcs = ["src.test.ts"],
    declaration = True,
    tsconfig = "//:tsconfig",
    deps = [
        ":src",
        "//:node_modules/@types/node",
    ],
)

It doesn't really matter what you have in src.ts, but supposing the following is in src.test.ts:

import assert from 'assert';

console.log(assert);

You'll get the following error when trying to build :src_test:

src.test.ts(1,20): error TS2307: Cannot find module 'assert' or its corresponding type declarations.

It seems the double dep of "//:node_modules/@types/node" in both :src and :src_test is the root of the issue, though I'm not at all sure why. Removing @types/node from :src will prevent the error from emitting, but that doesn't seem like the right fix!

@paullewis
Copy link
Author

For clarity, @types/node is where the assert module is declared.

@paullewis
Copy link
Author

paullewis commented Aug 8, 2022

Just a heads up that this is present in rules_ts-1.0.0-rc0 (and rules_js-1.0.0 since that's how the node deps are presented).

@paullewis paullewis changed the title Declaring a dependency twice (direct and transitive) causes build failures Types are only made available when explicitly added in tsconfig.json Aug 8, 2022
@paullewis
Copy link
Author

paullewis commented Aug 8, 2022

Okay, I dug a little further and what I thought was the issue ... wasn't.

I've made a repo here: https://github.com/paullewis/repro-rules-ts-issue-111

According to the TS docs any @types dir in node_modules should be picked up. When looking at the repro repo in VSCode I see that the types for assert are fine, and I can Cmd + click to get to the node types. When I try a Bazel build it can't find the types, even though they are presented as a dep (//:node_modules/@types/node).

If, however, I explicitly add "types": ["node"] to the tsconfig.json (in the repro it's already there for uncommenting) then the build passes.

All very confusing to me, really. Setting the types in the tsconfig.json to ["node"] should, as I understand it, make tsc go and look in @types/node... but when it's not set it should still find @types/node anyway given they're presented as a dep.

@paullewis
Copy link
Author

I sorted a temporary workaround: set supports_workers = False in the ts_project rule.

@thesayyn
Copy link
Member

thesayyn commented Aug 9, 2022

@paullewis could you try with https://github.com/aspect-build/rules_ts/releases/tag/v1.0.0-rc1 and see if the issue persists?

@paullewis
Copy link
Author

Yes, that seems to fix it - thank you! I'll close the Issue. Out of interest, what was the cause?

@gregmagolan
Copy link
Member

gregmagolan commented Aug 9, 2022

The worker mode virtual file system @thesayyn wrote wasn't following the node_modules/@types symlink to check if it was pointing to a directory. When typescript asked for the contents of node_modules/@types it got back an empty list.

@gregmagolan
Copy link
Member

Was fixed by #125

@paullewis
Copy link
Author

I see - thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants