Skip to content

Commit f4ef7b0

Browse files
fix: .npmrc settings not being passed to install/add command (#26473)
We weren't passing the resolved npmrc settings to the install commands. This lead us to always fall back to the default registry url instead of using the one from npmrc. Fixes #26139 Fixes #26033 Fixes #25924 Fixes #25822 Fixes #26152 --------- Co-authored-by: Bartek Iwańczuk <[email protected]>
1 parent f62283f commit f4ef7b0

39 files changed

+317
-42
lines changed

cli/args/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ fn discover_npmrc(
578578
let resolved = npmrc
579579
.as_resolved(npm_registry_url())
580580
.context("Failed to resolve .npmrc options")?;
581+
log::debug!(".npmrc found at: '{}'", path.display());
581582
Ok(Arc::new(resolved))
582583
}
583584

cli/cache/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl Loader for FetchCacher {
378378
} else {
379379
FetchPermissionsOptionRef::DynamicContainer(&permissions)
380380
},
381+
maybe_auth: None,
381382
maybe_accept: None,
382383
maybe_cache_setting: maybe_cache_setting.as_ref(),
383384
},

cli/file_fetcher.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use deno_graph::source::LoaderChecksum;
2424
use deno_path_util::url_to_file_path;
2525
use deno_runtime::deno_permissions::PermissionsContainer;
2626
use deno_runtime::deno_web::BlobStore;
27+
use http::header;
2728
use log::debug;
2829
use std::borrow::Cow;
2930
use std::collections::HashMap;
@@ -181,6 +182,7 @@ pub enum FetchPermissionsOptionRef<'a> {
181182
pub struct FetchOptions<'a> {
182183
pub specifier: &'a ModuleSpecifier,
183184
pub permissions: FetchPermissionsOptionRef<'a>,
185+
pub maybe_auth: Option<(header::HeaderName, header::HeaderValue)>,
184186
pub maybe_accept: Option<&'a str>,
185187
pub maybe_cache_setting: Option<&'a CacheSetting>,
186188
}
@@ -350,6 +352,7 @@ impl FileFetcher {
350352
maybe_accept: Option<&str>,
351353
cache_setting: &CacheSetting,
352354
maybe_checksum: Option<&LoaderChecksum>,
355+
maybe_auth: Option<(header::HeaderName, header::HeaderValue)>,
353356
) -> Result<FileOrRedirect, AnyError> {
354357
debug!(
355358
"FileFetcher::fetch_remote_no_follow - specifier: {}",
@@ -442,6 +445,7 @@ impl FileFetcher {
442445
.as_ref()
443446
.map(|(_, etag)| etag.clone()),
444447
maybe_auth_token: maybe_auth_token.clone(),
448+
maybe_auth: maybe_auth.clone(),
445449
maybe_progress_guard: maybe_progress_guard.as_ref(),
446450
})
447451
.await?
@@ -538,7 +542,18 @@ impl FileFetcher {
538542
specifier: &ModuleSpecifier,
539543
) -> Result<File, AnyError> {
540544
self
541-
.fetch_inner(specifier, FetchPermissionsOptionRef::AllowAll)
545+
.fetch_inner(specifier, None, FetchPermissionsOptionRef::AllowAll)
546+
.await
547+
}
548+
549+
#[inline(always)]
550+
pub async fn fetch_bypass_permissions_with_maybe_auth(
551+
&self,
552+
specifier: &ModuleSpecifier,
553+
maybe_auth: Option<(header::HeaderName, header::HeaderValue)>,
554+
) -> Result<File, AnyError> {
555+
self
556+
.fetch_inner(specifier, maybe_auth, FetchPermissionsOptionRef::AllowAll)
542557
.await
543558
}
544559

@@ -552,6 +567,7 @@ impl FileFetcher {
552567
self
553568
.fetch_inner(
554569
specifier,
570+
None,
555571
FetchPermissionsOptionRef::StaticContainer(permissions),
556572
)
557573
.await
@@ -560,12 +576,14 @@ impl FileFetcher {
560576
async fn fetch_inner(
561577
&self,
562578
specifier: &ModuleSpecifier,
579+
maybe_auth: Option<(header::HeaderName, header::HeaderValue)>,
563580
permissions: FetchPermissionsOptionRef<'_>,
564581
) -> Result<File, AnyError> {
565582
self
566583
.fetch_with_options(FetchOptions {
567584
specifier,
568585
permissions,
586+
maybe_auth,
569587
maybe_accept: None,
570588
maybe_cache_setting: None,
571589
})
@@ -585,12 +603,14 @@ impl FileFetcher {
585603
max_redirect: usize,
586604
) -> Result<File, AnyError> {
587605
let mut specifier = Cow::Borrowed(options.specifier);
606+
let mut maybe_auth = options.maybe_auth.clone();
588607
for _ in 0..=max_redirect {
589608
match self
590609
.fetch_no_follow_with_options(FetchNoFollowOptions {
591610
fetch_options: FetchOptions {
592611
specifier: &specifier,
593612
permissions: options.permissions,
613+
maybe_auth: maybe_auth.clone(),
594614
maybe_accept: options.maybe_accept,
595615
maybe_cache_setting: options.maybe_cache_setting,
596616
},
@@ -602,6 +622,10 @@ impl FileFetcher {
602622
return Ok(file);
603623
}
604624
FileOrRedirect::Redirect(redirect_specifier) => {
625+
// If we were redirected to another origin, don't send the auth header anymore.
626+
if redirect_specifier.origin() != specifier.origin() {
627+
maybe_auth = None;
628+
}
605629
specifier = Cow::Owned(redirect_specifier);
606630
}
607631
}
@@ -666,6 +690,7 @@ impl FileFetcher {
666690
options.maybe_accept,
667691
options.maybe_cache_setting.unwrap_or(&self.cache_setting),
668692
maybe_checksum,
693+
options.maybe_auth,
669694
)
670695
.await
671696
}
@@ -756,6 +781,7 @@ mod tests {
756781
FetchOptions {
757782
specifier,
758783
permissions: FetchPermissionsOptionRef::AllowAll,
784+
maybe_auth: None,
759785
maybe_accept: None,
760786
maybe_cache_setting: Some(&file_fetcher.cache_setting),
761787
},
@@ -1255,6 +1281,7 @@ mod tests {
12551281
FetchOptions {
12561282
specifier: &specifier,
12571283
permissions: FetchPermissionsOptionRef::AllowAll,
1284+
maybe_auth: None,
12581285
maybe_accept: None,
12591286
maybe_cache_setting: Some(&file_fetcher.cache_setting),
12601287
},
@@ -1268,6 +1295,7 @@ mod tests {
12681295
FetchOptions {
12691296
specifier: &specifier,
12701297
permissions: FetchPermissionsOptionRef::AllowAll,
1298+
maybe_auth: None,
12711299
maybe_accept: None,
12721300
maybe_cache_setting: Some(&file_fetcher.cache_setting),
12731301
},

cli/http_util.rs

+21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use deno_runtime::deno_fetch;
1919
use deno_runtime::deno_fetch::create_http_client;
2020
use deno_runtime::deno_fetch::CreateHttpClientOptions;
2121
use deno_runtime::deno_tls::RootCertStoreProvider;
22+
use http::header;
2223
use http::header::HeaderName;
2324
use http::header::HeaderValue;
2425
use http::header::ACCEPT;
@@ -204,6 +205,7 @@ pub struct FetchOnceArgs<'a> {
204205
pub maybe_accept: Option<String>,
205206
pub maybe_etag: Option<String>,
206207
pub maybe_auth_token: Option<AuthToken>,
208+
pub maybe_auth: Option<(header::HeaderName, header::HeaderValue)>,
207209
pub maybe_progress_guard: Option<&'a UpdateGuard>,
208210
}
209211

@@ -382,6 +384,8 @@ impl HttpClient {
382384
request
383385
.headers_mut()
384386
.insert(AUTHORIZATION, authorization_val);
387+
} else if let Some((header, value)) = args.maybe_auth {
388+
request.headers_mut().insert(header, value);
385389
}
386390
if let Some(accept) = args.maybe_accept {
387391
let accepts_val = HeaderValue::from_str(&accept)?;
@@ -792,6 +796,7 @@ mod test {
792796
maybe_etag: None,
793797
maybe_auth_token: None,
794798
maybe_progress_guard: None,
799+
maybe_auth: None,
795800
})
796801
.await;
797802
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -818,6 +823,7 @@ mod test {
818823
maybe_etag: None,
819824
maybe_auth_token: None,
820825
maybe_progress_guard: None,
826+
maybe_auth: None,
821827
})
822828
.await;
823829
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -845,6 +851,7 @@ mod test {
845851
maybe_etag: None,
846852
maybe_auth_token: None,
847853
maybe_progress_guard: None,
854+
maybe_auth: None,
848855
})
849856
.await;
850857
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -866,6 +873,7 @@ mod test {
866873
maybe_etag: Some("33a64df551425fcc55e".to_string()),
867874
maybe_auth_token: None,
868875
maybe_progress_guard: None,
876+
maybe_auth: None,
869877
})
870878
.await;
871879
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
@@ -885,6 +893,7 @@ mod test {
885893
maybe_etag: None,
886894
maybe_auth_token: None,
887895
maybe_progress_guard: None,
896+
maybe_auth: None,
888897
})
889898
.await;
890899
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -914,6 +923,7 @@ mod test {
914923
maybe_etag: None,
915924
maybe_auth_token: None,
916925
maybe_progress_guard: None,
926+
maybe_auth: None,
917927
})
918928
.await;
919929
if let Ok(FetchOnceResult::Code(body, _)) = result {
@@ -939,6 +949,7 @@ mod test {
939949
maybe_etag: None,
940950
maybe_auth_token: None,
941951
maybe_progress_guard: None,
952+
maybe_auth: None,
942953
})
943954
.await;
944955
if let Ok(FetchOnceResult::Redirect(url, _)) = result {
@@ -974,6 +985,7 @@ mod test {
974985
maybe_etag: None,
975986
maybe_auth_token: None,
976987
maybe_progress_guard: None,
988+
maybe_auth: None,
977989
})
978990
.await;
979991
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -1021,6 +1033,7 @@ mod test {
10211033
maybe_etag: None,
10221034
maybe_auth_token: None,
10231035
maybe_progress_guard: None,
1036+
maybe_auth: None,
10241037
})
10251038
.await;
10261039

@@ -1083,6 +1096,7 @@ mod test {
10831096
maybe_etag: None,
10841097
maybe_auth_token: None,
10851098
maybe_progress_guard: None,
1099+
maybe_auth: None,
10861100
})
10871101
.await;
10881102

@@ -1136,6 +1150,7 @@ mod test {
11361150
maybe_etag: None,
11371151
maybe_auth_token: None,
11381152
maybe_progress_guard: None,
1153+
maybe_auth: None,
11391154
})
11401155
.await;
11411156
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -1177,6 +1192,7 @@ mod test {
11771192
maybe_etag: None,
11781193
maybe_auth_token: None,
11791194
maybe_progress_guard: None,
1195+
maybe_auth: None,
11801196
})
11811197
.await;
11821198
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -1199,6 +1215,7 @@ mod test {
11991215
maybe_etag: Some("33a64df551425fcc55e".to_string()),
12001216
maybe_auth_token: None,
12011217
maybe_progress_guard: None,
1218+
maybe_auth: None,
12021219
})
12031220
.await;
12041221
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
@@ -1233,6 +1250,7 @@ mod test {
12331250
maybe_etag: None,
12341251
maybe_auth_token: None,
12351252
maybe_progress_guard: None,
1253+
maybe_auth: None,
12361254
})
12371255
.await;
12381256
if let Ok(FetchOnceResult::Code(body, headers)) = result {
@@ -1262,6 +1280,7 @@ mod test {
12621280
maybe_etag: None,
12631281
maybe_auth_token: None,
12641282
maybe_progress_guard: None,
1283+
maybe_auth: None,
12651284
})
12661285
.await;
12671286
assert!(result.is_err());
@@ -1283,6 +1302,7 @@ mod test {
12831302
maybe_etag: None,
12841303
maybe_auth_token: None,
12851304
maybe_progress_guard: None,
1305+
maybe_auth: None,
12861306
})
12871307
.await;
12881308

