Skip to content

Commit be6505a

Browse files
authored
feat(global): port global package install support to pacquet (#12638)
Port global package installation from the TypeScript pnpm CLI (pnpm11/) to the Rust pacquet port: `add -g`, `remove -g`, `update -g`, `outdated -g`, and a global `list`/`ls`. - New pacquet-global crate (ports @pnpm/global.packages + the pure helpers of @pnpm/global.commands): scan/find global package groups, hash-symlink helpers, createGlobalCacheKey, bin-conflict detection, and the depth-0 list -g tree/json/parseable renderers. - config: default_pnpm_home_dir (mirrors getDataDir), GLOBAL_LAYOUT_VERSION, the global dir fields, and checkGlobalBinDir (NO_PATH_ENV / GLOBAL_BIN_DIR_NOT_IN_PATH / PNPM_DIR_NOT_WRITABLE). Config now derives Clone. - cmd-shim: remove_bin (ports @pnpm/bins.remover) and an exclude_bins variant of link_bins_of_packages. - crypto-hash: create_hex_hash (full hex sha256) backing the cache key. - cli: per-group isolated installs reusing the install pipeline, the interactive promptApproveGlobalBuilds flow, -g on add/remove/update, the outdated -g aggregation, and the new list/ls command (global-only for now). list without -g and runtime --global remain unsupported. This is the pacquet (Rust) side of a feature that already exists in the TypeScript CLI.
1 parent 3425e80 commit be6505a

41 files changed

Lines changed: 2404 additions & 39 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/global-bin-hardening.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@pnpm/bins.remover": patch
3+
"@pnpm/global.packages": patch
4+
"@pnpm/global.commands": patch
5+
"@pnpm/installing.commands": patch
6+
"pnpm": patch
7+
---
8+
9+
Hardened global package management:
10+
11+
- On Windows, removing or updating a global package now also cleans up the `node.exe` flavor of a bin, so a stale `node.exe` no longer survives on `PATH` after uninstall, and a new global install no longer silently overwrites an existing `node.exe`.
12+
- `pnpm add -g pnpm@<version>` (and `@pnpm/exe@<version>`) is now rejected like the bare `pnpm` form, pointing to `pnpm self-update`.
13+
- Dependency aliases read from a global package's manifest are validated before being joined onto `node_modules` paths, preventing a tampered manifest from escaping the install directory.
14+
- Each global install group is created in its own freshly-made directory (no longer reusing a colliding or pre-existing path).
15+
- Removing or updating a global package no longer unlinks a bin that belongs to a different globally installed package.

‎Cargo.lock‎

Lines changed: 16 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pacquet-executor = { path = "pacquet/crates/executor" }
4747
pacquet-exportable-manifest = { path = "pacquet/crates/exportable-manifest" }
4848
pacquet-directory-fetcher = { path = "pacquet/crates/directory-fetcher" }
4949
pacquet-git-fetcher = { path = "pacquet/crates/git-fetcher" }
50+
pacquet-global = { path = "pacquet/crates/global" }
5051
pacquet-deps-path = { path = "pacquet/crates/deps-path" }
5152
pacquet-detect-libc = { path = "pacquet/crates/detect-libc" }
5253
pacquet-diagnostics = { path = "pacquet/crates/diagnostics" }

‎pacquet/crates/cli/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pacquet-pnpr-client = { workspace = true }
2222
pacquet-env-installer = { workspace = true }
2323
pacquet-executor = { workspace = true }
2424
pacquet-fs = { workspace = true }
25+
pacquet-global = { workspace = true }
2526
pacquet-graph-hasher = { workspace = true }
2627
pacquet-hooks = { workspace = true }
2728
pacquet-lockfile = { workspace = true }

‎pacquet/crates/cli/src/cli_args.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ pub mod dlx;
1010
pub mod exec;
1111
pub mod fetch;
1212
pub mod find_hash;
13+
pub mod global;
1314
pub mod ignored_builds;
1415
pub mod import;
1516
pub mod install;
1617
pub mod link;
18+
pub mod list;
1719
pub mod outdated;
1820
pub mod patch;
1921
pub mod patch_commit;
@@ -53,6 +55,7 @@ use ignored_builds::IgnoredBuildsArgs;
5355
use import::ImportArgs;
5456
use install::InstallArgs;
5557
use link::LinkArgs;
58+
use list::ListArgs;
5659
use miette::{Context, IntoDiagnostic};
5760
use outdated::{OutdatedArgs, OutdatedOutcome};
5861
use pacquet_config::{Config, Host};
@@ -176,6 +179,9 @@ pub enum CliCommand {
176179
Outdated(OutdatedArgs),
177180
/// Checks for known security issues with the installed packages.
178181
Audit(AuditArgs),
182+
/// List installed packages (global only for now).
183+
#[clap(visible_alias = "ls")]
184+
List(ListArgs),
179185
/// Shows the packages that depend on `pkg`
180186
Why(WhyArgs),
181187
/// Rebuild a package.
@@ -396,6 +402,20 @@ impl CliArgs {
396402
PackageManifest::init(manifest_path_ref).wrap_err("initialize package.json");
397403
Box::pin(std::future::ready(result))
398404
}
405+
CliCommand::Add(args) if args.global => {
406+
let config = config()?;
407+
match reporter {
408+
ReporterType::Default | ReporterType::AppendOnly => {
409+
Box::pin(args.run_global::<DefaultReporter>(config, dir_ref))
410+
}
411+
ReporterType::Ndjson => {
412+
Box::pin(args.run_global::<NdjsonReporter>(config, dir_ref))
413+
}
414+
ReporterType::Silent => {
415+
Box::pin(args.run_global::<SilentReporter>(config, dir_ref))
416+
}
417+
}
418+
}
399419
CliCommand::Add(args) => {
400420
let command_state = state(false)?;
401421
match reporter {
@@ -406,6 +426,16 @@ impl CliArgs {
406426
ReporterType::Silent => Box::pin(args.run::<SilentReporter>(command_state)),
407427
}
408428
}
429+
CliCommand::Update(args) if args.global => {
430+
let config = config()?;
431+
match reporter {
432+
ReporterType::Default | ReporterType::AppendOnly => {
433+
Box::pin(args.run_global::<DefaultReporter>(config))
434+
}
435+
ReporterType::Ndjson => Box::pin(args.run_global::<NdjsonReporter>(config)),
436+
ReporterType::Silent => Box::pin(args.run_global::<SilentReporter>(config)),
437+
}
438+
}
409439
CliCommand::Update(args) => {
410440
let command_state = state(false)?;
411441
match reporter {
@@ -421,6 +451,15 @@ impl CliArgs {
421451
// install pipeline to dispatch on. It reports back whether any
422452
// dependency was outdated; process termination stays here, at
423453
// the top-level harness, rather than inside the command.
454+
CliCommand::Outdated(args) if args.global => {
455+
let config = config()?;
456+
Box::pin(async move {
457+
if args.run_global(config).await? == OutdatedOutcome::Outdated {
458+
std::process::exit(1);
459+
}
460+
Ok(())
461+
})
462+
}
424463
CliCommand::Outdated(args) => {
425464
let command_state = state(false)?;
426465
Box::pin(async move {
@@ -439,7 +478,15 @@ impl CliArgs {
439478
Ok(())
440479
})
441480
}
481+
CliCommand::List(args) => {
482+
args.run(config()?)?;
483+
Box::pin(std::future::ready(Ok(())))
484+
}
442485
CliCommand::Why(args) => Box::pin(args.run(state(true)?)),
486+
CliCommand::Remove(args) if args.global => {
487+
global::handle_global_remove(config()?, &args.package_names)?;
488+
Box::pin(std::future::ready(Ok(())))
489+
}
443490
CliCommand::Remove(args) => {
444491
let command_state = state(false)?;
445492
match reporter {

‎pacquet/crates/cli/src/cli_args/add.rs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{State, cli_args::supported_architectures::SupportedArchitecturesArgs, config_deps};
22
use clap::Args;
33
use miette::Context;
4+
use pacquet_config::Config;
45
use pacquet_package_manager::Add;
56
use pacquet_package_manifest::DependencyGroup;
67
use pacquet_registry::PinnedVersion;
@@ -104,6 +105,11 @@ pub struct AddArgs {
104105
/// All direct and indirect dependencies of the project are linked into this directory
105106
#[clap(long = "virtual-store-dir", default_value = "node_modules/.pacquet")]
106107
pub virtual_store_dir: Option<PathBuf>, // TODO: make use of this
108+
109+
/// Install the package globally, linking its bins into the global bin
110+
/// directory. Mirrors pnpm's `add -g`.
111+
#[clap(short = 'g', long)]
112+
pub global: bool,
107113
}
108114

109115
impl AddArgs {
@@ -175,12 +181,46 @@ impl AddArgs {
175181
)
176182
.await
177183
}
184+
185+
/// `pnpm add -g`: install the package into the global packages
186+
/// directory and link its bins. Delegates to
187+
/// [`crate::cli_args::global::handle_global_add`].
188+
pub async fn run_global<Reporter: self::Reporter + 'static>(
189+
self,
190+
config: &'static Config,
191+
dir: &Path,
192+
) -> miette::Result<()> {
193+
// `--config` (configurational dependency) and `--lockfile-only` have
194+
// no meaning for a global install; reject rather than silently ignore.
195+
if self.config {
196+
return Err(miette::miette!(
197+
"`pacquet add --config` cannot be combined with --global."
198+
));
199+
}
200+
if self.lockfile_only {
201+
return Err(miette::miette!(
202+
"`pacquet add --lockfile-only` cannot be combined with --global."
203+
));
204+
}
205+
let supported_architectures =
206+
self.supported_architectures.apply_to(config.supported_architectures.clone());
207+
let pinned_version =
208+
PinnedVersion::from_save_options(self.save_exact, self.save_prefix.as_deref());
209+
Box::pin(crate::cli_args::global::handle_global_add::<Reporter>(
210+
config,
211+
&[self.package_name],
212+
pinned_version,
213+
supported_architectures,
214+
dir,
215+
))
216+
.await
217+
}
178218
}
179219

180220
/// Add a single package to `state`'s manifest and install it.
181221
///
182222
/// Shared by `pacquet add` and `pacquet dlx`. dlx points `state` at a
183-
/// cache directory (via a [`Config`](pacquet_config::Config) whose
223+
/// cache directory (via a [`Config`] whose
184224
/// `modules_dir` is anchored there) and saves to `dependencies` so the
185225
/// package's bin lands in `<cacheDir>/node_modules/.bin`.
186226
pub(crate) async fn add_package<Reporter, ListDependencyGroups, DependencyGroupList>(

0 commit comments

Comments
 (0)