fix(ext/node): discover .npmrc in user's homedir (#24021)

This commit adds discovery of `.npmrc` files in user's homedir.

This is not a perfect fix as it doesn't merge multiple `.npmrc` files
together as per https://github.com/denoland/deno/issues/23954
but allows to fallback if no `.npmrc` file is discovered in the project
root.
This commit is contained in:
Bartek Iwańczuk 2024-06-26 23:17:00 +01:00 committed by GitHub
parent 0da01c0ca6
commit 2a2ff96be1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 57 additions and 2 deletions

View File

@ -72,6 +72,7 @@ use std::sync::Arc;
use thiserror::Error;
use crate::args::import_map::enhance_import_map_value_with_workspace_members;
use crate::cache;
use crate::file_fetcher::FileFetcher;
use crate::util::fs::canonicalize_path_maybe_not_exists;
use crate::version;
@ -592,6 +593,7 @@ pub fn discover_npmrc(
Ok(Arc::new(resolved))
}
// 1. Try `.npmrc` next to `package.json`
if let Some(package_json_path) = maybe_package_json_path {
if let Some(package_json_dir) = package_json_path.parent() {
if let Some((source, path)) = try_to_read_npmrc(package_json_dir)? {
@ -600,6 +602,7 @@ pub fn discover_npmrc(
}
}
// 2. Try `.npmrc` next to `deno.json(c)`
if let Some(deno_json_path) = maybe_deno_json_path {
if let Some(deno_json_dir) = deno_json_path.parent() {
if let Some((source, path)) = try_to_read_npmrc(deno_json_dir)? {
@ -608,6 +611,15 @@ pub fn discover_npmrc(
}
}
// TODO(bartlomieju): update to read both files - one in the project root and one and
// home dir and then merge them.
// 3. Try `.npmrc` in the user's home directory
if let Some(home_dir) = cache::home_dir() {
if let Some((source, path)) = try_to_read_npmrc(&home_dir)? {
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
}
}
log::debug!("No .npmrc file found");
Ok((create_default_npmrc(), None))
}

View File

@ -169,7 +169,7 @@ impl DenoDir {
/// To avoid the poorly managed dirs crate
#[cfg(not(windows))]
mod dirs {
pub mod dirs {
use std::path::PathBuf;
pub fn cache_dir() -> Option<PathBuf> {
@ -227,7 +227,7 @@ mod dirs {
// https://github.com/dirs-dev/dirs-sys-rs/blob/ec7cee0b3e8685573d847f0a0f60aae3d9e07fa2/src/lib.rs#L140-L164
// MIT license. Copyright (c) 2018-2019 dirs-rs contributors
#[cfg(windows)]
mod dirs {
pub mod dirs {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;

1
cli/cache/mod.rs vendored
View File

@ -43,6 +43,7 @@ pub use caches::Caches;
pub use check::TypeCheckCache;
pub use code_cache::CodeCache;
pub use common::FastInsecureHasher;
pub use deno_dir::dirs::home_dir;
pub use deno_dir::DenoDir;
pub use deno_dir::DenoDirProvider;
pub use disk_cache::DiskCache;

View File

@ -0,0 +1,4 @@
@denotest:registry=http://localhost:4261/
//localhost:4261/:_authToken=private-reg-token
@denotest2:registry=http://localhost:4262/
//localhost:4262/:_authToken=private-reg-token2

View File

@ -0,0 +1,11 @@
{
"if": "unix",
"tempDir": true,
"envs": {
"DENO_FUTURE": "1",
"HOME": "$PWD/../"
},
"cwd": "subdir",
"args": "install",
"output": "install.out"
}

View File

@ -0,0 +1,9 @@
⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag.
[UNORDERED_START]
Download http://localhost:4261/@denotest/basic
Download http://localhost:4262/@denotest2/basic
Download http://localhost:4261/@denotest/basic/1.0.0.tgz
Download http://localhost:4262/@denotest2/basic/1.0.0.tgz
Initialize @denotest2/basic@1.0.0
Initialize @denotest/basic@1.0.0
[UNORDERED_END]

View File

@ -0,0 +1,8 @@
import { getValue, setValue } from "@denotest/basic";
import * as test from "@denotest2/basic";
console.log(getValue());
setValue(42);
console.log(getValue());
console.log(test.getValue());

View File

@ -0,0 +1,8 @@
{
"name": "npmrc_test",
"version": "0.0.1",
"dependencies": {
"@denotest/basic": "1.0.0",
"@denotest2/basic": "1.0.0"
}
}

View File

@ -32,6 +32,7 @@ use crate::npm_registry_unset_url;
use crate::pty::Pty;
use crate::strip_ansi_codes;
use crate::testdata_path;
use crate::tests_path;
use crate::HttpServerGuard;
use crate::TempDir;
@ -837,6 +838,7 @@ impl TestCommandBuilder {
text
.replace("$DENO_DIR", &self.deno_dir.path().to_string_lossy())
.replace("$TESTDATA", &testdata_path().to_string_lossy())
.replace("$TESTS", &tests_path().to_string_lossy())
.replace("$PWD", &cwd.to_string_lossy())
}
}