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

[Bug]: ts_projects fail on Windows (with standalone strategy) without converting all targets to (composite) project references #664

Open
willjschmitt opened this issue Aug 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@willjschmitt
Copy link

willjschmitt commented Aug 1, 2024

What happened?

I'm trying to use ts_project with granular typescript libraries in our monorepo, which works fine on Linux, but fails on Windows due to lack of sandboxing support (I think). It looks like in general ts_project rules are compiling (or attempting to compile) typescript sources outside of the declared srcs for the target, since the depedency source *.ts files are available on the path and not just the compiled .d.ts/*.js files. This means we get a "'rootDir' is expected to contain all source files." error when depending on any other ts_project, and/or 2) we get a "EPERM: operation not permitted" error when we compile a dependency in a child directory once and then again in the dependency itself

https://github.com/bazelbuild/rules_nodejs/wiki/Debugging-problems-with-ts_project/#ts6059-file-is-not-under-rootdir suggests that this indeed likely related to a standalone spawn strategy, where we have unrelated ts files to the target getting included into the compilation implicitly, but it's unclear what the solve would be on Windows, in the absence of a sandbox spawn strategy.

Version

Development (host) and target OS/architectures: Windows 10 x86_64

Output of bazel --version: "bazel 7.1.1"

Version of the Aspect rules, or other relevant rules from your
WORKSPACE or MODULE.bazel file: da284ad

Language(s) and/or frameworks involved: rules_ts examples

How to reproduce

git clone https://github.com/aspect-build/rules_ts
cd rules_ts

# Only because aspect-cli wasn't resolving.
rm .bazeliskrc

# runfiles necessary on Windows.
bazel build //examples/ts_project_dep/b:ts --enable_runfiles

Yields:

INFO: Analyzed target //examples/ts_project_dep/b:ts (1 packages loaded, 3 targets configured).
INFO: From TsValidateOptions examples/ts_project_dep/b/ts.optionsvalid.d.ts:
'DOSKEY' is not recognized as an internal or external command,
operable program or batch file.
ERROR: C:/users/willschmitt/documents/achillea/rules_ts/examples/ts_project_dep/b/BUILD.bazel:3:11: Transpiling TypeScript project @@//examples/ts_project_dep/b:ts [tsc -p examples/ts_project_dep/b/tsconfig.json] failed: (Exit 2): tsc.bat failed: error executing TsProject command (from target //examples/ts_project_dep/b:ts) bazel-out\x64_windows-opt-exec-ST-13d3ddad9198\bin\external\_main~ext~npm_typescript\tsc_\tsc.bat --project examples/ts_project_dep/b/tsconfig.json --outDir examples/ts_project_dep/b --rootDir ... (remaining 1 argument skipped)
'DOSKEY' is not recognized as an internal or external command,
operable program or batch file.
error TS5033: Could not write file 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.js': EPERM: operation not permitted, open 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.js'.
examples/ts_project_dep/b/b.ts(1,19): error TS6059: File 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.ts' is not under 'rootDir' 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/b'. 'rootDir' is expected to contain all source files.
INFO: Found 1 target...
Target //examples/ts_project_dep/b:ts failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 7.168s, Critical Path: 6.20s
INFO: 5 processes: 2 internal, 3 local.
ERROR: Build did NOT complete successfully

Turning on explainFiles indicates the inclusion into the compilation since a.ts is around and is imported by b.ts:

error TS5033: Could not write file 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.js': EPERM: operation not permitted, open 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.js'.
examples/ts_project_dep/b/b.ts(1,19): error TS6059: File 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/a/a.ts' is not under 'rootDir' 'C:/_bzl/execroot/_main/bazel-out/x64_windows-fastbuild/bin/examples/ts_project_dep/b'. 'rootDir' is expected to contain all source files.
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.d.ts
  Default library for target 'es5'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.es5.d.ts
  Library referenced via 'es5' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.d.ts'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.dom.d.ts
  Library referenced via 'dom' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.d.ts'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.webworker.importscripts.d.ts
  Library referenced via 'webworker.importscripts' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.d.ts'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.scripthost.d.ts
  Library referenced via 'scripthost' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.d.ts'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.decorators.d.ts
  Library referenced via 'decorators' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.es5.d.ts'
../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.decorators.legacy.d.ts
  Library referenced via 'decorators.legacy' from file '../../x64_windows-opt-exec-ST-13d3ddad9198/bin/external/_main~ext~npm_typescript/node_modules/.aspect_rules_js/typescript@5.5.2/node_modules/typescript/lib/lib.es5.d.ts'
examples/ts_project_dep/a/a.ts
  Imported via '../a/a' from file 'examples/ts_project_dep/b/b.ts'
examples/ts_project_dep/b/b.ts
  Matched by default include pattern '**/*'
examples/ts_project_dep/b/ts.optionsvalid.d.ts
  Matched by default include pattern '**/*'

Any other information?

I'm using a user .bazelrc option to move the startup path close to root because of path length limits (but I reproduced with this disabled as well):

startup --output_base=C:/_bzl

I can workaround this by converting all ts_project rules that are dependencies into composite typescript projects, and then add references to the tsconfigs for dependent projects (similar to how it's described and performed in https://github.com/aspect-build/rules_ts/blob/main/examples/project_references/README.md), but it's clunky and requires a specialized tsconfig (either tsconfig.json or inline tsconfig in the target definition which is a little more ergonomic, especially if I have multiple ts_project targets per package

It feels like I have to be doing something wrong, especially since that README suggests it shouldn't be necessary, but maybe this is just a fundamental limitation in standalone/local mode?

@willjschmitt willjschmitt added the bug Something isn't working label Aug 1, 2024
@willjschmitt
Copy link
Author

Example patch that illustrates updating tsconfigs and target definitions to support a successful build of the example:

diff --git a/examples/ts_project_dep/a/BUILD.bazel b/examples/ts_project_dep/a/BUILD.bazel
index 1a9e604..0efef93 100644
--- a/examples/ts_project_dep/a/BUILD.bazel
+++ b/examples/ts_project_dep/a/BUILD.bazel
@@ -4,5 +4,7 @@ ts_project(
     name = "ts",
     srcs = ["a.ts"],
     declaration = True,
+    composite = True,
+    incremental = True,
     visibility = ["//visibility:public"],
 )
diff --git a/examples/ts_project_dep/a/tsconfig.json b/examples/ts_project_dep/a/tsconfig.json
index ced1d5e..c73fe33 100644
--- a/examples/ts_project_dep/a/tsconfig.json
+++ b/examples/ts_project_dep/a/tsconfig.json
@@ -1,6 +1,8 @@
 {
     "compilerOptions": {
-        "declaration": true
+        "declaration": true,
+        "composite": true,
+        "incremental": true
     },
     // Workaround https://github.com/microsoft/TypeScript/issues/59036
     "exclude": []
diff --git a/examples/ts_project_dep/b/tsconfig.json b/examples/ts_project_dep/b/tsconfig.json
index 6cc3296..eb7a86c 100644
--- a/examples/ts_project_dep/b/tsconfig.json
+++ b/examples/ts_project_dep/b/tsconfig.json
@@ -1,4 +1,7 @@
 {
     // Workaround https://github.com/microsoft/TypeScript/issues/59036
-    "exclude": []
+    "exclude": [],
+    "references": [
+        { "path": "../a" }
+    ],
 }

@willjschmitt
Copy link
Author

Referencing also the unresolved (but stale/closed) issue for sandbox support on windows: bazelbuild/bazel#5136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant