Skip to content

Fix TypeScript with node shebang detected as JavaScript#7919

Open
mondeja wants to merge 4 commits intogithub-linguist:mainfrom
mondeja:fix-ts-file-with-node-shebang
Open

Fix TypeScript with node shebang detected as JavaScript#7919
mondeja wants to merge 4 commits intogithub-linguist:mainfrom
mondeja:fix-ts-file-with-node-shebang

Conversation

@mondeja
Copy link
Copy Markdown

@mondeja mondeja commented Apr 15, 2026

Fixes #7348

Description

TypeScript files with .ts extension which include "#!/usr/bin/env node or "#!/usr/bin/env node shebangs are misclassified as JavaScript files.

Checklist:

  • I am adding a new extension to a language.

  • I am adding a new language.

    • The extension of the new language is used in hundreds of repositories on GitHub.com.
    • I have included a real-world usage sample for all extensions added in this PR:
      • Sample source(s):
        • [URL to each sample source, if applicable]
      • Sample license(s):
    • I have included a syntax highlighting grammar: [URL to grammar repo]
    • I have added a color
      • Hex value: #RRGGBB
      • Rationale:
    • I have updated the heuristics to distinguish my language from others using the same extension.
  • I am fixing a misclassified language

    • I have included a new sample for the misclassified language:
      • Sample source(s):
      • Sample license(s):
    • I have included a change to the heuristics to distinguish my language from others using the same extension.
  • I am changing the source of a syntax highlighting grammar

    • Old: [URL to grammar repo]
    • New: [URL to grammar repo]
  • I am updating a grammar submodule

  • I am adding new or changing current functionality

    • I have added or updated the tests for the new or changed functionality.
  • I am changing the color associated with a language

    • I have obtained agreement from the wider language community on this color change.
      • [URL to public discussion]
      • [Optional: URL to official branding guidelines for the language]

@mondeja mondeja requested a review from a team as a code owner April 15, 2026 17:45
Copy link
Copy Markdown
Member

@lildude lildude left a comment

Choose a reason for hiding this comment

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

Please update the PR body to use the "I am adding a new extension to a language." part of the template and fill it in as if you're adding an extension.

The important part is we need real-world samples with their original filenames, and a link to the original source location and the license for legal reasons.

Please also update the test_find_by_interpreter test like in #7878 (the test wing pass until that PR is merged).

@Alhadis
Copy link
Copy Markdown
Collaborator

Alhadis commented Apr 15, 2026

Since TypeScript is a clean superset of JavaScript, how will you identify a Node executable that ends in .ts but is syntactically valid JavaScript and TypeScript? Does Node.js actually support native runtime execution of TypeScript the way Deno does?

This is a can-of-worms we've dealt with before at Linguist…

@mondeja
Copy link
Copy Markdown
Author

mondeja commented Apr 15, 2026

Since TypeScript is a clean superset of JavaScript, how will you identify a Node executable that ends in .ts but is syntactically valid JavaScript and TypeScript? Does Node.js actually support native runtime execution of TypeScript the way Deno does?

This is a can-of-worms we've dealt with before at Linguist…

You can differentiate them by the extension of the file. If it's JavasScript you can put a .js extension to the file, but if the file becomes TypeScript you can rename to use a .ts extension.

@Alhadis
Copy link
Copy Markdown
Collaborator

Alhadis commented Apr 15, 2026

@mondeja And what about Node.js executables that don't have any file extensions?

@mondeja mondeja marked this pull request as draft April 15, 2026 19:31
@mondeja
Copy link
Copy Markdown
Author

mondeja commented Apr 15, 2026

@mondeja And what about Node.js executables that don't have any file extensions?

For a file without an extension containing the #!/usr/bin/env node shebang, the result would be ambiguous (JavaScript + TypeScript), and subsequent strategies (heuristics, classifier) ​​would have to resolve it. Since TypeScript is a superset of JavaScript, a file without type annotations would likely be classified as JavaScript.

@Alhadis
Copy link
Copy Markdown
Collaborator

Alhadis commented Apr 15, 2026

… and subsequent strategies (heuristics, classifier) ​​would have to resolve it.

This is the very issue that's plagued Linguist's past attempts at getting Node.js executables containing TypeScript to be recognised as the latter. Both JavaScript and TypeScript's syntaxes are very similar, and heuristics that match dead-giveaways (like declare type foo =) aren't going to match every ambiguous file, so the most common outcome is a repository that's partially classified as TypeScript, and partially classified as JavaScript. Usually incorrectly.

Also, does node even support running TypeScript executables?

@mondeja
Copy link
Copy Markdown
Author

mondeja commented Apr 16, 2026

Also, does node even support running TypeScript executables?

Yes. Starting with Node.js 23.6 ts files can be executed running node file.ts.

This is the very issue that's plagued Linguist's past attempts at getting Node.js executables containing TypeScript to be recognised as the latter. Both JavaScript and TypeScript's syntaxes are very similar, and heuristics that match dead-giveaways (like declare type foo =) aren't going to match every ambiguous file, so the most common outcome is a repository that's partially classified as TypeScript, and partially classified as JavaScript. Usually incorrectly.

If there are bugs in linguist's classification logic we can fix them, of course. But that is out of the scope of this PR.

@mondeja mondeja requested a review from lildude April 18, 2026 06:56
@mondeja mondeja marked this pull request as ready for review April 18, 2026 06:56
Comment thread lib/linguist/languages.yml Outdated
@mondeja mondeja requested a review from lildude April 18, 2026 12:21
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

Successfully merging this pull request may close these issues.

Consider supporting .ts files with node shebang

3 participants