Typescript: tsc generating TS5055 error when generating declaration files

Created on 11 Dec 2015  Â·  19Comments  Â·  Source: microsoft/TypeScript

I'm consistently getting the following error when compiling any .ts file:
error TS5055: Cannot write file 'dist/[source-file-name].d.ts' because it would overwrite input file.

The compiler should be willing to overwrite the declarations files any time it recompiles. There are no permissions issues and this seems to be a new problem introduced recently since I moved from a stable ts1.7 release to using typescript@next releases (I'm currently on last night's - i.e. Version 1.8.0-dev.20151210).

I'm using tsc --watch with the following tsconfig.json file:

{
  "compilerOptions": {
    "target": "es6",
    "moduleResolution": "node",
    "module": "commonjs",
    "preserveConstEnums": true,
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "sourceMap": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "outDir": "./dist"
  },
  "exclude": ["node_modules", "jspm_packages"],
  "filesGlob": [
    "./src/**/*.ts"
  ]
}

(NOTE - the filesGlob above is not appearing correctly due to github reformatting..... it is actually:
./src/[asterix][asterix]/[asterix].ts
just in case you thought I have a bad tsconfig)

Thank you!

By Design

Most helpful comment

can you add ./dist to the exclude list?

All 19 comments

I can see a user getting extremely frustrated that we blindly overwrote a file that they won't be able to recover. I think the protection introduced is likely to outweigh the effort in workarounds. Any thoughts @mhegazy?

But I’m asking tsc to generate the file, and it’s a definition file, so it
should be free to do this. This has certainly been the case until
recently.

Thanks for your quick response,

PB

On Thu, Dec 10, 2015 at 4:56 PM, Daniel Rosenwasser <
[email protected]> wrote:

I can see a user getting extremely frustrated that we blindly overwrote a
file that they won't be able to recover. I think the protection introduced
is likely to outweigh the effort in workarounds. Any thoughts @mhegazy
https://github.com/mhegazy?

—
Reply to this email directly or view it on GitHub
https://github.com/Microsoft/TypeScript/issues/6046#issuecomment-163790152
.

can you add ./dist to the exclude list?

I seem to be missing something.......

The way I have my tsconfig set up, I'm asking tsc to "please generate
definition files for me".
It will generate one when there's not one already there, but then when I
change the .ts file it complains and says "I can't overwrite the one that I
just generated".

A similar argument could be made for the .js files that are emitted by the
compiler (i.e. Even though you changed the .ts file and invoked the
compiler, I can't write a new .js file because there is already one
there).......

..... Again, am I missing something?

On Thursday, December 10, 2015, Mohamed Hegazy [email protected]
wrote:

can you add ./dist to the exclude list?

—
Reply to this email directly or view it on GitHub
https://github.com/Microsoft/TypeScript/issues/6046#issuecomment-163798320
.

You are asking the compiler to overwrite an input file. Seeing as we are planning to support .js files as input files, yes, I would be inclined to say that we should not allow you to overwrite a .js input file.

After thinking about this a little more, it's probably a situation where you're damned if you do and damned if you don't (i.e. one could see it from either perspective).......

In any event, per @mhegazy's suggestion, simply 'excluding' the dist directory works in my case. I'm wondering if there will be knock-on effects if/when file globbing patterns are allowed and folks are doing 'in-place' compilation (i.e. same directory). We'll see.

Thanks for all your help.

No prob, thanks for filing the issue. It's always worth talking over.

Appreciate it - have a nice W/E

On Fri, Dec 11, 2015 at 11:45 AM, Daniel Rosenwasser <
[email protected]> wrote:

No prob, thanks for filing the issue. It's always worth talking over.

—
Reply to this email directly or view it on GitHub
https://github.com/Microsoft/TypeScript/issues/6046#issuecomment-164014740
.

Is there a reasonable workaround for people who need the declaration option to be compatible with in-place compiling? My workflow is to let Visual Studio handle my typescript build, and then use Visual Studio as my debugger. So it feels like in-place compiling is the most obvious way for this to work.

