Skip to content

Wrong output file location #123

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

Closed
aradzie opened this issue May 15, 2015 · 5 comments
Closed

Wrong output file location #123

aradzie opened this issue May 15, 2015 · 5 comments

Comments

@aradzie
Copy link

aradzie commented May 15, 2015

This is strange. I am pulling may hair out, cannot figure out what is wrong with my gulpfile. It contains two identical TS compile tasks, one produces a correct JS file, another does not.

The tasks are defined as follows:

gulp.task("typescript:app1", function () {
    return gulp.src(["./src/ts/app1.ts"])
            .pipe(plugin.sourcemaps.init())
            .pipe(plugin.typescript({
                out: "app1.js",
                target: "es6",
                module: "commonjs"
            }))
            .pipe(plugin.sourcemaps.write(".", {sourceRoot: "."}))
            .pipe(gulp.dest("./build"));
});

The only difference between tasks is that one takes app1.ts and produces app1.js. Another takes app2.ts and produces app2.js.

The project directory layout is as follows:

/home/user/project/gulpfile.js
/home/user/project/src/ts/app1.ts
/home/user/project/src/ts/app2.ts
/home/user/project/build

The first task produces a correct JS file:

/home/user/project/build/app1.js
/home/user/project/build/app1.js.map

However, the second task produces the following files:

/home/user/project/build/app2.js  <-- this one is empty
/home/user/project/build/app2.js.map
/home/user/project/build/home/user/project/src/ts/app2.js <-- this is a correct output file in a wrong directory
/home/user/project/build/home/user/project/src/ts/app2.js.map

Please note that the wrong directory is concatenation of the output directory and the source directory paths.

The problem goes away if I remove module option from the second task. E.g., remove the following line:

module: "commonjs"
@aradzie aradzie changed the title Wrong output file location after compiling with gulp-typecript Wrong output file location May 15, 2015
@KoKuToru
Copy link

I am not using gulp-typescript, but I have the exact same problem with typescript (using broccoli)

I am using 1.5-alpha.
Using the out-option generates a empty files.
Using the outDir-option generates the js in the wrong directory.

I thought it was a process.cwd() problem in typescript.
But using process.chdir(???) changes nothing..

My typescript-options are the following:

var options = {
        noEmitOnError: true,
        noImplicitAny: false,
        target: ts.ScriptTarget.ES6,
        module: undefined,
        outDir: outputPath
    };

Just wanted to share my information... maybe it's a bug in typescript.

@aradzie
Copy link
Author

aradzie commented May 15, 2015

Well, indeed, it seems like a TypeScript compiler bug. I tried to compile the same files as above with the tsc command line tool with the same options.

./node_modules/typescript/bin/tsc --target ES6 --module commonjs ./src/ts/app1.ts --out /tmp/app1.js

It created two files. An empty file in a correct directory, and a correct file in a wrong directory.

/home/user/project/src/ts/app1.js <-- a correct output file in the same directory as the input ts file
/tmp/app1.js <-- this file is empty

However, running the same commands on another ts file produced correct results. The only difference between two ts files, is that one uses import directive, and the other one does not.

import app = require("app");

The way to fix the problem is to replace import with var. Can you believe it?

var app = require("app"); // Fixes the problem!

@aradzie aradzie closed this as completed May 15, 2015
@ivogabe
Copy link
Owner

ivogabe commented May 15, 2015

That's strange indeed. It looks like an issue with typescript, as you could reproduce it with tsc. Have you tried using TS1.5 beta?

@mhegazy @DanielRosenwasser Any idea what's going on?

@DanielRosenwasser
Copy link

Any idea what's going on?

Perhaps; @aradzie, can you try out TypeScript 1.5-beta as @ivogabe suggested and let us know what the result is? If you're still running into the problem, I'd create a new bug on the TypeScript GitHub repo and link back to this issue.

The way to fix the problem is to replace import with var. Can you believe it?

Yup, that's because when you use an import, we'll try to resolve it as a module, whereas with a var you'll just let node pick up the logic and type app as any.

@aradzie
Copy link
Author

aradzie commented May 15, 2015

Well, I tried it with the latest tsc from the master branch checked out a few minutes ago.

user@notebook:~/TypeScript$ git rev-parse HEAD
e3812ff8f24172d94e6d037ba5c3acc9de97826e
user@notebook:~/TypeScript$ node ./bin/tsc --version
message TS6029: Version 1.5.2

The result is the same, an empty file is generated. However, this might be as intended. I never used external modules in my TS projects before, so the result came as a surprise for me. From my prior experience with TS, I find this behavior counter-intuitive. Now I can't decide, if it's my lack of knowledge, or really a bug.

Here is my test set up

./src/main.ts

import foo = require("./module1");
import bar = require("./module2");

foo.foo();
bar.bar();

./src/module1.ts

export function foo() {
    console.log("foo");
}

./src/module2.ts

export function bar() {
    console.log("bar");
}

./build.sh

#!/usr/bin/env bash

node ~/TypeScript/bin/tsc \
    --target ES5 \
    --module commonjs \
    --out ./build/result.js \
    --outDir ./build \
    ./src/main.ts

And the result of compilation:

./build/main.js
./build/module1.js
./build/module2.js
./build/result.js <-- this file is empty

The TSC creates a separate JS file for every TS module, including main.ts, module1.ts and module2.ts. This makes sense, because any of these files can be imported into another TS file, and the main.ts file can be imported too. The result.js file, as specified on the command line to the TSC, is empty, because there is no corresponding module for it.

However, I would prefer another compilation model, where every TS module is compiled by a separate invocation to the TSC, and the --out option is honored.

Let me explain. The project I am working on does not define any amd/commonjs modules. It only generates a simple standalone script that requires already existing modules from nodejs. The modules that it requires in the TS source, are just ambient external module declarations, from module.d.ts files. If only the TSC had an option to stop producing modules, something like --generateModules=false, it would be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants