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

TypeScript needs a baseDir option #77

Open
basarat opened this issue Mar 11, 2014 · 31 comments
Open

TypeScript needs a baseDir option #77

basarat opened this issue Mar 11, 2014 · 31 comments

Comments

@basarat
Copy link
Member

basarat commented Mar 11, 2014

Say we have the JS structure:

ts
    a.ts
    foo
    |    b.ts
    |    c.ts
js
    a.js
    foo
    |    b.js
    |    c.js

If we compile with all three files with outDir set to js its all okay. But if we only compile b.ts or c.ts with outDir set to js then we would get:

js    
    |    b.js
    |    c.js

Workaround is to create a temporary file under ts called ignoreBaseDirFile.ts and pass it to the tsc compiler

@basarat
Copy link
Member Author

basarat commented Mar 11, 2014

Reported to TS team : https://typescript.codeplex.com/workitem/2297

@basarat
Copy link
Member Author

basarat commented Mar 15, 2014

Instead of having the user specify the baseDir manually we can simply expand src and find the most common dir and assume that as the baseDir when fast compiling.

@jeffmay
Copy link

jeffmay commented Mar 15, 2014

Love the idea! I think we should also have a way to override that for safety. In a large enough Org I can see people getting confused if the basedir changes on them.

@basarat
Copy link
Member Author

basarat commented Mar 27, 2014

In a large enough Org I can see people getting confused if the basedir changes on them.

v1.9.2 has baseDir determined if user hasn't specified.

@basarat basarat closed this as completed Mar 27, 2014
@Bartvds
Copy link
Member

Bartvds commented Mar 28, 2014

This is cool!

Maybe make it a short dot file name? Like.baseDir.ts as ignoreBaseDirFile.ts is so bulky and regular.

@Bartvds
Copy link
Member

Bartvds commented Mar 28, 2014

When I use this I end up with a ignoreBaseDirFile.js.map, ignoreBaseDirFile.js and ignoreBaseDirFile.d.ts in my output directory.

Maybe we can delete those after compilation?

@basarat
Copy link
Member Author

basarat commented Mar 29, 2014

Like.baseDir.ts as ignoreBaseDirFile.ts is so bulky and regular.

