tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
341 stars 104 forks source link

How to run 'tree-sitter parse'? #160

Closed mjambon closed 3 years ago

mjambon commented 3 years ago

I used to do this:

npm install
cd typescript  # or tsx
# ... make some changes to the grammar ...
npx tree-sitter generate
npx tree-sitter parse <(echo 'hello();')

But I'm no longer able to run tree-sitter directly from the command line. I can do this and it works fine:

npm install
# ... make some changes to the grammar ...
npm run build
npm run test

But I don't know what to do to parse a snippet without adding to the test corpus. npx, whose job is to locate executables installed locally, can't find tree-sitter when running from a subfolder. Here's what I do:

~/tree-sitter-typescript $ git clean -dfx
~/tree-sitter-typescript $ npm install
~/tree-sitter-typescript $ npm build  # I do that after changing the grammar
~/tree-sitter-typescript $ cd typescript
~/tree-sitter-typescript/typescript $ npx tree-sitter --help
command not found: tree-sitter

(I'm using npm version 7.5.3 and npx version 10.2.2).

Calling the tree-sitter installed under <root>/node_modules/.bin/ appears to work, but it's cumbersome.

~/tree-sitter-typescript/typescript $ ../node_modules/.bin/tree-sitter parse <(echo 'hello();')
(program [0, 0] - [1, 0]
  (expression_statement [0, 0] - [0, 8]
    (call_expression [0, 0] - [0, 7]
      function: (identifier [0, 0] - [0, 5])
      arguments: (arguments [0, 5] - [0, 7]))))

Is there a convenient way to use the project-local tree-sitter command or is it more practical to use a globally-installed tree-sitter?

patrickt commented 3 years ago

@mjambon Since the tree-sitter executable itself is now a binary (rather than something that depends on a JS env), I can’t see a downside to installing one globally, as it’s easy to upgrade (or if you're building it locally) with a cp into /usr/local/bin. I don’t think we can provide a more convenient way than that, though you could always install a shell alias mapping to ../node_modules/.bin/tree-sitter. I’m going to close this, but feel free to reopen it or just leave a commentif you have further questions.

dcreager commented 3 years ago

Echoing @patrickt, if you're not making changes to the CLI itself, you could definitely install it globally. Depending on your platform, the CLI might also be available in your system package manager. (I'm on Arch Linux, for instance, and I typically use sudo pacman -S tree-sitter to get a stock install of the CLI and runtime library.)

That said, npx should still work, I think... I was just able to use it in a fresh checkout of dcreager/tree-sitter-test:

$ npm --version
7.11.1
$ npx --version
7.11.1
$ git clone https://github.com/dcreager/tree-sitter-test
$ npm install
$ ls -l node_modules/.bin
total 0
lrwxrwxrwx 1 dcreager dcreager 25 Apr 26 13:45 tree-sitter -> ../tree-sitter-cli/cli.js*
$ npx tree-sitter --version
tree-sitter 0.19.4 (6dd41e2e45f8b4a00fda21f28bc0ebc6b172ffed)
mjambon commented 3 years ago

Thanks @patrickt and @dcreager! I'll use a global install.