Skip to content

Commit 784c6a5

Browse files
authored
fix(check): do not add @types/node@* to lockfile if a @types/node req already exists (denoland#29270)
1 parent 527c740 commit 784c6a5

File tree

14 files changed

+122
-88
lines changed

14 files changed

+122
-88
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ deno_lint = "=0.75.0"
6565
deno_lockfile = "=0.28.0"
6666
deno_media_type = { version = "=0.2.8", features = ["module_specifier"] }
6767
deno_native_certs = "0.3.0"
68-
deno_npm = "=0.33.2"
68+
deno_npm = "=0.33.3"
6969
deno_package_json = { version = "=0.6.0", default-features = false }
7070
deno_path_util = "=0.3.2"
7171
deno_semver = "=0.7.1"

cli/factory.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,6 @@ impl CliFactory {
973973
cli_options.clone(),
974974
self.module_graph_builder().await?.clone(),
975975
self.node_resolver().await?.clone(),
976-
self.npm_installer_if_managed().await?.cloned(),
977976
self.npm_resolver().await?.clone(),
978977
self.sys(),
979978
self.tsconfig_resolver()?.clone(),
@@ -1034,7 +1033,6 @@ impl CliFactory {
10341033
let cli_options = self.cli_options()?;
10351034
Ok(Arc::new(ModuleGraphCreator::new(
10361035
cli_options.clone(),
1037-
self.npm_installer_if_managed().await?.cloned(),
10381036
self.module_graph_builder().await?.clone(),
10391037
self.type_checker().await?.clone(),
10401038
)))

cli/graph_util.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,18 @@ pub struct CreateGraphOptions<'a> {
364364

365365
pub struct ModuleGraphCreator {
366366
options: Arc<CliOptions>,
367-
npm_installer: Option<Arc<NpmInstaller>>,
368367
module_graph_builder: Arc<ModuleGraphBuilder>,
369368
type_checker: Arc<TypeChecker>,
370369
}
371370

372371
impl ModuleGraphCreator {
373372
pub fn new(
374373
options: Arc<CliOptions>,
375-
npm_installer: Option<Arc<NpmInstaller>>,
376374
module_graph_builder: Arc<ModuleGraphBuilder>,
377375
type_checker: Arc<TypeChecker>,
378376
) -> Self {
379377
Self {
380378
options,
381-
npm_installer,
382379
module_graph_builder,
383380
type_checker,
384381
}
@@ -469,7 +466,7 @@ impl ModuleGraphCreator {
469466
if self.options.type_check_mode().is_true()
470467
&& !graph_has_external_remote(&graph)
471468
{
472-
self.type_check_graph(graph.clone()).await?;
469+
self.type_check_graph(graph.clone())?;
473470
}
474471

475472
if build_fast_check_graph {
@@ -501,12 +498,6 @@ impl ModuleGraphCreator {
501498
.build_graph_with_npm_resolution(&mut graph, options)
502499
.await?;
503500

504-
if let Some(npm_installer) = &self.npm_installer {
505-
if graph.has_node_specifier && self.options.type_check_mode().is_true() {
506-
npm_installer.inject_synthetic_types_node_package().await?;
507-
}
508-
}
509-
510501
Ok(graph)
511502
}
512503

@@ -530,7 +521,7 @@ impl ModuleGraphCreator {
530521

531522
if self.options.type_check_mode().is_true() {
532523
// provide the graph to the type checker, then get it back after it's done
533-
let graph = self.type_check_graph(graph).await?;
524+
let graph = self.type_check_graph(graph)?;
534525
Ok(graph)
535526
} else {
536527
Ok(Arc::new(graph))
@@ -541,22 +532,19 @@ impl ModuleGraphCreator {
541532
self.module_graph_builder.graph_valid(graph)
542533
}
543534

544-
async fn type_check_graph(
535+
fn type_check_graph(
545536
&self,
546537
graph: ModuleGraph,
547538
) -> Result<Arc<ModuleGraph>, CheckError> {
548-
self
549-
.type_checker
550-
.check(
551-
graph,
552-
CheckOptions {
553-
build_fast_check_graph: true,
554-
lib: self.options.ts_type_lib_window(),
555-
reload: self.options.reload_flag(),
556-
type_check_mode: self.options.type_check_mode(),
557-
},
558-
)
559-
.await
539+
self.type_checker.check(
540+
graph,
541+
CheckOptions {
542+
build_fast_check_graph: true,
543+
lib: self.options.ts_type_lib_window(),
544+
reload: self.options.reload_flag(),
545+
type_check_mode: self.options.type_check_mode(),
546+
},
547+
)
560548
}
561549
}
562550

@@ -785,7 +773,15 @@ impl ModuleGraphBuilder {
785773
},
786774
options.npm_caching,
787775
)
788-
.await
776+
.await?;
777+
778+
if let Some(npm_installer) = &self.npm_installer {
779+
if graph.has_node_specifier && options.graph_kind.include_types() {
780+
npm_installer.inject_synthetic_types_node_package().await?;
781+
}
782+
}
783+
784+
Ok(())
789785
}
790786

791787
async fn build_graph_with_npm_resolution_and_build_options<'a>(

cli/module_loader.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,19 @@ impl ModuleLoadPreparer {
224224

225225
// type check if necessary
226226
if self.options.type_check_mode().is_true() && !has_type_checked {
227-
self
228-
.type_checker
229-
.check(
230-
// todo(perf): since this is only done the first time the graph is
231-
// created, we could avoid the clone of the graph here by providing
232-
// the actual graph on the first run and then getting the Arc<ModuleGraph>
233-
// back from the return value.
234-
graph.clone(),
235-
CheckOptions {
236-
build_fast_check_graph: true,
237-
lib,
238-
reload: self.options.reload_flag(),
239-
type_check_mode: self.options.type_check_mode(),
240-
},
241-
)
242-
.await?;
227+
self.type_checker.check(
228+
// todo(perf): since this is only done the first time the graph is
229+
// created, we could avoid the clone of the graph here by providing
230+
// the actual graph on the first run and then getting the Arc<ModuleGraph>
231+
// back from the return value.
232+
graph.clone(),
233+
CheckOptions {
234+
build_fast_check_graph: true,
235+
lib,
236+
reload: self.options.reload_flag(),
237+
type_check_mode: self.options.type_check_mode(),
238+
},
239+
)?;
243240
}
244241

245242
// write the lockfile if there is one and do so after type checking

cli/npm/installer/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,16 @@ impl NpmInstaller {
206206
&self,
207207
) -> Result<(), JsErrorBox> {
208208
self.npm_resolution_initializer.ensure_initialized().await?;
209+
210+
// don't inject this if it's already been added
211+
if self
212+
.npm_resolution
213+
.any_top_level_package(|id| id.nv.name == "@types/node")
214+
{
215+
return Ok(());
216+
}
217+
209218
let reqs = &[PackageReq::from_str("@types/node").unwrap()];
210-
// add and ensure this isn't added to the lockfile
211219
self
212220
.add_package_reqs(reqs, PackageCaching::Only(reqs.into()))
213221
.await?;

cli/tools/publish/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,15 @@ impl PublishPreparer {
357357
} else {
358358
// fast check passed, type check the output as a temporary measure
359359
// until we know that it's reliable and stable
360-
let mut diagnostics_by_folder = self
361-
.type_checker
362-
.check_diagnostics(
363-
graph,
364-
CheckOptions {
365-
build_fast_check_graph: false, // already built
366-
lib: self.cli_options.ts_type_lib_window(),
367-
reload: self.cli_options.reload_flag(),
368-
type_check_mode: self.cli_options.type_check_mode(),
369-
},
370-
)
371-
.await?;
360+
let mut diagnostics_by_folder = self.type_checker.check_diagnostics(
361+
graph,
362+
CheckOptions {
363+
build_fast_check_graph: false, // already built
364+
lib: self.cli_options.ts_type_lib_window(),
365+
reload: self.cli_options.reload_flag(),
366+
type_check_mode: self.cli_options.type_check_mode(),
367+
},
368+
)?;
372369
// ignore unused parameter diagnostics that may occur due to fast check
373370
// not having function body implementations
374371
for result in diagnostics_by_folder.by_ref() {

cli/type_checker.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::graph_util::resolution_error_for_tsc_diagnostic;
3737
use crate::graph_util::BuildFastCheckGraphOptions;
3838
use crate::graph_util::ModuleGraphBuilder;
3939
use crate::node::CliNodeResolver;
40-
use crate::npm::installer::NpmInstaller;
4140
use crate::npm::CliNpmResolver;
4241
use crate::sys::CliSys;
4342
use crate::tsc;
@@ -104,7 +103,6 @@ pub struct TypeChecker {
104103
cjs_tracker: Arc<TypeCheckingCjsTracker>,
105104
cli_options: Arc<CliOptions>,
106105
module_graph_builder: Arc<ModuleGraphBuilder>,
107-
npm_installer: Option<Arc<NpmInstaller>>,
108106
node_resolver: Arc<CliNodeResolver>,
109107
npm_resolver: CliNpmResolver,
110108
sys: CliSys,
@@ -120,7 +118,6 @@ impl TypeChecker {
120118
cli_options: Arc<CliOptions>,
121119
module_graph_builder: Arc<ModuleGraphBuilder>,
122120
node_resolver: Arc<CliNodeResolver>,
123-
npm_installer: Option<Arc<NpmInstaller>>,
124121
npm_resolver: CliNpmResolver,
125122
sys: CliSys,
126123
tsconfig_resolver: Arc<TsConfigResolver>,
@@ -132,7 +129,6 @@ impl TypeChecker {
132129
cli_options,
133130
module_graph_builder,
134131
node_resolver,
135-
npm_installer,
136132
npm_resolver,
137133
sys,
138134
tsconfig_resolver,
@@ -144,12 +140,12 @@ impl TypeChecker {
144140
///
145141
/// It is expected that it is determined if a check and/or emit is validated
146142
/// before the function is called.
147-
pub async fn check(
143+
pub fn check(
148144
&self,
149145
graph: ModuleGraph,
150146
options: CheckOptions,
151147
) -> Result<Arc<ModuleGraph>, CheckError> {
152-
let mut diagnostics = self.check_diagnostics(graph, options).await?;
148+
let mut diagnostics = self.check_diagnostics(graph, options)?;
153149
let mut failed = false;
154150
for result in diagnostics.by_ref() {
155151
let mut diagnostics = result?;
@@ -178,7 +174,7 @@ impl TypeChecker {
178174
///
179175
/// It is expected that it is determined if a check and/or emit is validated
180176
/// before the function is called.
181-
pub async fn check_diagnostics(
177+
pub fn check_diagnostics(
182178
&self,
183179
mut graph: ModuleGraph,
184180
options: CheckOptions,
@@ -212,15 +208,6 @@ impl TypeChecker {
212208
));
213209
}
214210

215-
// node built-in specifiers use the @types/node package to determine
216-
// types, so inject that now (the caller should do this after the lockfile
217-
// has been written)
218-
if let Some(npm_installer) = &self.npm_installer {
219-
if graph.has_node_specifier {
220-
npm_installer.inject_synthetic_types_node_package().await?;
221-
}
222-
}
223-
224211
log::debug!("Type checking");
225212

226213
// add fast check to the graph before getting the roots

resolvers/deno/npm/managed/resolution.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ impl NpmResolutionCell {
141141
.collect::<Vec<_>>()
142142
}
143143

144+
pub fn any_top_level_package(
145+
&self,
146+
check: impl Fn(&NpmPackageId) -> bool,
147+
) -> bool {
148+
self.snapshot.read().top_level_packages().any(check)
149+
}
150+
144151
pub fn all_system_packages(
145152
&self,
146153
system_info: &NpmSystemInfo,
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
{
22
"tempDir": true,
3-
"steps": [{
4-
"args": "check --frozen=true",
5-
"output": "[WILDCARD]The lockfile is out of date.[WILDCARD]",
6-
"exitCode": 1
7-
}, {
8-
"args": "check --frozen=false",
9-
"output": "[WILDCARD]",
10-
"exitCode": 0
11-
}, {
12-
"args": "check --frozen=true",
13-
"output": "[WILDCARD]",
14-
"exitCode": 0
15-
}]
3+
"tests": {
4+
"check": {
5+
"steps": [{
6+
"args": "check --frozen=true",
7+
"output": "[WILDCARD]The lockfile is out of date.[WILDCARD]",
8+
"exitCode": 1
9+
}, {
10+
"args": "check --frozen=false",
11+
"output": "[WILDCARD]",
12+
"exitCode": 0
13+
}, {
14+
"args": "check --frozen=true",
15+
"output": "[WILDCARD]",
16+
"exitCode": 0
17+
}]
18+
},
19+
"install": {
20+
"steps": [{
21+
"args": "install --frozen=false --entrypoint main.ts",
22+
"output": "[WILDCARD]",
23+
"exitCode": 0
24+
}, {
25+
"args": "check --frozen=true",
26+
"output": "[WILDCARD]",
27+
"exitCode": 0
28+
}]
29+
}
30+
}
1631
}

0 commit comments

Comments
 (0)