From 0d3d4f54664b53d35781364a6c237cacdb8b84f4 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 20 Jan 2025 16:50:34 -0500 Subject: [PATCH] fix(install/global): remove importMap field from specified config file (#27744) Closes https://github.com/denoland/deno/issues/27734 --- cli/tools/installer.rs | 56 ++++++++++++++++--- .../config_file_import_map/__test__.jsonc | 5 ++ .../global/config_file_import_map/deno.json | 3 + .../config_file_import_map/import_map.json | 2 + .../global/config_file_import_map/install.out | 3 + .../global/config_file_import_map/main.ts | 1 + 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 tests/specs/install/global/config_file_import_map/__test__.jsonc create mode 100644 tests/specs/install/global/config_file_import_map/deno.json create mode 100644 tests/specs/install/global/config_file_import_map/import_map.json create mode 100644 tests/specs/install/global/config_file_import_map/install.out create mode 100644 tests/specs/install/global/config_file_import_map/main.ts diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index 509158779a..0f14782a48 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -115,10 +115,16 @@ exec deno {} "$@" Ok(()) } -fn get_installer_root() -> Result { - if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT") { +fn get_installer_root() -> Result { + if let Some(env_dir) = env::var_os("DENO_INSTALL_ROOT") { if !env_dir.is_empty() { - return canonicalize_path_maybe_not_exists(&PathBuf::from(env_dir)); + let env_dir = PathBuf::from(env_dir); + return canonicalize_path_maybe_not_exists(&env_dir).with_context(|| { + format!( + "Canonicalizing DENO_INSTALL_ROOT ('{}').", + env_dir.display() + ) + }); } } // Note: on Windows, the $HOME environment variable may be set by users or by @@ -587,11 +593,22 @@ async fn resolve_shim_data( let copy_path = get_hidden_file_with_ext(&file_path, "deno.json"); executable_args.push("--config".to_string()); executable_args.push(copy_path.to_str().unwrap().to_string()); - extra_files.push(( - copy_path, - fs::read_to_string(config_path) - .with_context(|| format!("error reading {config_path}"))?, - )); + let mut config_text = fs::read_to_string(config_path) + .with_context(|| format!("error reading {config_path}"))?; + // always remove the import map field because when someone specifies `--import-map` we + // don't want that file to be attempted to be loaded and when they don't specify that + // (which is just something we haven't implemented yet) + if let Some(new_text) = remove_import_map_field_from_text(&config_text) { + if flags.import_map_path.is_none() { + log::warn!( + "{} \"importMap\" field in the specified config file we be ignored. Use the --import-map flag instead.", + crate::colors::yellow("Warning"), + ); + } + config_text = new_text; + } + + extra_files.push((copy_path, config_text)); } else { executable_args.push("--no-config".to_string()); } @@ -631,6 +648,16 @@ async fn resolve_shim_data( }) } +fn remove_import_map_field_from_text(config_text: &str) -> Option { + let value = + jsonc_parser::cst::CstRootNode::parse(config_text, &Default::default()) + .ok()?; + let root_value = value.object_value()?; + let import_map_value = root_value.get("importMap")?; + import_map_value.remove(); + Some(value.to_string()) +} + fn get_hidden_file_with_ext(file_path: &Path, ext: &str) -> PathBuf { // use a dot file to prevent the file from showing up in some // users shell auto-complete since this directory is on the PATH @@ -1585,4 +1612,17 @@ mod tests { assert!(!file_path.exists()); } } + + #[test] + fn test_remove_import_map_field_from_text() { + assert_eq!( + remove_import_map_field_from_text( + r#"{ + "importMap": "./value.json" +}"#, + ) + .unwrap(), + "{}" + ); + } } diff --git a/tests/specs/install/global/config_file_import_map/__test__.jsonc b/tests/specs/install/global/config_file_import_map/__test__.jsonc new file mode 100644 index 0000000000..7999d7f6d8 --- /dev/null +++ b/tests/specs/install/global/config_file_import_map/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "tempDir": true, + "args": "install -g --root ./folder --config deno.json main.ts --name my-cli", + "output": "install.out" +} diff --git a/tests/specs/install/global/config_file_import_map/deno.json b/tests/specs/install/global/config_file_import_map/deno.json new file mode 100644 index 0000000000..12a79304f3 --- /dev/null +++ b/tests/specs/install/global/config_file_import_map/deno.json @@ -0,0 +1,3 @@ +{ + "importMap": "./import_map.json" +} diff --git a/tests/specs/install/global/config_file_import_map/import_map.json b/tests/specs/install/global/config_file_import_map/import_map.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/tests/specs/install/global/config_file_import_map/import_map.json @@ -0,0 +1,2 @@ +{ +} diff --git a/tests/specs/install/global/config_file_import_map/install.out b/tests/specs/install/global/config_file_import_map/install.out new file mode 100644 index 0000000000..a39b941cc0 --- /dev/null +++ b/tests/specs/install/global/config_file_import_map/install.out @@ -0,0 +1,3 @@ +Warning "importMap" field in the specified config file we be ignored. Use the --import-map flag instead. +✅ Successfully installed my-cli +[WILDCARD] diff --git a/tests/specs/install/global/config_file_import_map/main.ts b/tests/specs/install/global/config_file_import_map/main.ts new file mode 100644 index 0000000000..296d5492b0 --- /dev/null +++ b/tests/specs/install/global/config_file_import_map/main.ts @@ -0,0 +1 @@ +console.log(1);