Skip to content

Commit ea3251e

Browse files
dsherretcrowlKats
authored andcommitted
fix(npmrc): skip loading .npmrc in home dir on permission error (#24758)
(cherry picked from commit 086fa28)
1 parent 65ed063 commit ea3251e

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

cli/args/mod.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ pub fn discover_npmrc_from_workspace(
517517
///
518518
/// In the future we will need to support it in user directory or global directory
519519
/// as per https://docs.npmjs.com/cli/v10/configuring-npm/npmrc#files.
520-
pub fn discover_npmrc(
520+
fn discover_npmrc(
521521
maybe_package_json_path: Option<PathBuf>,
522522
maybe_deno_json_path: Option<PathBuf>,
523523
) -> Result<(Arc<ResolvedNpmRc>, Option<PathBuf>), AnyError> {
@@ -527,16 +527,22 @@ pub fn discover_npmrc(
527527
std::env::var(var_name).ok()
528528
}
529529

530+
#[derive(Debug, Error)]
531+
#[error("Error loading .npmrc at {}.", path.display())]
532+
struct NpmRcLoadError {
533+
path: PathBuf,
534+
#[source]
535+
source: std::io::Error,
536+
}
537+
530538
fn try_to_read_npmrc(
531539
dir: &Path,
532-
) -> Result<Option<(String, PathBuf)>, AnyError> {
540+
) -> Result<Option<(String, PathBuf)>, NpmRcLoadError> {
533541
let path = dir.join(NPMRC_NAME);
534542
let maybe_source = match std::fs::read_to_string(&path) {
535543
Ok(source) => Some(source),
536544
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
537-
Err(err) => {
538-
bail!("Error loading .npmrc at {}. {:#}", path.display(), err)
539-
}
545+
Err(err) => return Err(NpmRcLoadError { path, source: err }),
540546
};
541547

542548
Ok(maybe_source.map(|source| (source, path)))
@@ -577,8 +583,20 @@ pub fn discover_npmrc(
577583
// home dir and then merge them.
578584
// 3. Try `.npmrc` in the user's home directory
579585
if let Some(home_dir) = cache::home_dir() {
580-
if let Some((source, path)) = try_to_read_npmrc(&home_dir)? {
581-
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
586+
match try_to_read_npmrc(&home_dir) {
587+
Ok(Some((source, path))) => {
588+
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
589+
}
590+
Ok(None) => {}
591+
Err(err) if err.source.kind() == std::io::ErrorKind::PermissionDenied => {
592+
log::debug!(
593+
"Skipping .npmrc in home directory due to permission denied error. {:#}",
594+
err
595+
);
596+
}
597+
Err(err) => {
598+
return Err(err.into());
599+
}
582600
}
583601
}
584602

0 commit comments

Comments
 (0)