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

Deno installs unused npm dependencies despite --entrypoint flag usage #27627

Open
M4RC3L05 opened this issue Jan 10, 2025 · 5 comments
Open

Deno installs unused npm dependencies despite --entrypoint flag usage #27627

M4RC3L05 opened this issue Jan 10, 2025 · 5 comments
Assignees
Labels

Comments

@M4RC3L05
Copy link
Contributor

Given this simple project:

main.ts:

import "@std/assert";
import "express";

console.log("oioioi");

deno.json:

{
  "tasks": {
    "dev": "deno run --watch main.ts"
  },
  "imports": {
    "@std/assert": "jsr:@std/assert@1",
    "dbmate": "npm:[email protected]",
    "express": "npm:[email protected]"
  }
}

Running:

deno clean && deno install --entrypoint main.ts && du -h --max-depth=0 ~/.cache/deno/

I get the following output:

Removed /home/main/.cache/deno (1004 files, 4.63MB)
38M     /home/main/.cache/deno/

If now i edit the main.ts to comment out the import "express" line, and rerun the above command:

deno clean && deno install --entrypoint main.ts && du -h --max-depth=0 ~/.cache/deno/

i get:

Removed ~/.cache/deno (241 files, 2MB)
2,7M    ~/.cache/deno/

Checking into the files, it appears that both dbmate and express are being installed even tho i do not use dbmate directly, and since i am running the install command with --entrypoint i would expect deno to only install express.

Is my assumption correct? i would expect deno to be smart about which deps are being used and only install does.

@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Jan 10, 2025

This is the deno cache folder with express commented out:
Screenshot From 2025-01-10 22-21-24

And this with express being imported:
Screenshot From 2025-01-10 22-21-55

@M4RC3L05 M4RC3L05 changed the title Add beaviour with deno install and npm dependencies Odd beaviour with deno install and npm dependencies Jan 10, 2025
@M4RC3L05 M4RC3L05 changed the title Odd beaviour with deno install and npm dependencies Odd behavior with deno install and npm dependencies Jan 10, 2025
@M4RC3L05 M4RC3L05 changed the title Odd behavior with deno install and npm dependencies Deno installs unused npm dependencies despite --entrypoint flag usage Jan 10, 2025
@dsherret dsherret added bug Something isn't working correctly install labels Jan 10, 2025
@nathanwhit
Copy link
Member

The behavior you want was implemented in #27300, gated behind an unstable flag.

It's technically a breaking change (it can break things in some edge cases), so we didn't enable it by default. (That PR description is out of date, I ended up changing it so --entrypoint behavior was unchanged by default as well).

You can opt in with the --unstable-npm-lazy-caching flag or

"unstable": ["npm-lazy-caching"]

in deno.json

@nathanwhit nathanwhit removed the bug Something isn't working correctly label Jan 10, 2025
@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Jan 11, 2025

@nathanwhit It did worked, but if i have this:

{
  "tasks": {
    "dev": "deno run --watch main.ts"
  },
  "lock": {
    "frozen": true
  },
  "unstable": ["npm-lazy-caching"],
  "imports": {
    "@std/assert": "jsr:@std/assert@1",
    "dbmate": "npm:[email protected]",
    "express": "npm:[email protected]"
  }
}

Runnin deno clean && deno install --entrypoint main.ts && du -h --max-depth=0 ~/.cache/deno/ dbmate gets installed. Looks like it does not place nice with frozen lock files? if i set it to false it works as expected, only used deps gets installed

@M4RC3L05
Copy link
Contributor Author

Actually after some tests it only works if you provide the flags on the command it self, setting it in the deno.json under the unstable property did not work.

@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Jan 11, 2025

Another update, with this deno.json and the same main.ts as above:

{
  "tasks": {
    "dev": "deno run --watch --cached-only main.ts"
  },
  "lock": {
    "frozen": false
  },
  "unstable": ["npm-lazy-caching"],
  "imports": {
    "@std/assert": "jsr:@std/assert@1",
    "dbmate": "npm:[email protected]",
    "express": "npm:[email protected]"
  }
}

Running:

deno clean && deno install --entrypoint main.ts && du -h --max-depth=0 ~/.cache/deno

I get:

Removed ~/.cache/deno (3415 files, 61.57MB)
38M     ~/.cache/deno

Which means the unstable prop did not work.

Running:

deno clean && deno install --entrypoint --unstable-npm-lazy-caching main.ts && du -h --max-depth=0 ~/.cache/deno

I get:

Removed ~/.cache/deno (1009 files, 34.22MB)
7,0M    ~/.cache/deno

If i followup this with the following command:

deno task dev

I get:

Download https://registry.npmjs.org/dbmate/-/dbmate-2.24.2.tgz
Download https://registry.npmjs.org/@dbmate/linux-x64/-/linux-x64-2.24.2.tgz
Task dev deno run --watch --cached-only main.ts
Watcher Process started.
Warning 'npm-lazy-caching' isn't a valid unstable feature
error: Uncaught (in promise) NotCapable: Requires read access to <CWD>, run again with the --allow-read flag
    at Process.cwd (ext:deno_fs/30_fs.js:152:10)
    at Object.<anonymous> (file:///home/main/.cache/deno/npm/registry.npmjs.org/depd/2.0.0/index.js:23:24)
    at Object.<anonymous> (file:///home/main/.cache/deno/npm/registry.npmjs.org/depd/2.0.0/index.js:540:4)
    at Module._compile (node:module:745:34)
    at loadMaybeCjs (node:module:770:10)
    at Object.Module._extensions..js (node:module:755:12)
    at Module.load (node:module:662:32)
    at Function.Module._load (node:module:534:12)
    at Module.require (node:module:681:19)
    at require (node:module:812:16)
Watcher Process failed. Restarting on file change...

Looks like deno task command just downloads everything?

If instead of running deno task i run the command directly:

deno run --watch --cached-only main.ts

I get an error and also a warning about the unstable config:

Watcher Process started.
Warning 'npm-lazy-caching' isn't a valid unstable feature
error: Failed caching npm package '[email protected]'
    0: npm package not found in cache: "dbmate", --cached-only is specified.
Watcher Process failed. Restarting on file change...

But if i add --unstable-npm-lazy-caching to it, it works:

deno run --watch --cached-only --unstable-npm-lazy-caching main.ts

output:

Watcher Process started.
Warning 'npm-lazy-caching' isn't a valid unstable feature
✅ Granted read access to <CWD>.
✅ Granted env access to "NO_DEPRECATION".
✅ Granted env access to "TRACE_DEPRECATION".
✅ Granted env access.
oioioi
Watcher Process finished. Restarting on file change...

Running deno run without the --cached-only just downloads the extra deps.

From this examples looks like the deno.json unstable prop does not seems to be picked up? also should deno task command fetch dependencies?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants