-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
TS5055 error while import a non relative json module with resolveJsonModule option #24715
Comments
the compiler when resolving a .json file will read it and write it again. so you really want to have |
In this case the error pops out when rollup plugin calls I will do a workaround by providing a fake |
@mhegazy If there is no outDir specified, why should the .json file be duplicated? It's a static resource... is there a way to do this without needing outDir? |
@mhegazy, still not feel right: if import a native js module, it won't issue TS5055 error |
maybe add an compile option --allowJson(and default to false) like --allowJs for js file? |
cause the compiler does not overwrite .d.ts files. so no issue here.
if you use |
yes , import js module and set --allowJs to true would cause TS5055 too. if we know the imported js file do not need to be transformed(compiled), we would set allowJs to false, and i think that is the point to provide allowJs option. json file/module would never need to be transformed, so why not just make them "no emit" by default (and we don't lose anything)? |
well we do not rely know that. |
And this is why |
If the outDir is different, that makes sense. But if not, then why try to overwrite it? .json files are closer to being treated like static assets than javascript, so there isn't a 'build artefact' equivalent here. |
this issue should be solved since it makes sence to use "./" as outDir in some cases |
@mhegazy why is this issue still marked 'working as intended'? If this is intended to break all projects that don't specify outDir, then how does that make any sense? |
And can you answer why the compiler is attempting to write a static file over the top of itself? The correct solution here is to not attempt to overwrite a .json file is outDir is not specified, because the default location will be in the same directory. Seems like a pretty simple solution to me. |
My actual building of the code involves a special outDir, so thankfully this isn't interfering with my deployment. However this issue is breaking checkers which are just concerned with ensuring the code is all good and nothing more. fuse-box/fuse-box-typechecker#53 I have proposed a PR to the a particular checker that essentially ignores all TS5055 checks as a workaround. I don't think this is a good idea, it makes sense for TS to be smarter about this. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Can this be re-opened and the If it is really "working as intended", I think there should be some documentation that this feature only works in conjunction with the It almost seems like it is not intended to be used for common use-cases like: import { version } from './package.json' I guess it would be fine if you could somehow ignore |
Another common use-case. Let's say I have the following project structure:
If I specify
Even if that's "working as intended", I think that might surprise a lot of people. |
Just hit this as well. The fact that the |
we should be able to tell |
so what I did was compile to tsconfig.json:
package.json:
this way relative imports and express statics are preserved as well as all masks (from |
I was surprised to find this doesn't work already. I've had to revert to using When using |
Have you tried my method of outputting to a temporary folder and having npm copy contents of |
I'm sure that would work for the normal build case, but I don't think it'll work when tsc is in watch mode, which I use. Ultimately, |
Before I found this issue, I also left a comment on #24744 which is still open (I hadn't realized this issue is closed and marked fixed, so this discussion will likely go unnoticed). |
You could use |
Having to explicitly set one option like I thing that the options should be decoupled - we shouldn't have to mandatory set one option to have another in a working state. Not to mention that you can still set the outDir` to the very same folder and cause the very same bug to reappear (which is creating a third rule - do not set outDir as the project dir...) |
I'm using 'use strict';
import { readFileSync } from 'fs';
import { resolve } from 'path';
export default {
// ...
externals: Object.keys(JSON.parse(readFileSync(resolve('package.json')).toString()).dependencies),
// ...
}; |
This issue has any updates ? or workaround? |
this issue should not be closed as there is still people struggling with this. Is not fixed and while working with visual studio is very annoying |
If you follow this common use-case, then I have a workaround for importing I am able to convert this problematic statement: import pkg from '../package.json' into const pkg = require('../package.json')
One side-effect is you lose access to static type-checking as the compiler doesn't know where the dynamically imported module is from at compile-time. |
TypeScript Version: 3.0.0-dev.20180605
Search Terms: TS5055 resolveJsonModule
Code
tsjson> find .
.
./main.ts
./json
./json/myjson.json
./tsconfig.json
./src
./src/test.ts
tsconfig.json:
main.ts:
src/test.ts:
json/myjson.json:
Expected behavior:
compile successful
Actual behavior:
tsjson> tsc
error TS5055: Cannot write file '*************/tsjson/json/myjson.json' because it would overwrite input file.
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: