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

No output with --out when using import statement #1544

Closed
felixSchl opened this issue Dec 20, 2014 · 24 comments
Closed

No output with --out when using import statement #1544

felixSchl opened this issue Dec 20, 2014 · 24 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@felixSchl
Copy link

This cost me quite a bit of time, so please enlighten me if this is anticipated behaviour and let's put a big note in the docs.

tsc --module commonjs --out app.js app.ts type checks correctly, however produces no output. In fact it does not produce output with --module amd either.

tsc --module commonjs app.ts type checks correctly and produces output, but I do not have control over the location of the output.

For reference, I am developing a node app.

Let's say my app looks like this:

/// <reference path="./typings/node/node.d.ts"/>
/// <reference path="./typings/lodash/lodash.d.ts"/>
import _     = require("lodash");
var x = "test";

and I used tsc --module commonjs --out app.js app.ts to compile, I would get no output at all

To get my output back, I would either need to drop the import statement or drop the --out argument.

This is happening on version 1.3.0.0

@panost
Copy link

panost commented Dec 21, 2014

I already asked this here http://stackoverflow.com/questions/27547972/typescript-empty-output-file-when-compiling

If you have a top level "import" statement compiler goes to "external module" mode. In your case it correctly generates the app.js, but then it's been overriden an empty app.js. Since you have one single file as input, you can fix this by removing the "--out" switch

While I understand why this happens, I don't find it any helpfull, specially if you are creating a node app and not an external module.

One workaround is to use "var" instead of the "import" statement, but you will loose all VStudio intellisense

@felixSchl
Copy link
Author

One workaround is to use "var" instead of the "import" statement, but you will loose all VStudio intellisense

I don't mind that, but you also lose your type safety. The compiler doesn't associate the d.ts file with the require statement and hence you don't get type safety and intellisense.

If --module commonjs and --out are conceptually mutually exclusive, I would appreciate the compiler warning me. It is not obvious.

@danquirk danquirk added the Suggestion An idea for TypeScript label Jan 6, 2015
@danquirk
Copy link
Member

danquirk commented Jan 6, 2015

Seems reasonable to give some sort of warning/error here.

@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Jan 8, 2015
@RyanCavanaugh
Copy link
Member

I thought we had disallowed this in 1.0, but apparently that is not the case.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 8, 2015

--out will only apply to global code. Consider the case of a project with a loader and a set of modules. the loader.js is the only piece included in the html file, the other module files are imported dynamically by the loader. --out loader.js will build the non-external module pieces into one file, the other module files will be emitted to their own files.
This is a special case though, i am fine with a warning. what is the proposal?

@danquirk
Copy link
Member

danquirk commented Jan 8, 2015

In the case where --out is specified and the emitted file has no content emitted we should warn something to the effect of: "Warning: output file is empty." I'm not sure we want the message to say anything more speculative (about combining it with external modules), presumably anyone searching for this error will quickly find a thread here or on StackOverflow with an explanation.

@felixSchl
Copy link
Author

Warning: output file is empty.

That's something, but also pretty vague. I could just have compiled an empty file to start with. It's not giving me an indication as to why - I would still be baffled to find nothing in the output file after specifying --out. Maybe the problem is the implicit nature of what is considered "external" and "internal". The simple presence of a keyword changes my file from being one or the other. That had me really confused until I clicked on to it.

@danquirk
Copy link
Member

danquirk commented Jan 8, 2015

Yeah, that's a common source of confusion, but it's also not something that's really amenable to being quickly explained in a command line error message. An explicit warning at least gives you an immediate clue something is awry and some text to key your search for answers off of. Open to suggestions for alternatives though.

@felixSchl
Copy link
Author

Maybe it really just comes down to having better docs on the topic. I have read the official docs a couple of times to try to understand it, but it was a rather painful experience. The information is lacking and key points are not driven home. The section on modules starts of writing some module code, which by the looks of it would classify as an "internal" module - but really in my opinion it should explain the module infrastructure first - tell me what "internal" and "external" is supposed to be, because the words themselves don't do a good job at it. Then the next section patronises me and tells me the benefits of modules. Let alone the code used in the example is simply unconcise and distracting from the point.

I apologize for the rant, but the more I think about it, a simple warning message as suggested above, coupled with a solid documentation upgrade would be a worthwhile effort. I would be willing to create a pull request for it, if there's a repo for the docs.

@panost
Copy link

panost commented Jan 14, 2015

