Skip to content

Commit 9780f11

Browse files
bartlomiejuzebreus
authored andcommitted
fix(ext/node): discover .npmrc in user's homedir (denoland#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 denoland#23954 but allows to fallback if no `.npmrc` file is discovered in the project root.
1 parent 97dcea7 commit 9780f11

File tree

9 files changed

+57
-2
lines changed

9 files changed

+57
-2
lines changed

cli/args/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use std::sync::Arc;
7272
use thiserror::Error;
7373

7474
use crate::args::import_map::enhance_import_map_value_with_workspace_members;
75+
use crate::cache;
7576
use crate::file_fetcher::FileFetcher;
7677
use crate::util::fs::canonicalize_path_maybe_not_exists;
7778
use crate::version;
@@ -592,6 +593,7 @@ pub fn discover_npmrc(
592593
Ok(Arc::new(resolved))
593594
}
594595

596+
// 1. Try `.npmrc` next to `package.json`
595597
if let Some(package_json_path) = maybe_package_json_path {
596598
if let Some(package_json_dir) = package_json_path.parent() {
597599
if let Some((source, path)) = try_to_read_npmrc(package_json_dir)? {
@@ -600,6 +602,7 @@ pub fn discover_npmrc(
600602
}
601603
}
602604

605+
// 2. Try `.npmrc` next to `deno.json(c)`
603606
if let Some(deno_json_path) = maybe_deno_json_path {
604607
if let Some(deno_json_dir) = deno_json_path.parent() {
605608
if let Some((source, path)) = try_to_read_npmrc(deno_json_dir)? {
@@ -608,6 +611,15 @@ pub fn discover_npmrc(
608611
}
609612
}
610613

614+
// TODO(bartlomieju): update to read both files - one in the project root and one and
615+
// home dir and then merge them.
616+
// 3. Try `.npmrc` in the user's home directory
617+
if let Some(home_dir) = cache::home_dir() {
618+
if let Some((source, path)) = try_to_read_npmrc(&home_dir)? {
619+
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
620+
}
621+
}
622+
611623
log::debug!("No .npmrc file found");
612624
Ok((create_default_npmrc(), None))
613625
}

cli/cache/deno_dir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl DenoDir {
169169

170170
/// To avoid the poorly managed dirs crate
171171
#[cfg(not(windows))]
172-
mod dirs {
172+
pub mod dirs {
173173
use std::path::PathBuf;
174174

175175
pub fn cache_dir() -> Option<PathBuf> {
@@ -227,7 +227,7 @@ mod dirs {
227227
// https://github.com/dirs-dev/dirs-sys-rs/blob/ec7cee0b3e8685573d847f0a0f60aae3d9e07fa2/src/lib.rs#L140-L164
228228
// MIT license. Copyright (c) 2018-2019 dirs-rs contributors
229229
#[cfg(windows)]
230-
mod dirs {
230+
pub mod dirs {
231231
use std::ffi::OsString;
232232
use std::os::windows::ffi::OsStringExt;
233233
use std::path::PathBuf;

cli/cache/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub use caches::Caches;
4343
pub use check::TypeCheckCache;
4444
pub use code_cache::CodeCache;
4545
pub use common::FastInsecureHasher;
46+
pub use deno_dir::dirs::home_dir;
4647
pub use deno_dir::DenoDir;
4748
pub use deno_dir::DenoDirProvider;
4849
pub use disk_cache::DiskCache;

tests/specs/npm/npmrc_homedir/.npmrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@denotest:registry=http://localhost:4261/
2+
//localhost:4261/:_authToken=private-reg-token
3+
@denotest2:registry=http://localhost:4262/
4+
//localhost:4262/:_authToken=private-reg-token2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"if": "unix",
3+
"tempDir": true,
4+
"envs": {
5+
"DENO_FUTURE": "1",
6+
"HOME": "$PWD/../"
7+
},
8+
"cwd": "subdir",
9+
"args": "install",
10+
"output": "install.out"
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag.
2+
[UNORDERED_START]
3+
Download http://localhost:4261/@denotest/basic
4+
Download http://localhost:4262/@denotest2/basic
5+
Download http://localhost:4261/@denotest/basic/1.0.0.tgz
6+
Download http://localhost:4262/@denotest2/basic/1.0.0.tgz
7+
Initialize @denotest2/[email protected]
8+
Initialize @denotest/[email protected]
9+
[UNORDERED_END]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getValue, setValue } from "@denotest/basic";
2+
import * as test from "@denotest2/basic";
3+
4+
console.log(getValue());
5+
setValue(42);
6+
console.log(getValue());
7+
8+
console.log(test.getValue());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "npmrc_test",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"@denotest/basic": "1.0.0",
6+
"@denotest2/basic": "1.0.0"
7+
}
8+
}

tests/util/server/src/builders.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::npm_registry_unset_url;
3232
use crate::pty::Pty;
3333
use crate::strip_ansi_codes;
3434
use crate::testdata_path;
35+
use crate::tests_path;
3536
use crate::HttpServerGuard;
3637
use crate::TempDir;
3738

@@ -837,6 +838,7 @@ impl TestCommandBuilder {
837838
text
838839
.replace("$DENO_DIR", &self.deno_dir.path().to_string_lossy())
839840
.replace("$TESTDATA", &testdata_path().to_string_lossy())
841+
.replace("$TESTS", &tests_path().to_string_lossy())
840842
.replace("$PWD", &cwd.to_string_lossy())
841843
}
842844
}

0 commit comments

Comments
 (0)