bazelbuild / rules_typescript

MOVED to https://github.com/bazelbuild/rules_nodejs/tree/3.x/third_party/github.com/bazelbuild/rules_typescript
https://github.com/bazelbuild/rules_nodejs
Apache License 2.0
275 stars 94 forks source link

Get error error TS2307 even though dependency is direct #464

Closed fortuna closed 5 years ago

fortuna commented 5 years ago

🐞 bug report

Affected Rule

The issue is caused by the rule: ts_library

Is this a regression?

I don't know. I'm just converting my code to Bazel and this error is blocking me from making progress.

Description

I'm getting the error:

error TS2307: transitive dependency on external/npm/node_modules/ShadowsocksConfig/shadowsocks_config.ts not allowed. Please add the BUILD target to your rule's deps.

However, my rule is already depending on ShadowsocksConfig:

ts_library(
    name = "manager_service",
    srcs = ["manager_service.ts"],
    deps = [
        ":manager_metrics",
        ":server_config",
        ":shared_metrics",
        "//src/shadowbox/infrastructure:json_config",
        "//src/shadowbox/infrastructure:logging",
        "//src/shadowbox/model",
        "@npm//ShadowsocksConfig",
        "@npm//restify",
        "@npm//@types/restify",
    ],
)

There's no way to depend on ShadowsocksConfig/shadowsocks_config.ts directly because it's an external package and there's no rule for it.

Notice that ShadowsocksConfig is not an NPM package, but a Github repository. This is how the entry on package.json looks like:

    "ShadowsocksConfig": "Jigsaw-Code/outline-shadowsocksconfig#^v0.0.9",

πŸ”¬ Minimal Reproduction

git clone git@github.com:Jigsaw-Code/outline-server.git outline-server
cd outline-server
git checkout fortuna-bazel
bazel test //src/shadowbox/server:manager_service_test

πŸ”₯ Exception or Error


error TS2307: transitive dependency on external/npm/node_modules/ShadowsocksConfig/shadowsocks_config.ts not allowed. Please add the BUILD target to your rule's deps.

🌍 Your Environment

Operating System:

  
  macOS version 10.14.5
  MacBook Pro (13-inch, 2018)
  

Output of bazel version:

  
Build label: 0.28.1
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Jul 19 15:22:50 2019 (1563549770)
Build timestamp: 1563549770
Build timestamp as int: 1563549770
  

Rules version (SHA):

  
?
  

Anything else relevant?

alexeagle commented 5 years ago

The next bit of debugging would be to find out what is in there?

$ bazel query --output=build @npm//ShadowsocksConfig
# /home/alexeagle/.cache/bazel/_bazel_alexeagle/06fc29ac6c20b1d31292d0b1d355ba07/external/npm/ShadowsocksConfig/BUILD.bazel:86:1
node_module_library(
  name = "ShadowsocksConfig",
  srcs = ["@npm//ShadowsocksConfig:ShadowsocksConfig__files"],
  deps = ["@npm//ShadowsocksConfig:ShadowsocksConfig__contents", "@npm//base-64:base-64__contents", "@npm//punycode:punycode__contents"],
)
$ bazel query 'deps(@npm//ShadowsocksConfig:ShadowsocksConfig)' | grep \\\.ts
Loading: 0 packages loaded
@npm//:node_modules/ShadowsocksConfig/shadowsocks_config.ts
@npm//:node_modules/ShadowsocksConfig/shadowsocks_config.spec.ts
@npm//:node_modules/ShadowsocksConfig/punycode.d.ts

I think the issue is that ts_library filters .d.ts files from the deps to be the dependencies, but this package is "published" with .ts sources instead. It's atypical for npm packages to publish typescript sources.

I made a little repro with a ts_library named foo. Building with bazel build -s prints the subcommands including the call to tsc. Looking in dist/bin/foo_es5_tsconfig.json we see

"allowedStrictDeps": [
            "external/npm/node_modules/ShadowsocksConfig/punycode.d.ts",
            "foo.ts"
        ],

doesn't think that shadowsocks_config.ts is a valid allowed dependency.

Here is where it's calculated: https://github.com/bazelbuild/rules_typescript/blob/78af714217bd2b844d2f75961263be4a9e39773e/internal/common/compilation.bzl#L299

fortuna commented 5 years ago

I think you are right: ShadowsocksConfig was badly configured. I switched to the #master version, which has an main field in package.json and it works.

Thanks for taking a look!