@mavericken i do not think this is related to the OP. please file a different issue and provide more information about what you are running into. --d is supported and it emits files next to your sources just like the .js and .map files by default.

While I appreciate the warning, I think we can improve the messaging. It's a bit of a red herring, much like this discussion. Here's what I found: I'm working in Windows, not Linux. Case Sensitivity isnt as much of a big deal here in Windows land. I made a change to the class because, at first, it was lower cased, but I preferred the class name starting with upper case. well the previous lower.d.ts was hanging around when i transpiled. This generated the error. The transpiler intended to replace a "Lower.d.ts", instead it found a "lower.d.ts". Once I deleted the "lower.d.ts", this error disappeared. If you could add this to the exception message, it might help some folks resolve this issue. Or better yet, make an exception that raises when such a case occurs. Thanks. Btw, I wouldnt touch javaScript with a ten-foot pole, but TypeScript has made front-end work less anxiety-ridden. Thanks, again!

error TS5055: Cannot write file 'dist/index.d.ts' because it would overwrite input file.
index.d.ts is not an input file - its a file I just asked to be created. When I compile a .ts file to javascript, I don't get an error about index.js already existing, so why the error for .d.ts?

@NickGrease the compiler thinks it is an input. can you run tsc --listFiles? do you see it there?
if not, then its a bug, please provide some additional information for us to be able to reproduce it. if you do see it, then one of your files import module "dist" or have a /// in short an input file is either a file that you pass on the command line, or that is a dependent of one of the files you pass on the command line transitively.

In my understanding "--watch" and "--declaration" in combination dosen´t make sense. TSC generates the .d.ts files (--declaration) and triggers right again because --watch watches the declaration-files too (they end on .ts) and fires because they were changed by itself, resulting in a compile loop.
This doesn´t make sense in my eyes. TSC should ignore ".d.ts"-files if the "--declaration" option is activated.

So in case of "--declaration" .d.ts-files are no input files, but output-files and should be treated as such.

Unfortunately one cannot exclude "*.d.ts" in the exclude-option. This would be a workaround but in my case it isn´t recognized.

Is there any way to just suppress this check? -- Like a command line argument to turn this off? -- It's clearly causing a lot of trouble.

In my understanding "--watch" and "--declaration" in combination dosen´t make sense. TSC generates the .d.ts files (--declaration) and triggers right again because --watch watches the declaration-files too (they end on .ts) and fires because they were changed by itself, resulting in a compile loop.

This is not accurate. the way this wold happen is as i noted erlier, if your .d.ts file is some how included in your project.

This doesn´t make sense in my eyes. TSC should ignore ".d.ts"-files if the "--declaration" option is activated.

tsc does ignore .d.ts files that it believes it has generated.

So in case of "--declaration" .d.ts-files are no input files, but output-files and should be treated as such.
Unfortunately one cannot exclude "*.d.ts" in the exclude-option. This would be a workaround but in my case it isn´t recognized.

I would be happy to look at your project if you have one to share.

Is there any way to just suppress this check? -- Like a command line argument to turn this off? -- It's clearly causing a lot of trouble.

The check tells you the compiler is about to override an input file. the compiler will not override your input file, cause this would be user data loss..
So either the file is in your input (use tsc --listFiles to know), or it is a bug. if it is the former please exclude the .d.ts file from your input, if it is the later, please file a new issue and share your project.

The directory which has the tsconfig.json file is considered as a root of the application. The "d.ts" files in the root directory or all of its sub directories would be automatically included. if you generate the "d.ts" files in any of the subdirectory of the root, you would see this error. if you generate the "d.ts" file to some other directory outside of the root directory, then you would be fine.

you can change the location of declartion tile by setting the "declarationDir" variable in the tsconfig.json

I got this error when by mistake I managed to import a file from my dist folder in a src file.

Was this page helpful?
0 / 5 - 0 ratings