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

Can't use tsc output for js_library #3329

Closed
cartmanez opened this issue Feb 15, 2022 · 4 comments
Closed

Can't use tsc output for js_library #3329

cartmanez opened this issue Feb 15, 2022 · 4 comments

Comments

@cartmanez
Copy link

🐞 bug report

Affected Rule

tsc

Is this a regression?

Not sure. Did not use bazel before

Description

js_library works just fine for me when it depends on the output of ts_project.
However, I can't achieve the same using tsc.
I.e. the below does not produce valid package when dist is a tsc rule, and works when dist is ts_project


js_library(
    name = "package-a",
    package_name = "@my-scope/package-a",
    srcs = ["package.json",":dist"],
    deps = [":dist"],
)

🔬 Minimal Reproduction

https://github.com/cartmanez/bazel-ts-repo

In that repo /packages/package-b depends on /packages/package-a, /packages/package-d depends on /packages/package-c


# this command will fail, because //packages/package-a is built using tsc
bazel build //packages/package-b:dist


# this command will succeed - //packages/package-c is built using ts_project
bazel build //packages/package-d:dist

🔥 Exception or Error

When dependent package is built with tsc, it is present in node_modules folder in sandbox, but it's dist folder is empty

packages/package-b/src/index.ts:1:31 - error TS2307: Cannot find module '@my-scope/package-a'

🌍 Your Environment

Operating System:

  Linux archlinux 5.16.9-arch1-1 #1 SMP PREEMPT Fri, 11 Feb 2022 22:42:06 +0000 x86_64 GNU/Linux  

Output of bazel version:

  
Bazelisk version: v1.11.0
Build label: 5.0.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Wed Jan 19 14:08:54 2022 (1642601334)
Build timestamp: 1642601334
Build timestamp as int: 1642601334  

Rules_nodejs version:

  5.1.0  

Anything else relevant?
Seems like no. Thank you in advance!

@alexeagle alexeagle added Can Close? We will close this in 30 days if there is no further activity question/docs labels Mar 1, 2022
@alexeagle
Copy link
Collaborator

something is wrong with your package-a:

$ bazel build //packages/package-a:dist
$ ls -R bazel-bin/packages/package-a/dist
bazel-bin/packages/package-a/dist:

tsc isn't writing any files into the directory bazel expects. Add --listEmittedFiles to that target to see why:

INFO: From Action packages/package-a/dist:
TSFILE: /home/alexeagle/.cache/bazel/_bazel_alexeagle/df8ac8c6303c857c7c7030efffd3687b/sandbox/linux-sandbox/2/execroot/bazel-ts-repo/packages/package-a/dist/index.js
TSFILE: /home/alexeagle/.cache/bazel/_bazel_alexeagle/df8ac8c6303c857c7c7030efffd3687b/sandbox/linux-sandbox/2/execroot/bazel-ts-repo/packages/package-a/dist/index.js.map
TSFILE: /home/alexeagle/.cache/bazel/_bazel_alexeagle/df8ac8c6303c857c7c7030efffd3687b/sandbox/linux-sandbox/2/execroot/bazel-ts-repo/packages/package-a/dist/index.d.ts
TSFILE: /home/alexeagle/.cache/bazel/_bazel_alexeagle/df8ac8c6303c857c7c7030efffd3687b/sandbox/linux-sandbox/2/execroot/bazel-ts-repo/packages/package-a/dist/index.d.ts.map

tsc is writing to the execroot, not the output folder. You need to pass it an argument saying where to write. See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_genrule_variables for some docs.
for example, adding args "--outDir", "$(@D)" makes it write to the correct place.

Then to make the compilation of package-b succeed you'll also need to fixup the tsconfig.json with a pathMapping section that tells TypeScript where to resolve package-a. See the docs for that too.

Maybe since your other issue about resolveJsonModule is fixed in the next release, you don't really want to use tsc anyway? it's certainly harder.

@cartmanez
Copy link
Author

Then to make the compilation of package-b succeed you'll also need to fixup the tsconfig.json with a pathMapping section that tells TypeScript where to resolve package-a. See the docs for that too.

Isn't js_library supposed to help with this?

Maybe since your other issue about resolveJsonModule is fixed in the next release, you don't really want to use tsc anyway? it's certainly harder.

Yes, maybe. I'm only starting my journey with bazel and would like to know how things work.

@github-actions github-actions bot removed the Can Close? We will close this in 30 days if there is no further activity label Mar 4, 2022
@alexeagle
Copy link
Collaborator

no, js_library doesn't help the typescript compiler resolve references from bazel-out. microsoft/TypeScript#37378 (comment) might help, with a change to typescript upstream.

@cartmanez
Copy link
Author

Got it, thanks

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

2 participants