grunt-ts icon indicating copy to clipboard operation
grunt-ts copied to clipboard

TypeScript needs a `baseDir` option

Open basarat opened this issue 10 years ago • 31 comments

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 avatar Mar 11 '14 12:03 basarat

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

basarat avatar Mar 11 '14 12:03 basarat

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.

basarat avatar Mar 15 '14 02:03 basarat

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.

jeffmay avatar Mar 15 '14 02:03 jeffmay

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 avatar Mar 27 '14 12:03 basarat

This is cool!

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

Bartvds avatar Mar 28 '14 23:03 Bartvds

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?

Bartvds avatar Mar 28 '14 23:03 Bartvds

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 avatar Mar 29 '14 01:03 basarat

Cool, all fine by me.

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

Bartvds avatar Mar 29 '14 02:03 Bartvds

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

basarat avatar Mar 29 '14 03:03 basarat

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?

marcuswhit avatar Apr 23 '14 16:04 marcuswhit

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

basarat avatar Apr 24 '14 01:04 basarat

@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

marcuswhit avatar Apr 24 '14 16:04 marcuswhit

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?

basarat avatar Apr 25 '14 00:04 basarat

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.

marcuswhit avatar Apr 25 '14 09:04 marcuswhit

Should fast compilation work?

yes.

I am assuming you've already see https://github.com/grunt-ts/grunt-ts/issues/96#issuecomment-38987023

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).

basarat avatar Apr 26 '14 00:04 basarat

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 avatar Aug 25 '14 15:08 mwisnicki

Seems like it works when I create empty.ts.

mwisnicki avatar Aug 25 '14 15:08 mwisnicki

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...

iislucas avatar Aug 29 '14 03:08 iislucas

@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'],

basarat avatar Aug 29 '14 05:08 basarat

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

mwisnicki avatar Aug 29 '14 08:08 mwisnicki

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)?

zpdDG4gta8XKpMCd avatar Oct 26 '14 13:10 zpdDG4gta8XKpMCd

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

basarat avatar Oct 26 '14 20:10 basarat

@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?

iislucas avatar Jan 20 '15 21:01 iislucas

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

basarat avatar Jan 20 '15 23:01 basarat

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 avatar Jan 21 '15 00:01 iislucas

Actually, that doesn't work for me, issue filed here: https://github.com/TypeStrong/grunt-ts/issues/201

iislucas avatar Jan 21 '15 00:01 iislucas

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

JoshuaKGoldberg avatar May 08 '16 20:05 JoshuaKGoldberg

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.

nycdotnet avatar May 10 '16 12:05 nycdotnet

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...

nstCactus avatar Jun 25 '16 09:06 nstCactus

Is there anything happening on the issue?

stephanfriedrich avatar Dec 01 '17 10:12 stephanfriedrich