aspect-build / aspect-build/rules_ts

[Bug]: `ts_project`s fail on Windows (with standalone strategy) without converting all targets to (composite) project references

Open
#664 2 comments 0 reactions 0 assignees View on GitHub
bug need: funding need: investigation windows
Dominant language
Starlark
Stars
138
Forks
87
Avg merge
2d 8h
Merged PRs (30d)
3

Description

### 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

```shell
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?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failure with `bazel build //examples/ts_project_dep/b:ts --enable_runfiles` on Windows and inspect `examples/ts_project_dep/b/BUILD.bazel`, its `tsconfig.json`, and the dependency under `examples/ts_project_dep/a`. Compare this with `examples/project_references/README.md` and the `ts_project` rule behavior. Done means the dependency example builds on Windows without requiring every dependency to become a composite project.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.