From 4d1a14ca7fa9496f36470a7771448a9b006b0204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 20 Feb 2023 19:14:06 +0100 Subject: feat: auto-discover package.json for npm dependencies (#17272) This commits adds auto-discovery of "package.json" file when running "deno run" and "deno task" subcommands. In case of "deno run" the "package.json" is being looked up starting from the directory of the script that is being run, stopping early if "deno.json(c)" file is found (ie. FS tree won't be traversed "up" from "deno.json"). When "package.json" is discovered the "--node-modules-dir" flag is implied, leading to creation of local "node_modules/" directory - we did that, because most tools relying on "package.json" will expect "node_modules/" directory to be present (eg. Vite). Additionally "dependencies" and "devDependencies" specified in the "package.json" are downloaded on startup. This is a stepping stone to supporting bare specifier imports, but the actual integration will be done in a follow up commit. --------- Co-authored-by: David Sherret --- cli/tests/integration/run_tests.rs | 28 ++++++++++++++++++++++ cli/tests/integration/watcher_tests.rs | 2 +- cli/tests/testdata/npm/cached_only/main.out | 2 +- .../testdata/run/with_package_json/.gitignore | 1 + .../run/with_package_json/no_deno_json/main.out | 3 +++ .../run/with_package_json/no_deno_json/main.ts | 6 +++++ .../with_package_json/no_deno_json/package.json | 8 +++++++ .../run/with_package_json/npm_binary/main.out | 6 +++++ .../run/with_package_json/npm_binary/package.json | 8 +++++++ .../run/with_package_json/with_stop/main.out | 4 ++++ .../run/with_package_json/with_stop/package.json | 8 +++++++ .../with_stop/some/nested/deno.json | 5 ++++ .../with_stop/some/nested/dir/main.ts | 6 +++++ 13 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 cli/tests/testdata/run/with_package_json/.gitignore create mode 100644 cli/tests/testdata/run/with_package_json/no_deno_json/main.out create mode 100644 cli/tests/testdata/run/with_package_json/no_deno_json/main.ts create mode 100644 cli/tests/testdata/run/with_package_json/no_deno_json/package.json create mode 100644 cli/tests/testdata/run/with_package_json/npm_binary/main.out create mode 100644 cli/tests/testdata/run/with_package_json/npm_binary/package.json create mode 100644 cli/tests/testdata/run/with_package_json/with_stop/main.out create mode 100644 cli/tests/testdata/run/with_package_json/with_stop/package.json create mode 100644 cli/tests/testdata/run/with_package_json/with_stop/some/nested/deno.json create mode 100644 cli/tests/testdata/run/with_package_json/with_stop/some/nested/dir/main.ts (limited to 'cli/tests') diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 02fa5c2a0..6a436c318 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2693,6 +2693,34 @@ itest!(config_not_auto_discovered_for_remote_script { http_server: true, }); +itest!(package_json_auto_discovered_for_local_script_log { + args: "run -L debug no_deno_json/main.ts", + output: "run/with_package_json/no_deno_json/main.out", + maybe_cwd: Some("run/with_package_json/"), + envs: env_vars_for_npm_tests_no_sync_download(), + http_server: true, +}); + +// In this case we shouldn't discover `package.json` file, because it's in a +// directory that is above the directory containing `deno.json` file. +itest!( + package_json_auto_discovered_for_local_script_log_with_stop { + args: "run -L debug with_stop/some/nested/dir/main.ts", + output: "run/with_package_json/with_stop/main.out", + maybe_cwd: Some("run/with_package_json/"), + envs: env_vars_for_npm_tests_no_sync_download(), + http_server: true, + } +); + +itest!(package_json_auto_discovered_for_npm_binary { + args: "run -L debug -A npm:@denotest/bin/cli-esm this is a test", + output: "run/with_package_json/npm_binary/main.out", + maybe_cwd: Some("run/with_package_json/npm_binary/"), + envs: env_vars_for_npm_tests_no_sync_download(), + http_server: true, +}); + itest!(wasm_streaming_panic_test { args: "run run/wasm_streaming_panic_test.js", output: "run/wasm_streaming_panic_test.js.out", diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs index 99d5a5727..0c9b8c29f 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -1177,7 +1177,7 @@ fn run_watch_dynamic_imports() { .spawn() .unwrap(); let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); - + assert_contains!(stderr_lines.next().unwrap(), "No package.json file found"); assert_contains!(stderr_lines.next().unwrap(), "Process started"); wait_contains( diff --git a/cli/tests/testdata/npm/cached_only/main.out b/cli/tests/testdata/npm/cached_only/main.out index e902bff49..f49494839 100644 --- a/cli/tests/testdata/npm/cached_only/main.out +++ b/cli/tests/testdata/npm/cached_only/main.out @@ -1,4 +1,4 @@ -error: Error getting response at http://localhost:4545/npm/registry/chalk +error: Error getting response at http://localhost:4545/npm/registry/chalk for package "chalk" Caused by: An npm specifier not found in cache: "chalk", --cached-only is specified. diff --git a/cli/tests/testdata/run/with_package_json/.gitignore b/cli/tests/testdata/run/with_package_json/.gitignore new file mode 100644 index 000000000..40b878db5 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/cli/tests/testdata/run/with_package_json/no_deno_json/main.out b/cli/tests/testdata/run/with_package_json/no_deno_json/main.out new file mode 100644 index 000000000..c0dab77d0 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/no_deno_json/main.out @@ -0,0 +1,3 @@ +[WILDCARD]package.json file found at '[WILDCARD]with_package_json[WILDCARD]package.json' +[WILDCARD] +ok diff --git a/cli/tests/testdata/run/with_package_json/no_deno_json/main.ts b/cli/tests/testdata/run/with_package_json/no_deno_json/main.ts new file mode 100644 index 000000000..daefa8f60 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/no_deno_json/main.ts @@ -0,0 +1,6 @@ +// TODO(bartlomieju): currently we don't support actual bare specifier +// imports; this will be done in a follow up PR. +// import express from "express"; + +// console.log(express); +console.log("ok"); diff --git a/cli/tests/testdata/run/with_package_json/no_deno_json/package.json b/cli/tests/testdata/run/with_package_json/no_deno_json/package.json new file mode 100644 index 000000000..9ee3f39a8 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/no_deno_json/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "@denotest/check-error": "1.0.0" + }, + "devDependencies": { + "@denotest/cjs-default-export": "1.0.0" + } +} diff --git a/cli/tests/testdata/run/with_package_json/npm_binary/main.out b/cli/tests/testdata/run/with_package_json/npm_binary/main.out new file mode 100644 index 000000000..56cdae6f9 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/npm_binary/main.out @@ -0,0 +1,6 @@ +[WILDCARD]package.json file found at '[WILDCARD]with_package_json[WILDCARD]npm_binary[WILDCARD]package.json' +[WILDCARD] +this +is +a +test diff --git a/cli/tests/testdata/run/with_package_json/npm_binary/package.json b/cli/tests/testdata/run/with_package_json/npm_binary/package.json new file mode 100644 index 000000000..9ee3f39a8 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/npm_binary/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "@denotest/check-error": "1.0.0" + }, + "devDependencies": { + "@denotest/cjs-default-export": "1.0.0" + } +} diff --git a/cli/tests/testdata/run/with_package_json/with_stop/main.out b/cli/tests/testdata/run/with_package_json/with_stop/main.out new file mode 100644 index 000000000..e7ef053e4 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/with_stop/main.out @@ -0,0 +1,4 @@ +[WILDCARD]Config file found at '[WILDCARD]with_package_json[WILDCARD]with_stop[WILDCARD]some[WILDCARD]nested[WILDCARD]deno.json' +[WILDCARD]No package.json file found +[WILDCARD] +ok diff --git a/cli/tests/testdata/run/with_package_json/with_stop/package.json b/cli/tests/testdata/run/with_package_json/with_stop/package.json new file mode 100644 index 000000000..9ee3f39a8 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/with_stop/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "@denotest/check-error": "1.0.0" + }, + "devDependencies": { + "@denotest/cjs-default-export": "1.0.0" + } +} diff --git a/cli/tests/testdata/run/with_package_json/with_stop/some/nested/deno.json b/cli/tests/testdata/run/with_package_json/with_stop/some/nested/deno.json new file mode 100644 index 000000000..36e1765d1 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/with_stop/some/nested/deno.json @@ -0,0 +1,5 @@ +{ + "tasks": { + "dev": "deno run main.ts" + } +} diff --git a/cli/tests/testdata/run/with_package_json/with_stop/some/nested/dir/main.ts b/cli/tests/testdata/run/with_package_json/with_stop/some/nested/dir/main.ts new file mode 100644 index 000000000..daefa8f60 --- /dev/null +++ b/cli/tests/testdata/run/with_package_json/with_stop/some/nested/dir/main.ts @@ -0,0 +1,6 @@ +// TODO(bartlomieju): currently we don't support actual bare specifier +// imports; this will be done in a follow up PR. +// import express from "express"; + +// console.log(express); +console.log("ok"); -- cgit v1.2.3