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 build not working as expected with symlinks. #9552

Closed
micah-williamson opened this issue Jul 7, 2016 · 15 comments · Fixed by #12020
Closed

Typescript build not working as expected with symlinks. #9552

micah-williamson opened this issue Jul 7, 2016 · 15 comments · Fixed by #12020
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@micah-williamson
Copy link

TypeScript Version: 2.0.0-dev.20160630

Code

When I use a symlink in Windows I'm getting different build results. I haven't tested this with Mac/Linux yet.

dev
  app
    node_modules
      test
        foo.ts
    index.ts

Builds to:

dev
  app
    bld
      node_modules
        test
          foo.js
      index.js
    ...

Using symlink

mklink /D "c:\dev\app\node_modules\test\" "c:\dev\test\"
dev
  test
    foo.ts
  app
    node_modules
      test => /dev/test
    index.ts

Builds to:

dev
  app
    bld
      app   
        index.js
      test
        foo.js
    ...

Expected behavior:

Building with and without a symlink should build the same.

Actual behavior:

Building without a symlink behaves normally. Building with a symlink does not treat the directory as a link but uses the actual location of the linked directory. This causes problems with module resolution.

@micah-williamson
Copy link
Author

This appears to be a problem on Mac/Linux. Symlinked node_modules do not build the same way installed node_modules do. This was not a problem in ts 1.8.x.

This comes up for me because I am working on three libraries where two depend on the other. So I have library A, that B and C depend on. For development harmony, I use symlinks to work on them all at the same time.

The alternatives would be-

  1. To regularly push a changes to library A for B to npm update
  2. To regularly copy the contents of A to B/node_modules/A and C/node_modules/A
  3. To clone A to B/node_modules/A and regularly copy it's contents to C/node_modules/A (or vice versa)

So i'd much prefer symlinks if possible.

@micah-williamson micah-williamson changed the title Build is incorrect with symlinks are used on Windows (not yet tested on Mac/Linux) Typescript build not working as expected with symlinks. Jul 8, 2016
@DanielRosenwasser DanielRosenwasser added Help Wanted You can do this Bug A bug in TypeScript and removed Help Wanted You can do this labels Jul 18, 2016
@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Jul 19, 2016
@mhegazy mhegazy assigned ghost Jul 19, 2016
@opsxcq
Copy link

opsxcq commented Aug 11, 2016

+1

@mhegazy mhegazy modified the milestones: TypeScript 2.1, TypeScript 2.0.1 Aug 16, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Aug 16, 2016

The problem here is that the compiler will follow an import to test then resolve its realpath (a change made in TS 2.0 to support npm link scenarios) to /dev/test/foo.ts, and thus your program really has two files, /dev/app/index.ts and /dev/test/foo.ts, and there is no way to tell the compiler not to build /dev/test/foo.ts, as all source files (i.e. .ts files) are required to generate output.

An alternative is to use intermediate .d.ts files for your projects. i.e. have /dev/test/foo.d.ts instead of /dev/test/foo.ts. .d.ts files do not generate outputs, and will not participate in building your output folder structure. this means that when building your /dev/test you need to generate a declaration file (i.e. use --declaration).

Ultimately the fix would be the compiler to understand symlinks for all files, and unify files that come from the same real path. this is not a simple change, and would require some major changes to how programs are created and used.

@asibross
Copy link

I am experiencing the same issue. Looking forward to a fix.

@tekix
Copy link

tekix commented Oct 10, 2016

have similar issue when using npm link, already tried setting outDir and exclude in the tsconfig.json

@ghost
Copy link

ghost commented Nov 1, 2016

Here's what I tried to reproduce this:

mkdir test; cd test
echo 'export const x = 0;' > foo.ts
echo '{ "name": "test" }' > package.json

cd ..; mkdir app; cd app
npm link ../test
echo 'import { x } from "test/foo"; x + 1;' > index.ts
echo '{ "compilerOptions": { "outDir": "bld" }, "exclude": [] }' > tsconfig.json
tsc

This writes to bld/app/node_modules/test/foo.js and bld/app/index.js. (I made an issue regarding the extra app wrapper directory: #11977.) That's the same output I get if I remove the symlink and copy-paste test into node_modules.

Could you provide exact instructions to reproduce your problem?

As an aside, trying to compile the output of node_modules (by overriding the default "exclude") is a bad idea anyway; imagine the compile times if you had 100 imported libraries in there.

@jmc420
Copy link

jmc420 commented Nov 3, 2016

I am using typescript 2.0.6 and having the same issues with symbolic links.

I have put together a simple example that demonstrates the problem

https://github.com/jmc420/typescript_examples/tree/master/symlink_problem

The README explains the issue. The question is whether it is possible to get the compiler
to generate the same output regardless of whether a typescript file is a symbolic link or not.

@ep-mark
Copy link

ep-mark commented Nov 3, 2016

Have you tried changing the module resolution in your tsconfig to "classic"?

https://www.typescriptlang.org/docs/handbook/module-resolution.html

@jmc420
Copy link

jmc420 commented Nov 4, 2016

Thanks. Using "classic" module resolution is the answer.

@jmc420
Copy link

jmc420 commented Nov 4, 2016

classic module resolution generates the code in the way that I want but using "classic" means that typings in the node_modules directory are not found. Is it possible to use classic module resolution and find the typings in the node_modules directory?

@ghost
Copy link

ghost commented Nov 11, 2016

Fixed by #12020

@ghost ghost closed this as completed Nov 11, 2016
@ert78gb
Copy link

ert78gb commented Nov 13, 2016

I think it's not working in 2.2.0-dev.20161113 version. Which version will include this functionality?
I use node 7.0.0 on MacOs Sierra

The code works fine when I don't use npm link library
Error:(25, 10) TS2345:Argument of type '(group: AbstractControl) => { [key: string]: any; }' is not assignable to parameter of type 'ValidatorFn'.
Types of parameters 'group' and 'c' are incompatible.
Type 'AbstractControl' is not assignable to type 'AbstractControl'. Two different types with this name exist, but they are unrelated.
Types of property 'validator' are incompatible.
Type 'ValidatorFn' is not assignable to type 'ValidatorFn'. Two different types with this name exist, but they are unrelated.
Types of parameters 'c' and 'c' are incompatible.
Type 'AbstractControl' is not assignable to type 'AbstractControl'. Two different types with this name exist, but they are unrelated.
Types of property 'validator' are incompatible.
Type 'ValidatorFn' is not assignable to type 'ValidatorFn'. Two different types with this name exist, but they are unrelated.
Types of parameters 'c' and 'c' are incompatible.
Type 'AbstractControl' is not assignable to type 'AbstractControl'. Two different types with this name exist, but they are unrelated.

@ghost
Copy link

ghost commented Nov 13, 2016

@ert78gb Please provide steps to reproduce your error.

@ert78gb
Copy link

ert78gb commented Nov 14, 2016

  • nvm use 7.0
  • create project A with npm init
  • npm link in project A folder
  • create project B, with angular-cli
  • npm link A in project B folder

If I use IntelliJ to auto build, or if I use ng serve, ng build I got the same error.

If I don't use linked library the builds work the app is running with the expected result

@ghost
Copy link

ghost commented Nov 14, 2016

That may be because of the TypeScript version used by IntelliJ / angular-cli. This fix is in typescript@next and will be added to the next 2.1 release, so it should work whenever your tools are updated to use that.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants