## 経緯 [[Deno]]のv2.6がリリースされた。 <div class="link-card-v2"> <div class="link-card-v2-site"> <span class="link-card-v2-site-name">Deno</span> </div> <div class="link-card-v2-title"> Deno 2.6: dx is the new npx | Deno </div> <div class="link-card-v2-content"> This release includes `dx` for running package binaries, more granular permissions, source phase imports, faster ... </div> <a href="https://deno.com/blog/v2.6"></a> </div> 気になるものだけ試してみる。 ### 環境 | 対象 | バージョン | | --------- | ------ | | [[macOS]] | 15.7.2 | | [[Deno]] | 2.6.0 | ## Run package binaries with `dx` [[npm]]における[[npx]]コマンド同等の、[[dx (Deno)|dx]] ([[deno x]]) コマンドが使えるようになった。[[dx (Deno)|dx]]コマンドはエイリアスインストールが必要。 ```console deno x --install-alias ``` グローバルにインストールされていないとこんな感じ。([[deno add]]していればローカルで実行される。) ```console $ dx prettier Install npm:prettier? [Y/n] ``` [[deno run]]との主な違いはデフォルトのpermissionが割当たるため、余計な確認が不要になること。[[deno run]]の場合は以下のようにpermissionがすべて確認される。 ```console $ deno run prettier ✅ Granted env access to "PRETTIER_EXPERIMENTAL_CLI". ✅ Granted sys access to "cpus". ✅ Granted env access to "TERM". ┏ ⚠️ Deno requests env access to "npm_package_name". ┠─ To see a stack trace for this prompt, set the DENO_TRACE_PERMISSIONS environmental variable. ┠─ Learn more at: https://docs.deno.com/go/--allow-env ┠─ Run again with --allow-env to bypass this prompt. ┗ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) > ``` なお、[[deno run]]は `prettier` 直接指定だと動作しない。(`deno run npm:prettier`が必要) ```console $ deno run prettier error: Module not found "file:///Users/tadashi-aikawa/tmp/deno-sandbox/prettier". ``` ## More granular permissions [[--ignore-read (Deno)|--ignore-read]] や [[--ignore-env (Deno)|--ignore-env]] で特定のファイルや環境変数の読み込みのみを拒否できるようになった。 [[--ignore-env (Deno)|--ignore-env]] の例を示す。 ```ts const _public = Deno.env.get("PUBLIC"); const _secret = Deno.env.get("SECRET"); console.log("Public:", _public); console.log("Secret:", _secret); ``` [[--allow-env (Deno)|--allow-env]]を使うとすべての環境変数が許可されるため、`SECRET` も読み込まれてしまう。 ```console $ deno run --allow-env main.ts Public: ぱぶりっく Secret: しーくれっと ``` 一方、[[--ignore-env (Deno)|--ignore-env]]を使うと `NotCapable` エラーによって終了してしまう。 ```console $ deno run --allow-env --deny-env=SECRET main.ts error: Uncaught (in promise) NotCapable: Requires env access to "SECRET", run again with the --allow-env flag const _secret = Deno.env.get("SECRET"); ^ at Object.getEnv [as get] (ext:deno_os/30_os.js:124:10) at file:///Users/tadashi-aikawa/tmp/deno-sandbox/main.ts:2:26 ``` [[--ignore-env (Deno)|--ignore-env]]を指定することで、`SECRET` のみ読み込みを無視しつつ、プログラムを継続できる。 ```consol $ deno run --allow-env --ignore-env=SECRET main.ts Public: ぱぶりっく Secret: undefined ``` 権限ブローカーについては一旦スルー。 ## Faster type checking with `tsgo` and language server improvements [[tsgo]]を使った型チェックができるようになった。 環境変数でもOKだが、[[--unstable-tsgo (Deno)|--unstable-tsgo]]フラグを使った方で確認。 [[🦉Silhouette Core]]で確認したところ、0.67秒 -> 0.05秒 と10倍以上速くなっている。 ```console $ time deno check -r Check .releaserc.mjs Check domain/entity/OnetimeTask.ts Check domain/entity/RepetitionTask.test.ts Check domain/entity/RepetitionTask.ts Check domain/vo/Repetition.test.ts Check domain/vo/Repetition.ts Check mod.ts Check tests/testUtil.ts Check util/collections.test.ts Check util/collections.ts Check util/errors.ts Check util/strings.test.ts Check util/strings.ts deno check -r 0.67s user 0.12s system 57% cpu 1.389 total ``` ```console $ deno check --unstable-tsgo time deno check -r --unstable-tsgo Check .releaserc.mjs Check domain/entity/OnetimeTask.ts Check domain/entity/RepetitionTask.test.ts Check domain/entity/RepetitionTask.ts Check domain/vo/Repetition.test.ts Check domain/vo/Repetition.ts Check mod.ts Check tests/testUtil.ts Check util/collections.test.ts Check util/collections.ts Check util/errors.ts Check util/strings.test.ts Check util/strings.ts deno check -r --unstable-tsgo 0.05s user 0.09s system 13% cpu 1.070 total ``` また、[[LSP]]も[[source.organizeImports]]に対応するようになった。保存時の自動実行は他と競合して色々面倒だったので、回避策として明示的なkeymapを設定した。 ```lua vim.keymap.set("n", "<C-S-o>", function() vim.lsp.buf.code_action({ apply = true, context = { only = { "source.organizeImports" } } }) end, opts) ``` 基本的に[[Deno]]を使うときだけなので大きな問題はない。 ## Dependency management ### Granular control over `postinstall` scripts with `deno approve-scripts` [[postinstall]]のある[[パッケージ (npm)|パッケージ]]を[[deno install]]でインストールしようとすると、[[postinstall]]の内容は無視される。 ```warning ╭ Warning │ │ Ignored build scripts for packages: │ npm:[email protected] │ npm:[email protected] │ │ Run "deno approve-scripts" to run build scripts. ╰─ ``` [[deno approve-scripts]]を実行すると、許可した任意のパッケージだけ[[postinstall]]を実行できるようになった。 ```console $ deno approve-scripts Created deno.json configuration file. ? Select which packages to approve lifecycle scripts for (<space> to select, ↑/↓/j/k to navigate, a to select all, i to invert selection, enter to accept, <Ctrl-c> to cancel) ❯ ○ npm:[email protected] ○ npm:[email protected] ``` ここで選択しなかったものは `deno.json` で `"deny"` に放り込まれるので注意。(警告は出なくなる) ```json { "allowScripts": { "allow": ["npm:[email protected]"], "deny": ["npm:[email protected]"] } } ``` なお、v2.6以前の場合は以下のようなwarningが出ていた。[[--allow-scripts (Deno)|--allow-scripts]]で指定する方式。 ```warning ╭ Warning │ │ Ignored build scripts for packages: │ npm:[email protected] │ npm:[email protected] │ │ Run "deno install --allow-scripts=npm:unrs-resolver,npm:sharp" to run build scripts. ╰─ ``` ### Controlling dependency stability [[minimumDependencyAge (Deno)|minimumDependencyAge]]が追加された。 v2.5.5で対応されていた気がするけど、パッチバージョンだったからこのリリースノートに書いているだけと想定。 ## Node.js compatibility ### `@types/node` included by default [[@types.node|@types/node]]のインストールなしで、型が解決するようになった。 ```ts import { readFile } from "node:fs/promises"; const r = await readFile("./deno.json", "utf8"); console.log(r); ``` 型チェックの問題であり、上記のコードはv2.6より前のバージョンでも実行はできた。