denoland / deno

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

Deno add - "Failed to install from package.json" "Unexpected character." #26653

Open silverbucket opened 1 day ago

silverbucket commented 1 day ago

Version: Deno 2.0.4

$ deno add npm:debug
Add npm:debug@4.3.7
error: Failed to install from package.json

Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         ^
         ~
$ cat deno.json
{
  "name": "...",
  "description": "...",
  "version": "3.0.0-alpha.5",
  "exports": "./src/index.ts",
  "tasks": {
    "build": "deno run --allow-read --allow-write scripts/export-json-schema.ts",
    "coverage": "deno coverage | deno run jsr:@silverbucket/threshold 100 88",
    "test": "deno test --coverage --clean --allow-env src/**/*.test.ts"
  },
  "imports": {
    "@silverbucket/threshold": "jsr:@silverbucket/threshold@1.0.2",
    "@types/node": "npm:@types/node@22.5.0",
    "@types/debug": "npm:@types/debug@4.1.12",
    "ajv": "npm:ajv@8.12.0",
    "ajv-formats": "npm:ajv-formats@2.1.1",
    "@silverbucket/ajv-formats-draft2019": "jsr:@silverbucket/ajv-formats-draft2019@1.6.1",
    "debug": "npm:debug@4.3.7"
  }
}
bartlomieju commented 21 hours ago

Do you also have a package.json file in your project?

bartlomieju commented 20 hours ago

I tried reproducing the problem and with provided deno.json and running deno add npm:debug I don't get any error. @silverbucket could you please provide more information about your project?

silverbucket commented 19 hours ago

@bartlomieju Sure what would you like to see? To answer your question, there is no package.json.

$ ls -al
total 40
drwxr-xr-x@  9 njenning  staff   288 Oct 31 01:26 .
drwxr-xr-x@ 15 njenning  staff   480 Aug 26 23:06 ..
-rw-r--r--@  1 njenning  staff  1077 Aug 26 23:06 LICENSE
-rw-r--r--@  1 njenning  staff   271 Aug 29 14:54 README.md
drwxr-xr-x@  2 njenning  staff    64 Oct 31 01:24 coverage
-rw-r--r--@  1 njenning  staff   823 Oct 31 01:26 deno.json
-rw-r--r--@  1 njenning  staff  5814 Aug 27 17:53 deno.lock
drwxr-xr-x@  4 njenning  staff   128 Aug 27 17:44 scripts
drwxr-xr-x@ 12 njenning  staff   384 Oct 31 01:23 src

$ deno add npm:debug
Add npm:debug@4.3.7
error: Failed to install from package.json

Caused by:
    0: Invalid version requirement
    1: Unexpected character.
         ^
         ~

It might be worth mentioning that this is a mono-repo, however I've never had issues converting packages one at a time with deno 1.45, it was only upgrading to 2.0.x that I started getting issues like this.

bartlomieju commented 19 hours ago

@silverbucket is your reo public so o could try it myself?

dsherret commented 18 hours ago

We should improve this error message to say the file the error occurs in. Probably there's a package.json in the workspace in this case.

silverbucket commented 18 hours ago

@bartlomieju Here's the repo + branch + package in the mono repo where I ran those commands: https://github.com/sockethub/sockethub/tree/migrate-to-deno/packages/schemas

But while I was checking the modified files to commit and update that branch before I wrote this message, I noticed that the root package.json was modified with the debug dependencies. This goes along with what @dsherret was suggesting.

So, for some reason, using deno add while being in the packages/schemas/ directory (which has it's own deno.json file), deno is decided to try to add those to the repository root package.json and getting a syntax error of some sort (I still don't know what the syntax error is referring to as it's rather obtuse).

diff --git a/package.json b/package.json
index b805b00b..257b9dae 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,9 @@
   "engines": {
     "deno": ">=1.45"
   },
+  "dependencies": {
+    "debug": "^4.3.7"
+  },
   "devDependencies": {
     "@sockethub/data-layer": "workspace:1.0.0-alpha.4",
     "@sockethub/schemas": "workspace:3.0.0-alpha.4",
dsherret commented 17 hours ago

I opened https://github.com/denoland/deno/pull/26665 which should help reduce the confusion slightly in the future. It will say the package.json file and the dependency that caused the issue.

So, for some reason, using deno add while being in the packages/schemas/ directory (which has it's own deno.json file), deno is decided to try to add those to the repository root package.json and getting a syntax error of some sort (I still don't know what the syntax error is referring to as it's rather obtuse).

@nathanwhit could you take a look? I don't think it should be doing that.

bartlomieju commented 17 hours ago

Deno prefers to add npm dependencies to package.json file over deno.json, but yeah it's a bug - it should only happen for package.json in the same directory as deno.json, it shouldn't walk up the file tree to find package.json.