-
Notifications
You must be signed in to change notification settings - Fork 522
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
ts_project importing json fails #2365
Comments
I wonder what's the
|
Yeah seems like our test has a nested directory and that's why it passes there |
@alexeagle FYI, I've tried having
The issue happens disregarding where |
I've also run into this. On v2 I was overloading the I think we need to add a boolean attr for |
As of TypeScript v2.9, `tsc` can read and extract types from `.json` files by using the `resolveJsonModule` option. https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#new---resolvejsonmodule The new behavior allows people to pass `resolve_json_module` to `ts_project` in order to expect `.json` files from `tsc`. This change was modeled after the previous `allow_js` work in bazel-contrib#2260. Fixes bazel-contrib#2365
I've taken a swing at adding support for this in #2384 |
I think that resolving this properly would be easier if TypeScript could be configured to emit declaration files for .json files. See: |
I've been digging into this more, I don't see a full solution at this time. Here's a WIP diff of what I was working on at #2384 (comment) if anyone wants to take a look. The test scenario I was trying to solve for is having 2 projects that import a .json file and use it for types. Something like this in project 1: import config from './config.json';
export interface Testing {
aNumber: typeof config['aNumber'];
someOtherNumber: typeof config['aNumber'];
}
export default function fn(): Testing {
return {
// @ts-expect-error
aNumber: config.aString,
// @ts-expect-error
someOtherNumber: 'hi',
};
} And in project 2: import { Testing } from '../project-1';
import config from '../project-1/config.json';
export default function fn(): Testing {
return {
// @ts-expect-error
aNumber: config.aString,
// @ts-expect-error
someOtherNumber: 'hi',
};
} I've worked out how to make the .json files are available to TypeScript for types, and ensure that the types flow across projects successfully. I was able to do this by making some changes to my PR above, like this one where the json files are declared and copied in: + json_srcs = []
+ if ctx.attr.resolve_json_module == True:
+ for src in ctx.files.srcs:
+ if src.path.endswith(".json"):
+ declared = ctx.actions.declare_file(src.short_path[len(ctx.label.package) + 1:]
+ ctx.actions.expand_template(
+ output = declared,
+ substitutions = {},
+ template = src,
+ )
+ json_srcs.append(declared)
+
+ # resolve_json_module is True, which means that TypeScript will read
+ # and extract types from .json files, but it will not emit declaration
+ # files for these. We need to ensure that these .json files are passed
+ # along in the DeclarationInfo provider.
+ typings_outputs = typings_outputs + json_srcs However, this does not allow the .json file to be imported across projects because it is outside of the rootDir. The main problem is that tsc does not emit .d.ts files for .json files, which means that when you import the .json file, tsc needs to be able to read the .json file and not the .d.ts file. And, Bazel needs the rootDir to be scoped to the project. So the import of the config.json file in a different project ends up failing because it is not included in the rootDir. |
As it stands now, this is broken if you have a Maybe something like: def _normalize_json_path(package, path):
if len(package) == 0:
return path
else:
return path[len(package) + 1:] And then call it like: - ctx.actions.declare_file(_join(ctx.attr.out_dir, src.short_path[len(ctx.label.package) + 1:]))
+ ctx.actions.declare_file(_join(ctx.attr.out_dir, _normalize_json_path(ctx.label.package, src.short_path))) |
Think this was resolved by #2602? |
@alexeagle : Any updates on this issue? I would like to move a typescript monorepo into bazel and this is blocking us. |
@b-ram can you give a fresh repro or file a new issue? From the thread above it seems like this is already fixed, at least for some cases. |
🐞 bug report
Affected Rule
ts_project()
Is this a regression?
Maybe it is, it seems to supposedly have test coverage at https://github.com/bazelbuild/rules_nodejs/tree/master/packages/typescript/test/ts_project/json
Description
Following an example from the tests inside
@bazel/typescript
pretty barebones to import a json file into ats_project()
fails.🔬 Minimal Reproduction
See the repo https://github.com/tomasdev/ts_project_json_test/tree/main -- it has the minimal setup required.
Basically
a.ts
withimport data from './bar.json'
🔥 Exception or Error
🌍 Your Environment
Operating System:
Output of
bazel version
:Rules_nodejs version:
(Please check that you have matching versions between WORKSPACE file and
@bazel/*
npm packages.)(just released :D)
The text was updated successfully, but these errors were encountered: