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

Allow scripts to be run without a file extension #175

Closed
engina opened this issue Jan 18, 2023 · 10 comments
Closed

Allow scripts to be run without a file extension #175

engina opened this issue Jan 18, 2023 · 10 comments
Labels

Comments

@engina
Copy link

engina commented Jan 18, 2023

Feature request

A command line parameter to tsx to force extension.

Why?

It's slower to type the script name with the extension, especially because . character is in an inconvenient place.

./script.mts

Or if it's in your path

script.mts

Alternatives

Enfore extension in the shebang

#!/usr/bin/env -S tsx --type=mts

Additional context

And when you add the script to the PATH it will be just another tool in your work environment.

@engina engina added enhancement New feature or request pending triage labels Jan 18, 2023
@privatenumber
Copy link
Owner

Sorry I don't understand the request.

Can you elaborate on your use-case?

@engina
Copy link
Author

engina commented Jan 22, 2023

What I meant was:

> cat test.mts 
#!/usr/bin/env tsx
console.log('hello');
> ./test.mts
hello
> cp test.mts test
> chmod u+x test
> ./test
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for /Users/engin/code/cliui/test
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at new NodeError (node:internal/errors:371:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:87:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:102:38)
    at defaultLoad (node:internal/modules/esm/load:21:14)
    at x (file:///Users/engin/.local/share/nvm/v16.14.0/lib/node_modules/tsx/node_modules/@esbuild-kit/esm-loader/dist/index.js:1:2902)
    at ESMLoader.load (node:internal/modules/esm/loader:359:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:280:58)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:297:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:261:34)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

@privatenumber
Copy link
Owner

Sorry, I'm not interested in this enhancement.

Explicit extensions are enforced by Node.js as a part of ESM.

You can file the request with Node.js or add your own custom Node.js loader though.

@tonyxiao
Copy link

#!/usr/bin/env -S tsx --type=mts

@engina is this actually working? Or is it your proposed solution? I'm getting an error bin/node: bad option: --type=mts when I add it.

@engina
Copy link
Author

engina commented Jan 31, 2023

#!/usr/bin/env -S tsx --type=mts

@engina is this actually working? Or is it your proposed solution? I'm getting an error bin/node: bad option: --type=mts when I add it.

It was my proposed solution. It won't work for you.

I ended up doing a Makefile and a shell alias.

# Makefile
dist/ecm.js: $(shell find apps/ecm/src/ -type f)
	esbuild apps/ecm/src/main.ts --platform=node --bundle --minify --outfile=dist/ecm.js
alias ecm='make -s dist/ecm.js; node dist/ecm.js

This compiles the ts command line utility if source codes are changed. If not , just runs the already compiled version.

Even in the "cold boot" where it compiles and runs the tool, it does it faster than tsx.

tsx apps/ecm/src/main.ts takes 1.44 secs.

Method Time (sec.)
tsx cold 2.76
tsx warm 1.44
makefile cold 0.8
makefile warm 0.3

@privatenumber
Copy link
Owner

Related: nodejs/node#34049

Hope they find a Node.js solution to this.

@ulken
Copy link

ulken commented Oct 25, 2023

Related: nodejs/node#34049

Hope they find a Node.js solution to this.

Are we there yet? nodejs/node#34049 (comment)

@privatenumber
Copy link
Owner

privatenumber commented Oct 25, 2023

Since it's implemented in Node.js, I imagine tsx would get it for free

Have you tried Node v21.1.0 or v21.0.0?

Feel free to open a new bug report if it doesn't

@ulken
Copy link

ulken commented Oct 26, 2023

I haven't. Missed the part about holding off on back-porting to 20.X.

I'll give it a try and report back.

@ulken
Copy link

ulken commented Oct 26, 2023

Sorry, I was a little quick reading the issue description here. My use case is not for scripts. It's for running AVA without transpiling TS to JS first. Works fine in 18, but not in 20 (new LTS). Oh, well, sorry for the interruption.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants