denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.3k stars 5.36k forks source link

Scripts that use npm packages do not work with Deno v1.46.2 on Windows #25333

Closed fuji44 closed 2 months ago

fuji44 commented 2 months ago

After upgrading to Deno 1.46.2 on Windows, when I try to run a new script that uses the npm package, I get the following error and cannot run it.

> deno run main.ts
error: Failed creating symlink in node_modules folder

I rewrote a very simple script and npm package to isolate the problem, but the result was the same.

import isEven from "npm:is-even";

console.log(isEven(2)); // true
console.log(isEven(3)); // false

Additionally, we tried clearing the cache and degrading to v1.46.1. The result is that the error occurs only with v1.46.2.

# in powershell
> deno clean
Removed C:\Users\fuji44\AppData\Local\deno (49 files, 313.49KB)
> deno --version
deno 1.46.2 (stable, release, x86_64-pc-windows-msvc)
v8 12.9.202.5-rusty
typescript 5.5.2
> deno run main.ts
error: Failed creating symlink in node_modules folder
> deno upgrade v1.46.1
Current Deno version: v1.46.2
Downloading https://github.com/denoland/deno/releases/download/v1.46.1/deno-x86_64-pc-windows-msvc.zip
Deno is upgrading to version 1.46.1

Upgraded successfully to Deno v1.46.1 (stable)
> deno clean
Removed C:\Users\fuji44\AppData\Local\deno (22 files, 286.82KB)
> deno --version
deno 1.46.1 (stable, release, x86_64-pc-windows-msvc)
v8 12.9.202.2-rusty
typescript 5.5.2
> deno run main.ts
true
false

By the way, there is no problem on Ubuntu 22.04 on WSL.

hermy991 commented 2 months ago

Workaround: Running Visual Studio Code or PowerShell as a administrator works for me, the deno team in version 1.46.2 use symlinks when node_modules is created.

fuji44 commented 2 months ago

It is certainly possible if you run it with administrator privileges. I could also do it by adding my own user to the local security policy SeCreateSymbolicLinkPrivilege. I have not tried this, but it also seems to be possible with developer mode enabled. For the time being, I will try to work around this by granting permission to create symbolic links only to my own users.

It makes sense to structure the package with symbolic links, and I personally support this. However, we also want to avoid compromising the development experience.

For my part, I think a good compromise would be to give users the authority to create symbolic links on the windows side, but I am not happy with the hassle involved. If this is going to be the default work going forward, it needs to be added to the installation docs.

fuji44 commented 2 months ago

I think the pnpm approach would be useful as a way to address this on the deno side.

https://pnpm.io/faq#does-it-work-on-windows

lucacasonato commented 2 months ago

Probably caused by #25164

lucacasonato commented 2 months ago

Can you try again in deno canary (deno upgrade canary) - I think the error will persist, but the message should be more useful. Could you send over that error message?

fuji44 commented 2 months ago

@lucacasonato That didn't strike me as a straightforward message about the cause.

> deno --version
deno 2.0.0-rc.0+42cf762 (canary, release, x86_64-pc-windows-msvc)
v8 12.9.202.5-rusty
typescript 5.5.2
> deno run main.ts
error: Could not find a matching package for 'npm:is-even' in a package.json file. You must specify this as a package.json dependency when the node_modules folder is not managed by Deno.
    at file:///C:/Users/fuji44/git/file-manager-x/main.ts:1:20
marvinhagemeister commented 2 months ago

Reading the last error message makes me wonder if it's related to https://github.com/denoland/deno/issues/25355 . Do you happen to use a workspaces for your project?

fuji44 commented 2 months ago

Workspace is not used. Additional experimentation has shown that the problem occurs depending on the presence or absence of package.json.

The experimental procedure is as follows. Add files to the new directory as follows and run main.ts at each step. I experimented with v1.46.2 and canary.

  1. main.ts only
  2. main.ts + deno.jsonc
  3. main.ts + deno.jsonc + package.json

The result is an error only in step 3 for both versions. However, the canary error message seems to be about something other than this issue, since only canary causes an error when step 3 is performed with permission to create a symbolic link.

By the way, I don't want to use package.json, but prisma cli creates it on its own, so I have no choice.

Details #### deno.jsonc ```jsonc { "lock": false, "imports": { "@prisma/client": "npm:@prisma/client@^5.19.0", "@std/assert": "jsr:@std/assert@^1.0.2", "@std/fs": "jsr:@std/fs@^1.0.1", "@std/path": "jsr:@std/path@^1.0.2", "@std/yaml": "jsr:@std/yaml@^1.0.3", "ajv": "npm:ajv@^8.17.1", "ajv-formats": "npm:ajv-formats@^3.0.1", "hono": "npm:hono@^4.5.3", "json-schema-to-typescript": "npm:json-schema-to-typescript@^15.0.2", "neverthrow": "npm:neverthrow@^7.0.1" }, "tasks": { "prisma:generate": "rm -rf prisma/generated && deno run -A npm:prisma@^5.19.0 generate", "prisma:reset": "rm -rf prisma/dev.db && deno run -A npm:prisma@^5.19.0 db push && deno run -A npm:prisma@^5.19.0 db seed", "start": "deno run --allow-net --allow-read --allow-env --allow-sys --allow-ffi main.ts", "models:generate": "deno run -A ./scripts/generateConfigType.ts" }, "compilerOptions": { "jsx": "precompile", "jsxImportSource": "hono/jsx" } } ``` #### package.json ```json { "name": "file-manager-x", "devDependencies": { "prisma": "^5.17.0" }, "dependencies": { "@prisma/client": "^5.17.0" }, "prisma": { "seed": "deno run -A prisma/seed.ts" } } ``` #### command log ```powershell PS C:\Users\fuji44\git\deno-playground> deno run main.ts true false PS C:\Users\fuji44\git\deno-playground> cp ..\file-manager-x\deno.jsonc .\ PS C:\Users\fuji44\git\deno-playground> deno run main.ts true false PS C:\Users\fuji44\git\deno-playground> cp ..\file-manager-x\package.json .\ PS C:\Users\fuji44\git\deno-playground> deno run main.ts error: Failed creating symlink in node_modules folder PS C:\Users\fuji44\git\deno-playground> ll Directory: C:\Users\fuji44\git\deno-playground Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2024/09/02 20:16 node_modules -a--- 2024/09/01 16:08 967 deno.jsonc -a--- 2024/09/02 19:50 104 main.ts -a--- 2024/08/27 14:38 200 package.json PS C:\Users\fuji44\git\deno-playground> tree . フォルダー パスの一覧 ボリューム シリアル番号は 5631-6695 です C:\USERS\FUJI44\GIT\DENO-PLAYGROUND └─node_modules ├─.bin └─.deno ├─is-buffer@1.1.6 │ └─node_modules │ └─is-buffer │ └─test ├─is-even@1.0.0 │ └─node_modules │ └─is-even ├─is-number@3.0.0 │ └─node_modules │ └─is-number ├─is-odd@0.1.2 │ └─node_modules │ └─is-odd ├─kind-of@3.2.2 │ └─node_modules │ └─kind-of └─node_modules PS C:\Users\fuji44\git\deno-playground> deno upgrade canary Current Deno version: v1.46.2 Looking up canary version Found latest canary version 42cf762cf4a5d3bf6e4c53c48464c0aec1b9e6b9 Downloading https://dl.deno.land/canary/42cf762cf4a5d3bf6e4c53c48464c0aec1b9e6b9/deno-x86_64-pc-windows-msvc.zip Deno is upgrading to version 42cf762cf4a5d3bf6e4c53c48464c0aec1b9e6b9 Upgraded successfully to Deno 42cf762cf4a5d3bf6e4c53c48464c0aec1b9e6b9 (canary) PS C:\Users\fuji44\git\deno-playground> rmdir -Recurse -Force .\node_modules\ PS C:\Users\fuji44\git\deno-playground> rm .\deno.jsonc PS C:\Users\fuji44\git\deno-playground> rm .\package.json PS C:\Users\fuji44\git\deno-playground> deno run main.ts true false PS C:\Users\fuji44\git\deno-playground> cp ..\file-manager-x\deno.jsonc .\ PS C:\Users\fuji44\git\deno-playground> deno run main.ts true false PS C:\Users\fuji44\git\deno-playground> cp ..\file-manager-x\package.json .\ PS C:\Users\fuji44\git\deno-playground> deno run main.ts error: Could not find a matching package for 'npm:is-even' in a package.json file. You must specify this as a package.json dependency when the node_modules folder is not managed by Deno. at file:///C:/Users/fuji44/git/deno-playground/main.ts:1:20 PS C:\Users\fuji44\git\deno-playground> ll Directory: C:\Users\fuji44\git\deno-playground Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2024/09/01 16:08 967 deno.jsonc -a--- 2024/09/02 19:50 104 main.ts -a--- 2024/08/27 14:38 200 package.json ```
nathanwhit commented 2 months ago

Yeah this is a regression from https://github.com/denoland/deno/pull/25164.

We use symlinks, and are supposed to fall back to junctions if symlinking isn't permitted, but the fallback logic must be incorrect. I'll look into this.

nathanwhit commented 2 months ago

This should now be fixed in the latest version of deno (1.46.3).

Try it out and feel free to reopen this issue if it still isn't working for you

fuji44 commented 2 months ago

Works fine. Thanks for the quick response.