My bad. I'll do .baseDir.d.ts and see how that goes (or revert to .baseDir.ts) . I'll put it in a minor version update (since the user options don't change).


Another Idea :
A better solution would be to figure out the correct outDir option based on the changed files e.g in the given sample above it can be js/foo. But that involves more work than I want to do right now. I'd rather wait and see typescript have a native baseDir option

@basarat basarat reopened this Mar 29, 2014
@Bartvds
Copy link
Member

Bartvds commented Mar 29, 2014

Cool, all fine by me.

I'm not really using it explicitly (atm), just as-is with the fast option.

@basarat
Copy link
Member Author

basarat commented Mar 29, 2014

I'll do .baseDir.d.ts

a .d.ts does not play a role in determining the outDir inside the compiler (I sort of suspected that). Falling back to .baseDir.ts

Bartvds referenced this issue in DefinitelyTyped/definition-header Apr 6, 2014
@marcuswhit
Copy link

I'm a little confused. From the first post above, when I've set fast:always, I expected that your workaround would result in a JS folder tree that mirrors the TS tree (as it does when fast:never is set). But instead, I just get the following when I modify b.ts (newly generated files indicated):

ts
    a.ts
    foo
    |    .baseDir.ts [new]
    |    b.js [new]
    |    b.js.map [new]
    |    b.ts
    |    c.ts
js
    a.js
    foo
    |    b.js
    |    c.js

I was expecting the b.js file would be replaced in the JS file tree. What have I missed?

@basarat
Copy link
Member Author

basarat commented Apr 24, 2014

@marcuswhit baseDir (source) is only useful if an outDir (destination) is specified

@marcuswhit
Copy link

@basarat Ok, it seems with baseDir correctly set, the generated JS is placed in the correct target JS folder, but it's also created in the TS folder, as below. Given a full build doesn't create the extra js files in the TS folder, why does the fast compile do so?

ts
    .baseDir.ts [new]
    a.ts
    foo
    |    b.js [new]
    |    b.js.map [new]
    |    b.ts
    |    c.ts
js
    a.js
    foo
    |    b.js [new]
    |    b.js.map [new]
    |    c.js

@basarat
Copy link
Member Author

basarat commented Apr 25, 2014

why does the fast compile do so

It shouldn't. Are you sure its grunt-ts and not visualstudio / webstorm doing that? Can you share the sample?

@marcuswhit
Copy link

Ah yes, my dumb mistake. Seems VS is compiling on save, even though I've explicitly set the option not to.

Next question - I have a grunt-watch task which monitors my TS folder and then kicks off my regular ts:dev task. I'm not using your inbuilt watch option. Should fast compilation work?

  • When I set fast:'watch' it always clears the cache and recompiles everything.
  • When I set fast:'always', it does correctly compile just the changed file, but only works when the class has no dependencies. When a class has dependencies on other classes, it doesn't seem to know about previously compiled classes, so throws a bunch of errors complaining that the dependencies don't exist.

What have I missed?

Thanks.

@basarat
Copy link
Member Author

basarat commented Apr 26, 2014

Should fast compilation work?

yes.

I am assuming you've already see #96 (comment)

When I set fast:'watch' it always clears the cache and recompiles everything.

It should only do it the first time after you start grunt

When I set fast:'always', it does correctly compile just the changed file, but only works when the class has no dependencies

It only compiles the changed file and does no dependency tracking. This is a deliberate decision because if it did dependency tracking it seems very likely that it will end up compiling everything since it is after all one project (a tree with all files as leaves).

@mwisnicki
Copy link

Doesn't work for me. I have setup like above and grunt-ts ends up creating:

ts/foo/.baseDir.ts
js/.baseDir.ts
js/b.js

@mwisnicki
Copy link

Seems like it works when I create empty.ts.

@iislucas
Copy link

This isn't working for me either; and I haven't found a workaround :(

I think it may be because my original typescript code is in a symlinked directory... but don't have time to explore a lot more right now.

I have:

ldixon@l:~/code/uproxy/uproxy-lib$ ls -l build/typescript-src/
total 0
lrwxrwxrwx 1 ldixon eng 22 Aug 28 23:38 arraybuffers -> ../../src/arraybuffers
lrwxrwxrwx 1 ldixon eng 16 Aug 28 23:38 crypto -> ../../src/crypto
lrwxrwxrwx 1 ldixon eng 17 Aug 28 23:38 freedom -> ../../src/freedom
...

And every time I compile the code in array buffers with ts rule like this:

ts: {
  arraybuffers: {
      src: ['build/typescript-src/arraybuffers/**/*.ts',
            '!**/*.d.ts',
            '!**/.baseDir.ts'],
      outDir: 'build/',
      options: {
        compile: true,
        baseDir: 'build/typescript-src/',
        sourceRoot: 'build/typescript-src/',
        module: 'amd',
        comments: true,
        noImplicitAny: true
      }
  }  
}

Then I end up with:

-rw-r----- 1 ldixon eng 2938 Aug 28 23:55 accumulator.js
-rw-r----- 1 ldixon eng 1660 Aug 28 23:55 accumulator.js.map
-rw-r----- 1 ldixon eng 4773 Aug 28 23:55 arraybuffers.js
-rw-r----- 1 ldixon eng 4863 Aug 28 23:55 arraybuffers.js.map
-rw-r----- 1 ldixon eng 7814 Aug 28 23:55 arraybuffers.spec.js
-rw-r----- 1 ldixon eng 7733 Aug 28 23:55 arraybuffers.spec.js.map
drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 typescript-src

where I was expecting:

drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 arraybuffers
drwxr-x--- 2 ldixon eng 4096 Aug 28 23:55 typescript-src

Will revert back to grunt-typescript for now...

@basarat
Copy link
Member Author

basarat commented Aug 29, 2014

@iislucas It think its because you are excluding .baseDir.ts explicitly i.e. you have '!**/.baseDir.ts' :

      src: ['build/typescript-src/arraybuffers/**/*.ts',
            '!**/*.d.ts',
            '!**/.baseDir.ts'],

@mwisnicki
Copy link

I no longer have this problem, though I'm not sure what was wrong before.

@zpdDG4gta8XKpMCd
Copy link

Empty baseDir file is annoying. Is there a way to prevent baseDir from being created? If no, can you at least add the hidden file attribute to it so is not visible on the file system (NTFS, Windows)?

@basarat
Copy link
Member Author

basarat commented Oct 26, 2014

can you at least add the hidden file attribute to it so is not visible on the file system (NTFS, Windows)

you can open a new issue for this so that someone can tackle it

@iislucas
Copy link

@basarat thanks; that was the issue.

I still find this all a bit awkward... I think I have a suggestion for proposed behaviour that might help with this and a related issue:

I have my code in a directory called src. e.g. src/arraybuffers/accumulater.ts I want to compile it all into build to get output of build/arraybuffers/accumulater.js. In the shell, I can do this by a cd src && tsc */*.ts But in grunt-ts, there is no way to specify a cwd as one can for most other grunt rules. Adding a cwd parameter like the grunt-contrib-copy and others (see https://github.com/gruntjs/grunt-contrib-copy#usage-examples) would help give programatic control over where output files end up.

Make sense?

@basarat
Copy link
Member Author

basarat commented Jan 20, 2015

@iislucas you can specify baseDir in options. It is same as effectively cwd.

@iislucas
Copy link

Great! Probably good to add it to the README: I did a search there (in the readme), but the only occurrence of the string baseDir seemed to be unrelated, about the .baseDir.ts file

@iislucas
Copy link

Actually, that doesn't work for me, issue filed here: #201

@JoshuaKGoldberg
Copy link

I just got linked to here from a .basedir.ts file popping up during options: out builds. Is there anything happening on the issue?

@nycdotnet
Copy link
Contributor

I'm planning to eliminate it in a future patch if someone passes the new rootDir option to tsc. But it's a matter of getting to it.

@nstCactus
Copy link

This would be nice to get rid of this indeed as it introduced a bug.
When using the grunt-ts noLib: true parameter, and properly declaring a lib in my main ts file, I get the following output when running grunt ts -v:

Running "ts" task

Running "ts:app" (ts) task
Verifying property ts.app exists in config...OK
Files: src/app.ts -> .
Compiling...

Fast Compile >>src/app.ts

Using tsc v1.8.10
"/Projets/green-box-express/src/app.ts" "/Projets/green-box-express/src/.baseDir.ts" --removeComments --noLib --target ES6 --module commonjs --outDir .
error TS2318: Cannot find global type 'Array'.
error TS2318: Cannot find global type 'Boolean'.
error TS2318: Cannot find global type 'Function'.
error TS2318: Cannot find global type 'IArguments'.
error TS2318: Cannot find global type 'Iterable'.
error TS2318: Cannot find global type 'IterableIterator'.
error TS2318: Cannot find global type 'Iterator'.
error TS2318: Cannot find global type 'Number'.
error TS2318: Cannot find global type 'Object'.
error TS2318: Cannot find global type 'RegExp'.
error TS2318: Cannot find global type 'String'.
error TS2318: Cannot find global type 'Symbol'.
error TS2318: Cannot find global type 'TemplateStringsArray'.
error TS2468: Cannot find global value 'Symbol'.

14 non-emit-preventing type warnings
Error: tsc return code: 2
Warning: Task "ts:app" failed. Use --force to continue.

Aborted due to warnings.

And this is due to the file .baseDir.ts being passed to the compiler while not containing the library declaration.

To fix this one could add something like /// <reference path="../node_modules/typescript/lib/lib.es6.d.ts" /> to the .baseDir.ts file, but for a beginner in TS like me, it means wasting more than an hour debugging this issue...

Samisdat added a commit to Samisdat/ts-conway that referenced this issue Oct 24, 2016
OlegKi added a commit to free-jqgrid/jqGrid that referenced this issue Feb 13, 2017
@stephanfriedrich
Copy link

Is there anything happening on the issue?

@nycdotnet
Copy link
Contributor

No, but PRs graciously accepted.

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