diff options
author | nokazn <41154684+nokazn@users.noreply.github.com> | 2024-04-19 04:48:15 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-18 15:48:15 -0400 |
commit | 3d841acf4861220553614473538de935d24bcb21 (patch) | |
tree | d6e8a22bea4172d84730b7470ac83a2a9fce7a81 | |
parent | c6d44dbda6cb833bff9f88de6c66ecb95ff62523 (diff) |
fix(cli): avoid `deno add` and `deno vendor` errors when deno.json is empty (#23439)
-rw-r--r-- | cli/tools/registry/pm.rs | 10 | ||||
-rw-r--r-- | cli/tools/vendor/mod.rs | 1 | ||||
-rw-r--r-- | tests/integration/pm_tests.rs | 20 | ||||
-rw-r--r-- | tests/integration/vendor_tests.rs | 33 |
4 files changed, 62 insertions, 2 deletions
diff --git a/cli/tools/registry/pm.rs b/cli/tools/registry/pm.rs index a69a474e7..699b476cb 100644 --- a/cli/tools/registry/pm.rs +++ b/cli/tools/registry/pm.rs @@ -109,8 +109,14 @@ pub async fn add(flags: Flags, add_flags: AddFlags) -> Result<(), AnyError> { } } - let config_file_contents = - tokio::fs::read_to_string(&config_file_path).await.unwrap(); + let config_file_contents = { + let contents = tokio::fs::read_to_string(&config_file_path).await.unwrap(); + if contents.trim().is_empty() { + "{}\n".into() + } else { + contents + } + }; let ast = jsonc_parser::parse_to_ast( &config_file_contents, &Default::default(), diff --git a/cli/tools/vendor/mod.rs b/cli/tools/vendor/mod.rs index 2abdf6e99..cf10b77c7 100644 --- a/cli/tools/vendor/mod.rs +++ b/cli/tools/vendor/mod.rs @@ -310,6 +310,7 @@ fn update_config_text( ) -> Result<ModifiedResult, AnyError> { use jsonc_parser::ast::ObjectProp; use jsonc_parser::ast::Value; + let text = if text.trim().is_empty() { "{}\n" } else { text }; let ast = jsonc_parser::parse_to_ast(text, &Default::default(), &Default::default())?; let obj = match ast.value { diff --git a/tests/integration/pm_tests.rs b/tests/integration/pm_tests.rs index a8af67e5b..613ceef32 100644 --- a/tests/integration/pm_tests.rs +++ b/tests/integration/pm_tests.rs @@ -50,6 +50,26 @@ fn add_basic_no_deno_json() { } #[test] +fn add_basic_with_empty_deno_json() { + let context = pm_context_builder().build(); + let temp_dir = context.temp_dir(); + temp_dir.write("deno.json", ""); + + let output = context.new_command().args("add @denotest/add").run(); + output.assert_exit_code(0); + let output = output.combined_output(); + assert_contains!(output, "Add @denotest/add"); + temp_dir + .path() + .join("deno.json") + .assert_matches_json(json!({ + "imports": { + "@denotest/add": "jsr:@denotest/add@^1.0.0" + } + })); +} + +#[test] fn add_version_contraint() { let context = pm_context_builder().build(); let temp_dir = context.temp_dir().path(); diff --git a/tests/integration/vendor_tests.rs b/tests/integration/vendor_tests.rs index ab1119fe8..ce6aa7044 100644 --- a/tests/integration/vendor_tests.rs +++ b/tests/integration/vendor_tests.rs @@ -529,6 +529,39 @@ fn update_existing_config_test() { } #[test] +fn update_existing_empty_config_test() { + let _server = http_server(); + let t = TempDir::new(); + t.write( + "my_app.ts", + "import {Logger} from 'http://localhost:4545/vendor/logger.ts'; new Logger().log('outputted');", + ); + t.write("deno.json", ""); + + let deno = util::deno_cmd() + .current_dir(t.path()) + .arg("vendor") + .arg("my_app.ts") + .arg("--output") + .arg("vendor2") + .env("NO_COLOR", "1") + .piped_output() + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + assert_eq!( + String::from_utf8_lossy(&output.stderr).trim(), + format!( + "Download http://localhost:4545/vendor/logger.ts\n{}\n\n{}", + vendored_text("1 module", "vendor2"), + success_text_updated_deno_json("vendor2",) + ) + ); + assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), ""); + assert!(output.status.success()); +} + +#[test] fn vendor_npm_node_specifiers() { let context = TestContextBuilder::for_npm().use_temp_cwd().build(); let temp_dir = context.temp_dir(); |