Add check for ./node_modules/typescript when tsc is run #5157 by carlosdubus · Pull Request #5465 · microsoft/TypeScript · GitHub
Skip to content

Add check for ./node_modules/typescript when tsc is run #5157#5465

Closed
carlosdubus wants to merge 2 commits into
microsoft:masterfrom
carlosdubus:master
Closed

Add check for ./node_modules/typescript when tsc is run #5157#5465
carlosdubus wants to merge 2 commits into
microsoft:masterfrom
carlosdubus:master

Conversation

@carlosdubus

Copy link
Copy Markdown

@alexeagle Hello, I would like to work on this issue. I'm new to Typescript codebase and I'm not sure where to add a test for this. I changed the bin/tsc file directly so it doesn't load ../lib/tsc if is not needed. Would you rather have src/compiler/tsc.ts modified?

Let me know what do you think,
Thanks

@msftclas

Copy link
Copy Markdown

@msftclas

Copy link
Copy Markdown

@carlosdubus, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

@DickvdBrink

Copy link
Copy Markdown
Contributor

Hi @carlosdubus, you added resolve as a devDependency, I think it should be a normal dependency in this case as it required runtime and not only for development

@alexeagle

Copy link
Copy Markdown
Contributor

Have a look at angular/clang-format@2f9ceb9
where @mprobst added similar support to clang-format.

Comment thread bin/tsc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mprobst in clang-format you use the last arg as a hint where the node_modules directory can be located, then fallback to process.cwd(). I suppose this allows you to run from anywhere, do you think users do that?

There may be a different problem here - the -p flag. If the user is in their home directory, I think they could run tsc -p path/to/my/project - so maybe we should use the value of -p as a hint?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that commonly happens when you e.g. have an editor integration, and the editor effectively runs /usr/bin/clang-format some/path/to/my/file.ts. I'm not sure how much that applies to TypeScript.

I think -p is a very good signal, but of course at some point you end up having to parse all of tsc's args, which gets nasty. Some sort of a simple heuristic like -p or --project followed by a file name could work, though. If you want to be correct, you could use TypeScript's ts.parseCommandLine API, which should give a precise result.

@mhegazy

mhegazy commented Oct 30, 2015

Copy link
Copy Markdown
Contributor

//cc: @billti

Playing with this a bit, I am not sure we would want to take this change. with node v5 now flattening modules is the default, so if you are in a folder, and transitively, one of your imports has a dependency on TypeScript, it will be in ./node_modules/typescript and that can be confusing. at least now it is consistent.

@mhegazy mhegazy closed this Oct 30, 2015
@alexeagle

Copy link
Copy Markdown
Contributor

What about using the content of package.json (either parsing it or getting
from some npm command) to determine if typescript is an explicit dependency
of the project?

Note the value here is if someone on the team updates TypeScript, then some
time later, other people on the team get broken by the first new language
feature added. Days of "hey does someone remember how to fix the build
setup" ensue.

On Fri, Oct 30, 2015 at 4:00 PM Mohamed Hegazy notifications@github.com
wrote:

Closed #5465 #5465.


Reply to this email directly or view it on GitHub
#5465 (comment).

@DanielRosenwasser

Copy link
Copy Markdown
Member

@alexeagle I definitely see the value of it, but is that a common thing that other node projects do as well?

@alexeagle

Copy link
Copy Markdown
Contributor

gulp warns if you have a different version installed locally than global
one you're running.
clang-format has been very beneficial with this feature, before that the
team was very frequently running an out-of-date globally installed version.
It would be interesting to scan other packages on npm to see if they have
this behavior...

On Sun, Nov 1, 2015 at 12:53 AM Daniel Rosenwasser notifications@github.com
wrote:

@alexeagle https://github.com/alexeagle I definitely see the value of
it, but is that a common thing that other node projects do as well?


Reply to this email directly or view it on GitHub
#5465 (comment)
.

@billti

billti commented Nov 2, 2015

Copy link
Copy Markdown
Member

Another challenge is that on Windows running tsc will often run our tsc.exe SDK compiler, which uses Chakra and not Node, so we can't have a solution (at least not a cross platform, consistent one) that depends on loading Node / NPM modules.

Would an option be to use an NPM script ( https://docs.npmjs.com/cli/run-script )? This always looks in the ./node_modules/.bin directory first, so if a specific version of TypeScript is installed as a dependency, it will run that version.

For example, if your package.json scripts command had an entry:

"scripts": {
        "tsc": "tsc"
    }

Then running npm run tsc would run the dependent version installed, not the global version.

You can also pass args to it (e.g. run npm run tsc -- --version to verify the right version is being run), or just have the script automatically build and watch the project (e.g. the scripts entry could be: "tscw": "tsc -w -p ./", then just running npm run tscw would automatically build and watch the tsconfig.json in the root folder, using the TypeScript compiler installed as a dependency).

@DanielRosenwasser

Copy link
Copy Markdown
Member

I'm actually a big fan of warning the user that they are using a different version than is installed locally. I'll open an issue for that.

@alexeagle

Copy link
Copy Markdown
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants