From fadda6a8fb00602e85b7aa415747c56815d1efac Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 19 Nov 2024 19:49:19 -0500 Subject: [PATCH] fix(lockfile): track dependencies specified in TypeScript compiler options (#26551) We should track dependencies in `jsxImportSource`, `jsxImportSourceTypes`, and `types`. That way, for example, if someone removes or changes the `jsxImportSource` then we can remove those items from the lockfile. --- cli/args/deno_json.rs | 52 +++++++++++++++++-- cli/args/lockfile.rs | 6 +-- .../__test__.jsonc | 13 +++++ .../jsx_import_source_and_types/deno.json | 10 ++++ .../jsx_import_source_and_types/deno.lock.out | 30 +++++++++++ .../jsx_import_source_and_types/index.out | 7 +++ .../jsx_import_source_and_types/index.tsx | 1 + 7 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 tests/specs/lockfile/jsx_import_source_and_types/__test__.jsonc create mode 100644 tests/specs/lockfile/jsx_import_source_and_types/deno.json create mode 100644 tests/specs/lockfile/jsx_import_source_and_types/deno.lock.out create mode 100644 tests/specs/lockfile/jsx_import_source_and_types/index.out create mode 100644 tests/specs/lockfile/jsx_import_source_and_types/index.tsx diff --git a/cli/args/deno_json.rs b/cli/args/deno_json.rs index e9ab0189f5..a82289e67d 100644 --- a/cli/args/deno_json.rs +++ b/cli/args/deno_json.rs @@ -70,7 +70,41 @@ pub fn deno_json_deps( let values = imports_values(config.json.imports.as_ref()) .into_iter() .chain(scope_values(config.json.scopes.as_ref())); - values_to_set(values) + let mut set = values_to_set(values); + + if let Some(serde_json::Value::Object(compiler_options)) = + &config.json.compiler_options + { + // add jsxImportSource + if let Some(serde_json::Value::String(value)) = + compiler_options.get("jsxImportSource") + { + if let Some(dep_req) = value_to_dep_req(value) { + set.insert(dep_req); + } + } + // add jsxImportSourceTypes + if let Some(serde_json::Value::String(value)) = + compiler_options.get("jsxImportSourceTypes") + { + if let Some(dep_req) = value_to_dep_req(value) { + set.insert(dep_req); + } + } + // add the dependencies in the types array + if let Some(serde_json::Value::Array(types)) = compiler_options.get("types") + { + for value in types { + if let serde_json::Value::String(value) = value { + if let Some(dep_req) = value_to_dep_req(value) { + set.insert(dep_req); + } + } + } + } + } + + set } fn imports_values(value: Option<&serde_json::Value>) -> Vec<&String> { @@ -98,15 +132,23 @@ fn values_to_set<'a>( ) -> HashSet { let mut entries = HashSet::new(); for value in values { - if let Ok(req_ref) = JsrPackageReqReference::from_str(value) { - entries.insert(JsrDepPackageReq::jsr(req_ref.into_inner().req)); - } else if let Ok(req_ref) = NpmPackageReqReference::from_str(value) { - entries.insert(JsrDepPackageReq::npm(req_ref.into_inner().req)); + if let Some(dep_req) = value_to_dep_req(value) { + entries.insert(dep_req); } } entries } +fn value_to_dep_req(value: &str) -> Option { + if let Ok(req_ref) = JsrPackageReqReference::from_str(value) { + Some(JsrDepPackageReq::jsr(req_ref.into_inner().req)) + } else if let Ok(req_ref) = NpmPackageReqReference::from_str(value) { + Some(JsrDepPackageReq::npm(req_ref.into_inner().req)) + } else { + None + } +} + pub fn check_warn_tsconfig(ts_config: &TsConfigForEmit) { if let Some(ignored_options) = &ts_config.maybe_ignored_options { log::warn!("{}", ignored_options); diff --git a/cli/args/lockfile.rs b/cli/args/lockfile.rs index 1805d26426..a9eb8a0d7c 100644 --- a/cli/args/lockfile.rs +++ b/cli/args/lockfile.rs @@ -126,11 +126,7 @@ impl CliLockfile { maybe_deno_json: Option<&ConfigFile>, ) -> HashSet { maybe_deno_json - .map(|c| { - crate::args::deno_json::deno_json_deps(c) - .into_iter() - .collect() - }) + .map(crate::args::deno_json::deno_json_deps) .unwrap_or_default() } diff --git a/tests/specs/lockfile/jsx_import_source_and_types/__test__.jsonc b/tests/specs/lockfile/jsx_import_source_and_types/__test__.jsonc new file mode 100644 index 0000000000..08ab2acf95 --- /dev/null +++ b/tests/specs/lockfile/jsx_import_source_and_types/__test__.jsonc @@ -0,0 +1,13 @@ +{ + "tempDir": true, + "steps": [{ + "args": "run index.tsx", + "output": "index.out" + }, { + "args": [ + "eval", + "console.log(Deno.readTextFileSync('deno.lock').trim())" + ], + "output": "deno.lock.out" + }] +} diff --git a/tests/specs/lockfile/jsx_import_source_and_types/deno.json b/tests/specs/lockfile/jsx_import_source_and_types/deno.json new file mode 100644 index 0000000000..3755934a58 --- /dev/null +++ b/tests/specs/lockfile/jsx_import_source_and_types/deno.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "npm:react", + "jsxImportSourceTypes": "npm:@types/react", + "types": [ + "npm:@types/node" + ] + } +} diff --git a/tests/specs/lockfile/jsx_import_source_and_types/deno.lock.out b/tests/specs/lockfile/jsx_import_source_and_types/deno.lock.out new file mode 100644 index 0000000000..3a933a1aa2 --- /dev/null +++ b/tests/specs/lockfile/jsx_import_source_and_types/deno.lock.out @@ -0,0 +1,30 @@ +{ + "version": "4", + "specifiers": { + "npm:react@*": "18.2.0" + }, + "npm": { + "js-tokens@4.0.0": { + "integrity": "[WILDLINE]" + }, + "loose-envify@1.4.0": { + "integrity": "[WILDLINE]", + "dependencies": [ + "js-tokens" + ] + }, + "react@18.2.0": { + "integrity": "[WILDLINE]", + "dependencies": [ + "loose-envify" + ] + } + }, + "workspace": { + "dependencies": [ + "npm:@types/node@*", + "npm:@types/react@*", + "npm:react@*" + ] + } +} diff --git a/tests/specs/lockfile/jsx_import_source_and_types/index.out b/tests/specs/lockfile/jsx_import_source_and_types/index.out new file mode 100644 index 0000000000..7fc82c71df --- /dev/null +++ b/tests/specs/lockfile/jsx_import_source_and_types/index.out @@ -0,0 +1,7 @@ +Download http://localhost:4260/react +Download http://localhost:4260/loose-envify +Download http://localhost:4260/js-tokens +Download http://localhost:4260/react/react-18.2.0.tgz +Download http://localhost:4260/loose-envify/loose-envify-1.4.0.tgz +Download http://localhost:4260/js-tokens/js-tokens-4.0.0.tgz +1 diff --git a/tests/specs/lockfile/jsx_import_source_and_types/index.tsx b/tests/specs/lockfile/jsx_import_source_and_types/index.tsx new file mode 100644 index 0000000000..296d5492b0 --- /dev/null +++ b/tests/specs/lockfile/jsx_import_source_and_types/index.tsx @@ -0,0 +1 @@ +console.log(1);