bazel-contrib / rules_nodejs

NodeJS toolchain for Bazel.
https://bazelbuild.github.io/rules_nodejs/
Apache License 2.0
724 stars 523 forks source link

yarn_ install does not seems to work with typescript #1178

Closed AnatoleLucet closed 5 years ago

AnatoleLucet commented 5 years ago

🐞 bug report

Affected Rule

The issue is caused by the rule:

Description

A bazel build //myApp does not install my npm's dependencies when myApp contain a ts_library rule.

This part of the doc advise to install deps with bazel run @nodejs//:yarn. But is there a way to automatically install deps when trying to build the app ?

I gess there is a way (or I am probably missing something) because dataform's config automatically do it (acording to their doc).

🔬 Minimal Reproduction

WORKSPACE :

workspace(
  name = 'my_app'
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "build_bazel_rules_nodejs",
    sha256 = "da217044d24abd16667324626a33581f3eaccabf80985b2688d6a08ed2f864be",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.37.1/rules_nodejs-0.37.1.tar.gz"]
)

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install", "node_repositories")
node_repositories(package_json = ["//:package.json"])
yarn_install(
  name = "npm",
  package_json = "//:package.json",
  yarn_lock = "//:yarn.lock"
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
install_bazel_dependencies()

load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
ts_setup_workspace()

load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dev_dependencies")
rules_nodejs_dev_dependencies()

BUILD :

exports_files([
    "tsconfig.json",
    "package.json"
], visibility = ["//visibility:public"])

package.json :

{
  ...
  "dependencies": {
    "express": "^4.17.1",
    "typescript": "^3.6.3"
  },
  "devDependencies": {
    "@bazel/typescript": "^0.37.1"
  }
}

myApp/BUILD :

package(default_visibility = ["//visibility:public"])

load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "app1",
    srcs = glob(["index.ts"])
)

myApp/index.ts :

const foo: string = 'bar';

console.log(foo);

But it works (it install my deps) when myApp simply has a nodejs_binary rule and a js file.

myApp/build :

load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

nodejs_binary(
    name = "myApp",
    entry_point = ":index.js",
)

🔥 Exception or Error

Just a bunch of missing input file '@npm//:node_modules/typescript/lib/x' for basically every files that he need in the typescript module.

🌍 Your Environment

Operating System:

Linux Mint 19.2

Output of bazel version:

Build label: 0.29.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Sep 10 13:44:39 2019 (1568123079)
Build timestamp: 1568123079
Build timestamp as int: 1568123079

Rules version (SHA):

da217044d24abd16667324626a33581f3eaccabf80985b2688d6a08ed2f864be
Toxicable commented 5 years ago

whars the exception?

alexeagle commented 5 years ago

You need to reference the @npm//package-name somewhere in your build (the deps of your ts_library?) or else the dependencies will never be installed (Bazel only does work required to build the output you request)

AnatoleLucet commented 5 years ago

@Toxicable edited

Toxicable commented 5 years ago

Yeah so since you havn't referenced any deps in @npm bazel dosen't go and resolve it. If you did declare one, such as @npm//tslib (you'll need this eventually) in your deps then bazel will go and fetch those deps before running the target

AnatoleLucet commented 5 years ago

Alright, and how could I declare a dependency ? I need to do something in my "root BUILD", right ?

Toxicable commented 5 years ago
ts_library(
    name = "app1",
    srcs = glob(["index.ts"]),
    deps = [
      "@npm//tslib"
    ]
)
AnatoleLucet commented 5 years ago

@Toxicable oh ok, I was asking because I've already done that and it still doesn't work

I've also added tslib in my package.json

I have the same list of errors except tslib has added himself to it :

missing input file '@npm//:node_modules/tslib/tslib.d.ts'
AnatoleLucet commented 5 years ago

I'll make a repo, it will be easier

AnatoleLucet commented 5 years ago

Ok well now it works after doing the same in a new folder.

I guess this is because I've manually deleted my node_modules folder for cleaning it. Is there a good way to clean node_modules ?

Toxicable commented 5 years ago

@AnatoleLucet You shouldn't really ever have to clean your node_modules if you change your package.json bazel will reinstall the modules itself. But as a last resort bazel clean --expunge will remove the node_modules that are sitting in bazels working dir

AnatoleLucet commented 5 years ago

@Toxicable That is true. Well thanks you for your really fast answer (@alexeagle included) !