@@ -1306,6 +1326,7 @@ mod test {
13061326
maybe_etag: None,
13071327
maybe_auth_token: None,
13081328
maybe_progress_guard: None,
1329+
maybe_auth: None,
13091330
})
13101331
.await;
13111332

cli/lsp/npm.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use dashmap::DashMap;
44
use deno_core::anyhow::anyhow;
55
use deno_core::error::AnyError;
66
use deno_core::serde_json;
7+
use deno_npm::npm_rc::NpmRc;
78
use deno_semver::package::PackageNv;
89
use deno_semver::Version;
910
use serde::Deserialize;
@@ -25,7 +26,10 @@ pub struct CliNpmSearchApi {
2526

2627
impl CliNpmSearchApi {
2728
pub fn new(file_fetcher: Arc<FileFetcher>) -> Self {
28-
let resolver = NpmFetchResolver::new(file_fetcher.clone());
29+
let resolver = NpmFetchResolver::new(
30+
file_fetcher.clone(),
31+
Arc::new(NpmRc::default().as_resolved(npm_registry_url()).unwrap()),
32+
);
2933
Self {
3034
file_fetcher,
3135
resolver,

cli/lsp/registries.rs

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ impl ModuleRegistry {
482482
.fetch_with_options(FetchOptions {
483483
specifier: &specifier,
484484
permissions: FetchPermissionsOptionRef::AllowAll,
485+
maybe_auth: None,
485486
maybe_accept: Some("application/vnd.deno.reg.v2+json, application/vnd.deno.reg.v1+json;q=0.9, application/json;q=0.8"),
486487
maybe_cache_setting: None,
487488
})

cli/npm/managed/cache/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::cache::CACHE_PERM;
2626
use crate::util::fs::atomic_write_file_with_retries;
2727
use crate::util::fs::hard_link_dir_recursive;
2828

29-
mod registry_info;
29+
pub mod registry_info;
3030
mod tarball;
3131
mod tarball_extract;
3232

0 commit comments

Comments
 (0)