Given that one of TS goals, is the ease of development of large apps and large apps are always split into many files, then maybe when --out is specified the compiler does exactly what is told, Produce a single output file, merging all modules with the same name to one and raising errors when a conflict is found (ie private var with the same name). The current behaviour (produce a file for each input) then, can be easily achieved by calling the compiler without the --out flag or execute it multiple times.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Apr 27, 2015
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Apr 27, 2015
@RyanCavanaugh
Copy link
Member

Approved.

Spitballed solution in backlog meeting was to move the current behavior under an experimental flag (--globalOut?) and error if we try to emit an external module while --out was present.

We actually have no idea how many people are doing this on purpose but believe it to be extremely small. If somehow you find yourself looking at this issue and mix --out with external modules successfully, please post a comment!

@mhegazy
Copy link
Contributor

mhegazy commented Apr 28, 2015

If we do not allow --out with --module, then all non-module files (i.e. no top level import/export) should be coerced in a module output. see #2937 for more details.

@NickHeiner
Copy link

Hi – I just posted a dup of this (#3950) after losing a lot of time tracking it down. It looks like there are other people who have had similar issues. I would strongly 👍 throwing an error or at least logging a warning in this case. If you just log a warning, then at least people can search for that text string and find the docs you write about it, and start to understand what's going on.

@Petah
Copy link

Petah commented Aug 19, 2015

As a newbie to TS I found this very confusing....

@pinoyyid
Copy link

Just wanna add my +1 to this. Just cost me 2 hours that a warning would have saved. I really like to think of the compilers I use as being on my side, rather than providing a language aptitude test.

@amishshah
Copy link

Just want to +1 this, starting with TS and it's infuriating for this to happen. Not impressed.

@DanielRosenwasser DanielRosenwasser removed the Help Wanted You can do this label Sep 17, 2015
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 1.8 milestone Sep 17, 2015
@weswigham
Copy link
Member

With #5090 merged this is resolved. It is now an error to specify --out with --module unless --module is amd or system, at which point the modules will be concatenated into the specified output file.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 11, 2015
@jasonszhao
Copy link

When I use module: system with out: app.js, I get the output in the the cwd/the cwd, like ~/Repositories/app/Users/Jason/Repositores/app. See ivogabe/gulp-typescript#123

@h-nazzal
Copy link

@weswigham im still facing the same problem as the others
tsc main.ts --out main.js
`will still output an empty file with no warning what so ever...

@mhegazy
Copy link
Contributor

mhegazy commented Mar 11, 2016

@h-nazzal you need --module amd or --module system

achew22 added a commit to achew22/rules_typescript_deprecated that referenced this issue May 8, 2016
See microsoft/TypeScript#1544 for more
information on why --out was unacceptable with modules.
@GarryFlemings
Copy link

@RyanCavanaugh @DanielRosenwasser I'm unfamiliar with Git statuses. I see "Fixed" and "Closed". I see Milestone for TypeScript 1.8, which is out. I see "@weswigham was unassigned by @capfei on Jan 20". All that's consistent. On the other hand, I see indication above from @jasonszhao that the problem remains. And I see discussion today on https://gitter.im/Microsoft/TypeScript possibly indicating that @MeirionHughes is having the same problem. Should someone open another ticket to get this considered again? And cite this one?

@fcamblor
Copy link

FYI (and because I spent an hour on this as well), if you :

  • Rely on tsconfig.json for your compilation, use outFile and not define any module entry in the file
  • Use either grunt-ts or Intellij IDEA Compiler to compile typescript from the tsconfig

Then, your resulting app.js will be empty as well.

Workaround : put a module entry in your tsconfig.json file

Note: I only tested with grunt-ts and IntelliJ IDEA, but maybe other tool may be concerned by this problem as well.

@mhegazy
Copy link
Contributor

mhegazy commented May 31, 2016

@fcamblor this issue was reported in #8634 and you should now see an error if you do not have --module specified with --outFile. the fix is available in typescript@next.

@mhegazy
Copy link
Contributor

mhegazy commented May 31, 2016

And I see discussion today on https://gitter.im/Microsoft/TypeScript possibly indicating that @MeirionHughes is having the same problem. Should someone open another ticket to get this considered again? And cite this one?

@Prairie-Falcon this feature was implemented for TS 1.8, and should be available today. if you are running into issues, please file a new ticket, and provide enough information to allow us to diagnose the issue.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests